Skip to content

Commit

Permalink
Update generate_search_json.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyjurg authored Jul 12, 2024
1 parent e66f722 commit 61c2ab7
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions docs/_plugins/generate_search_json.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
require 'json'
require 'nokogiri'

module Jekyll
class SearchJsonGenerator < Generator
safe true

def generate(site)
items = []
site.posts.docs.each do |post|

Dir.glob('_site/docs/**/*.html').each do |file|
doc = Nokogiri::HTML(File.read(file))
content = doc.css('body').text.gsub(/<\/?[^>]*>/, "") # Remove HTML tags
title = doc.at_css('title')&.text || 'No Title'
url = file.gsub('_site', '')

items << {
title: post.data['title'],
url: post.url,
content: post.content.gsub(/<\/?[^>]*>/, "") # Remove HTML tags
title: title,
url: url,
content: content
}
end

File.open('../search.json', 'w') do |f|
File.open(File.join(site.dest, '../search.json'), 'w') do |f|
f.write(JSON.pretty_generate(items))
end
end
Expand Down

0 comments on commit 61c2ab7

Please sign in to comment.