Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace flexsearch with fusejs #600

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 20 additions & 11 deletions assets/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@
(function () {
const searchDataURL = '{{ $searchData.RelPermalink }}';
const indexConfig = Object.assign({{ $searchConfig }}, {
doc: {
id: 'id',
field: ['title', 'content'],
store: ['title', 'href', 'section']
}
includeScore: true,
useExtendedSearch: true,
fieldNormWeight: 1.5,
threshold: 0.2,
ignoreLocation: true,
keys: [
{
name: 'title',
weight: 0.7
},
{
name: 'content',
weight: 0.3
}
]
});

const input = document.querySelector('#book-search-input');
Expand Down Expand Up @@ -63,8 +73,7 @@
fetch(searchDataURL)
.then(pages => pages.json())
.then(pages => {
window.bookSearchIndex = FlexSearch.create('balance', indexConfig);
window.bookSearchIndex.add(pages);
window.bookSearchIndex = new Fuse(pages, indexConfig);
})
.then(() => input.required = false)
.then(search);
Expand All @@ -79,14 +88,14 @@
return;
}

const searchHits = window.bookSearchIndex.search(input.value, 10);
const searchHits = window.bookSearchIndex.search(input.value).slice(0,10);
searchHits.forEach(function (page) {
const li = element('<li><a href></a><small></small></li>');
const a = li.querySelector('a'), small = li.querySelector('small');

a.href = page.href;
a.textContent = page.title;
small.textContent = page.section;
a.href = page.item.href;
a.textContent = page.item.title;
small.textContent = page.item.section;

results.appendChild(li);
});
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/docs/html-head.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{{- if default true .Site.Params.BookSearch -}}
{{- $searchJSFile := printf "%s.search.js" .Language.Lang }}
{{- $searchJS := resources.Get "search.js" | resources.ExecuteAsTemplate $searchJSFile . | resources.Minify | resources.Fingerprint }}
<script defer src="{{ "flexsearch.min.js" | relURL }}"></script>
<script defer src="{{ "fuse.min.js" | relURL }}"></script>
<script defer src="{{ $searchJS.RelPermalink }}" {{ template "integrity" $searchJS }}></script>
{{ end -}}

Expand Down
42 changes: 0 additions & 42 deletions static/flexsearch.min.js

This file was deleted.