Skip to content

Commit

Permalink
feat: add more Fuse.js options
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed Sep 18, 2022
1 parent c95a4a5 commit ce758a2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
3 changes: 3 additions & 0 deletions assets/search/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Engine {
constructor(form: Form, callback: (data: FormData) => void) {
const options = Object.assign(window.fuseOptions, {
useExtendedSearch: true,
includeMatches: true,
includeScore: true,
keys: [
'title',
'content',
Expand Down Expand Up @@ -35,6 +37,7 @@ class Engine {
datalist.appendChild(option);
}
}
console.debug('Fuse.js options', options)
this.fuse = new Fuse(pages, options);
callback(form.data());
}).catch((err) => {
Expand Down
10 changes: 6 additions & 4 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,17 @@ viewer = true # Image Viewer
[search]
# paginate = 5 # Pagination. Default to 10.
# resultContentWordCount = 240 # The maximum word count of result content for displaying.
# Fuse.js options. See https://fusejs.io/api/options.html.
# Fuse.js options, the following options are available. See https://fusejs.io/api/options.html.
[search.fuse]
# ignoreLocation = true
# location = 0
# isCaseSensitive = false
minMatchCharLength = 3
# isCaseSensitive = true
# minMatchCharLength = 3
# threshold = 0.6
# distance = 100
# useExtendedSearch = true
# findAllMatches = true
# ignoreFieldNorm = true
# fieldNormWeight = 0

#[docsearch]
# # https://docsearch.algolia.com/docs/api
Expand Down
26 changes: 14 additions & 12 deletions layouts/partials/assets/search/js.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
{{- if eq .Type "search" -}}
{{- $titleKey := dict "name" "title" "weight" 0.8 -}}
{{- $contentKey := dict "name" "content" "weight" 0.5 -}}
{{- $tagsKey := dict "name" "tags" "weight" 0.3 -}}
{{- $categoriesKey := dict "name" "categories" "weight" 0.3 -}}
{{- $seriesKey := dict "name" "series" "weight" 0.3 -}}
{{- $keys := slice $titleKey $contentKey $tagsKey $categoriesKey $seriesKey -}}
{{- $options := dict "ignoreLocation" true "includeMatches" true "includeScore" true "keys" $keys -}}
{{- $optionWhitelist := slice "ignoreLocation" "isCaseSensitive" "minMatchCharLength" "threshold" "distance" "useExtendedSearch" -}}
{{- $options := dict "ignoreLocation" true -}}
{{- $optionWhitelist := dict
"ignorelocation" "ignoreLocation"
"iscasesensitive" "isCaseSensitive"
"minmatchcharlength" "minMatchCharLength"
"location" "location"
"threshold" "threshold"
"distance" "distance"
"findallmatches" "findAllMatches"
"ignorefieldnorm" "ignoreFieldNorm"
"fieldnormweight" "fieldNormWeight"
-}}
{{- with .Site.Params.search.fuse -}}
{{- range $key, $value := . -}}
{{- range $optionWhitelist -}}
{{- if eq $key (lower .) -}}
{{- $options = merge $options (dict . $value) -}}
{{- end -}}
{{- if isset $optionWhitelist $key -}}
{{- $options = merge $options (dict (index $optionWhitelist $key) $value) -}}
{{- end -}}
{{- end -}}
{{- end -}}
Expand Down

0 comments on commit ce758a2

Please sign in to comment.