Skip to content

Commit 376dcad

Browse files
committed
feat: Improve search functionality and configuration
This commit enhances the search feature with: - Dynamic Fuse.js loading - Configurable search index URL - Adjusted search weights and threshold for better results - Added sorting by score and priority - Enhanced search configuration options in hugo.toml - Expanded searchable sections to include home page
1 parent 272a54d commit 376dcad

File tree

3 files changed

+157
-8
lines changed

3 files changed

+157
-8
lines changed

assets/js/search.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,28 @@
1515
let searchData;
1616
let isFuseInitialized = false;
1717

18+
// Function to load a script dynamically
19+
function loadScript(src) {
20+
return new Promise((resolve, reject) => {
21+
const script = document.createElement('script');
22+
script.src = src;
23+
script.onload = resolve;
24+
script.onerror = reject;
25+
document.head.appendChild(script);
26+
});
27+
}
28+
1829
async function initFuse() {
1930
if (isFuseInitialized) return;
2031
try {
32+
// Check if Fuse is loaded, if not, load it.
33+
if (typeof Fuse === 'undefined') {
34+
if (!window.fuseJSSrc) {
35+
throw new Error('Fuse.js library source URL not provided via window.fuseJSSrc');
36+
}
37+
await loadScript(window.fuseJSSrc);
38+
}
39+
2140
if (!window.searchIndexURL) {
2241
throw new Error('Search index URL not provided via window.searchIndexURL');
2342
}
@@ -28,15 +47,27 @@
2847
searchData = await response.json();
2948
const options = {
3049
keys: [
31-
{ name: 'title', weight: 0.8 },
32-
{ name: 'content', weight: 0.5 },
33-
{ name: 'description', weight: 0.6 },
50+
{ name: 'title', weight: 1.0 },
51+
{ name: 'description', weight: 0.4 },
52+
{ name: 'content', weight: 0.2 },
3453
{ name: 'tags', weight: 0.4 },
3554
{ name: 'categories', weight: 0.4 }
3655
],
3756
includeMatches: true,
3857
minMatchCharLength: 2,
39-
threshold: 0.3, // Lowered from 0.4 to make search stricter
58+
threshold: 0.2, // Stricter search threshold
59+
sortFn: (a, b) => {
60+
// Primary sort: by score (ascending, lower is better)
61+
if (a.score !== b.score) {
62+
return a.score - b.score;
63+
}
64+
// Secondary sort: by priority (ascending, lower is better)
65+
if (a.item.priority !== b.item.priority) {
66+
return a.item.priority - b.item.priority;
67+
}
68+
// Tertiary sort: by original index in the list
69+
return a.refIndex - b.refIndex;
70+
}
4071
};
4172
fuse = new Fuse(searchData, options);
4273
isFuseInitialized = true;

hugo.toml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,103 @@ author = "authors"
135135
[services.rss]
136136
# Number of items to include in the RSS feed.
137137
limit = 20
138+
139+
#################### default parameters ################################
140+
# favicon
141+
favicon = "images/favicon.png"
142+
# logo
143+
logo = "images/logo.png"
144+
logo_darkmode = "images/logo-darkmode.png"
145+
# use `px` or `x` with logo_width, example: "100px".
146+
# Note: logo_width is not work with .svg file
147+
logo_width = "350px"
148+
logo_height = "54px"
149+
# if logo_webp set false, will not generate WEBP version of logo | default is true
150+
logo_webp = true
151+
# logo text will only show when logo is missing.
152+
logo_text = "Open Neuromorphic"
153+
# navbar fixed to top
154+
navbar_fixed = true
155+
# theme-mode
156+
theme_switcher = true
157+
theme_default = "system" # available options [light/dark/system]
158+
# Main Sections
159+
mainSections = ["blog","workshops"]
160+
# contact form action
161+
contact_form_action = "#" # contact form works with [https://airform.io/] or [https://formspree.io]
162+
# google tag manager, see https://developers.google.com/tag-manager/
163+
google_tag_manager = "" # example: G-XXXXXXXXXX
164+
google_adsense = "" # example: ca-pub-xxxxxxxxxxxxxxxx
165+
# custom script on header, example: custom_script= "<script>console.log(\"Hello World\")</script>"
166+
custom_script = ""
167+
# copyright
168+
copyright = "© 2022 - 2025 Open Neuromorphic"
169+
170+
homepageTitle = "Open Neuromorphic is a global community fostering education, research, and open-source collaboration in brain-inspired AI and hardware. "
171+
172+
# Preloader
173+
# preloader module: https://github.com/gethugothemes/hugo-modules/tree/master/components/preloader
174+
[preloader]
175+
enable = false
176+
preloader = "" # use jpg, png, svg or gif format.
177+
178+
# Navigation button
179+
[navigation_button]
180+
enable = true
181+
label = "Join Discord"
182+
label_sub = "(2100+ joined)"
183+
link = "https://discord.gg/hUygPUdD8E"
184+
185+
# search
186+
[search]
187+
enable = true
188+
primary_color = "#667eea"
189+
include_sections = ["blog", "workshops", "neuromorphic-computing", "contributors", "getting-involved", "volunteer-opportunities"]
190+
show_image = false
191+
show_description = true
192+
show_tags = true
193+
show_categories = true
194+
195+
196+
# seo meta data for OpenGraph / Twitter Card
197+
# seo module: https://github.com/gethugothemes/hugo-modules/tree/master/seo-tools/basic-seo
198+
[metadata]
199+
keywords = ["neuromorphic computing", "artificial intelligence", "open source"]
200+
description = "Explore the world of Neuromorphic Computing, AI, and Devices in an open-source community. Join us for educational content, and collaborative innovation"
201+
author = "Open Neuromorphic"
202+
image = "images/og-image.png"
203+
204+
205+
# site verifications
206+
# verification module: https://github.com/gethugothemes/hugo-modules/tree/master/seo-tools/site-verifications
207+
[site_verification]
208+
google = "" # Your verification code
209+
bing = "" # Your verification code
210+
baidu = "" # Your verification code
211+
facebook = "" # Your verification code
212+
mastodon = "" # Your verification code
213+
214+
# cookies
215+
# cookies module: https://github.com/gethugothemes/hugo-modules/tree/master/components/cookie-consent
216+
[cookies]
217+
enable = true
218+
expire_days = 60
219+
content = "This site uses cookies & analytics. By continuing to use this website, you agree to their use."
220+
button = "I Accept"
221+
222+
223+
[[params.plugins.js]]
224+
link = "plugins/lazy-loader.js"
225+
#[[params.plugins.js]]
226+
#link = "plugins/cookie.js"
227+
228+
######################## sidebar widgets #########################
229+
[widgets]
230+
sidebar = []
231+
232+
# Subscription
233+
[subscription]
234+
enable = false
235+
# mailchimp subsciption
236+
#mailchimp_form_action = "https://gmail.us4.list-manage.com/subscribe/post?u=463ee871f45d2d93748e77cad&id=a0a2c6d074" # replace this url with yours
237+
#mailchimp_form_name = "b_463ee871f45d2d93748e77cad_a0a2c6d074"

layouts/index.SearchIndex.json

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
{{- $.Scratch.Add "index" slice -}}
22
{{- $searchableSections := site.Params.search.include_sections | default (slice "blog") -}}
3-
{{- range site.RegularPages -}}
3+
{{- range where site.Pages "Kind" "in" (slice "page" "section" "home") -}}
44
{{- $page := . -}}
5+
{{- if and (ne .Params.draft true) (ne .Params.exclude_sitemap true) -}}
56
{{- $isSearchable := false -}}
7+
{{- if .IsHome -}}
8+
{{- $isSearchable = true -}}
9+
{{- else if .File -}}
610
{{- range $searchableSections -}}
7-
{{- /* Corrected path check: .File.Path is relative to /content, so we don't need to add "content/" */ -}}
811
{{- if hasPrefix $page.File.Path . -}}
912
{{- $isSearchable = true -}}
1013
{{- end -}}
1114
{{- end -}}
12-
{{- if and $isSearchable (ne .Params.draft true) (ne .Params.exclude_sitemap true) -}}
13-
{{- $.Scratch.Add "index" (dict "title" .Title "description" .Description "permalink" .RelPermalink "tags" .Params.tags "categories" .Params.categories "content" (.Plain | truncate 2000)) -}}
15+
{{- end -}}
16+
17+
{{- if $isSearchable -}}
18+
{{- $pathSegments := split (strings.TrimSuffix "/" .RelPermalink) "/" -}}
19+
{{- $priority := len $pathSegments -}}
20+
{{- if .IsHome }}{{ $priority = 0 }}{{ end -}}
21+
{{- $.Scratch.Add "index" (dict
22+
"title" .Title
23+
"description" .Description
24+
"permalink" .RelPermalink
25+
"tags" .Params.tags
26+
"categories" .Params.categories
27+
"content" (.Plain | truncate 2000)
28+
"priority" $priority
29+
)
30+
-}}
31+
{{- end -}}
1432
{{- end -}}
1533
{{- end -}}
1634
{{- $.Scratch.Get "index" | jsonify -}}

0 commit comments

Comments
 (0)