Skip to content

Commit

Permalink
Expand search.json to include more searchable fields (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
dallasread authored Feb 13, 2025
1 parent 239d978 commit 1c72aa8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
27 changes: 20 additions & 7 deletions lib/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,28 @@ module Search
include Nanoc::Helpers::Text

def create_search_index
index = []
@items.each do |item|
if item.attributes[:filename].end_with?('markdown')
index << { id: item.path, title: item.attributes[:title], body: strip_html(item.compiled_content) }
index_file = File.join(@config[:output_dir], 'search.json')
File.write(index_file, articles_json)
end

def articles_json
@articles_json ||= begin
index = []

@items.each do |item|
next unless item.attributes[:filename].end_with?('markdown')

index << {
id: item.path,
title: item.attributes[:title],
excerpt: item[:excerpt],
categories: item[:categories],
body: item.compiled_content
}
end
end

index_file = File.join(@config[:output_dir], 'search.json')
File.write(index_file, JSON.generate(index))
JSON.generate(index)
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
publish = "output"
command = "NANOC_ENV=production rake publish"

[[headers]]
for = "/search.json"
[headers.values]
Access-Control-Allow-Origin = "*"

[[headers]]
for = "/*"
[headers.values]
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function toggleColorMode (e) {
};

const toggleColorButtons = document.querySelectorAll(".color-mode__btn");
console.log(toggleColorButtons);

toggleColorButtons.forEach(btn => {
btn.addEventListener("click", toggleColorMode);
Expand Down

0 comments on commit 1c72aa8

Please sign in to comment.