Skip to content

Commit 7de9edb

Browse files
up
1 parent a7b8995 commit 7de9edb

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

_plugins/opensearch_suggestions.rb

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'nokogiri'
22
require 'json'
3+
require 'open-uri'
34

45
module Jekyll
56
class OpenSearchSuggestions < Liquid::Tag
@@ -10,16 +11,18 @@ def initialize(tag_name, text, tokens)
1011

1112
def render(context)
1213
site = context.registers[:site]
13-
sitemap_path = File.join(site.source, 'sitemap.xml')
14+
15+
# Use the site's URL from _config.yml
16+
site_url = site.config['url']
17+
sitemap_url = "#{site_url}/sitemap.xml"
1418

1519
debug_info = {
1620
search_term: @search_term,
17-
sitemap_path: sitemap_path,
18-
sitemap_exists: File.exist?(sitemap_path)
21+
sitemap_url: sitemap_url
1922
}
2023

21-
if File.exist?(sitemap_path)
22-
sitemap = File.read(sitemap_path)
24+
begin
25+
sitemap = URI.open(sitemap_url).read
2326
doc = Nokogiri::XML(sitemap)
2427

2528
urls = doc.xpath('//xmlns:url/xmlns:loc').map(&:text)
@@ -35,8 +38,10 @@ def render(context)
3538

3639
result = [@search_term, suggestions.map(&:first), suggestions.map(&:last), debug_info]
3740
result.to_json
38-
else
39-
{ error: "Sitemap not found", debug_info: debug_info }.to_json
41+
rescue OpenURI::HTTPError => e
42+
{ error: "Failed to fetch sitemap: #{e.message}", debug_info: debug_info }.to_json
43+
rescue StandardError => e
44+
{ error: "An error occurred: #{e.message}", debug_info: debug_info }.to_json
4045
end
4146
end
4247
end

0 commit comments

Comments
 (0)