#80, Migrate to flexsearch
This commit is contained in:
+17
-11
@@ -3,22 +3,28 @@
|
||||
{{ range $index, $page := .Site.Pages }}
|
||||
{{- if $index -}},{{- end }}
|
||||
{
|
||||
"idx": {{ $index }},
|
||||
"href": "{{ $page.RelPermalink }}",
|
||||
"title": {{ (partial "docs/title" $page) | jsonify }},
|
||||
"content": {{ $page.Plain | jsonify }}
|
||||
'idx': {{ $index }},
|
||||
'href': '{{ $page.RelPermalink }}',
|
||||
'title': {{ (partial "docs/title" $page) | jsonify }},
|
||||
'content': {{ $page.Plain | jsonify }}
|
||||
}
|
||||
{{- end -}}
|
||||
];
|
||||
|
||||
var index = new FlexSearch({
|
||||
cache: true,
|
||||
encode: 'balance',
|
||||
/* tokenize: function(str) {
|
||||
return str.replace(/[\x00-\x7F]/g, ' ').split('');
|
||||
} */
|
||||
});
|
||||
|
||||
pages.forEach(function(page, x) {
|
||||
index.add(x, pages[x].content);
|
||||
})
|
||||
|
||||
window.bookSearch = {
|
||||
pages: pages,
|
||||
idx: lunr(function() {
|
||||
this.ref("idx");
|
||||
this.field("title");
|
||||
this.field("content");
|
||||
|
||||
pages.forEach(this.add, this);
|
||||
}),
|
||||
index: index,
|
||||
}
|
||||
})();
|
||||
|
||||
+13
-26
@@ -1,18 +1,18 @@
|
||||
{{- $searchData := resources.Get "search-data.js" | resources.ExecuteAsTemplate "search-data.js" . | resources.Minify | resources.Fingerprint }}
|
||||
|
||||
(function() {
|
||||
const input = document.querySelector("#book-search-input");
|
||||
const results = document.querySelector("#book-search-results");
|
||||
const input = document.querySelector('#book-search-input');
|
||||
const results = document.querySelector('#book-search-results');
|
||||
|
||||
input.addEventListener("focus", init);
|
||||
input.addEventListener("keyup", search);
|
||||
input.addEventListener('focus', init);
|
||||
input.addEventListener('keyup', search);
|
||||
|
||||
function init() {
|
||||
input.removeEventListener("focus", init); //init once
|
||||
input.removeEventListener('focus', init); //init once
|
||||
input.required = true;
|
||||
|
||||
loadScript("{{ "lunr.min.js" | relURL }}");
|
||||
loadScript("{{ $searchData.RelPermalink }}", function() {
|
||||
loadScript('{{ "flexsearch.light.js" | relURL }}');
|
||||
loadScript('{{ $searchData.RelPermalink }}', function() {
|
||||
input.required = false;
|
||||
search();
|
||||
});
|
||||
@@ -27,24 +27,11 @@
|
||||
return;
|
||||
}
|
||||
|
||||
const terms = lunr.tokenizer(input.value);
|
||||
const searchHits = window.bookSearch.idx.query(function(query) {
|
||||
query.term(terms, {
|
||||
boost: 100
|
||||
});
|
||||
query.term(terms, {
|
||||
boost: 10,
|
||||
wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING
|
||||
});
|
||||
query.term(terms, {
|
||||
editDistance: 2
|
||||
});
|
||||
});
|
||||
|
||||
searchHits.slice(0, 10).forEach(function(hit) {
|
||||
const page = window.bookSearch.pages[hit.ref];
|
||||
const li = document.createElement("li"),
|
||||
a = li.appendChild(document.createElement("a"));
|
||||
let searchHits = window.bookSearch.index.search(input.value, 10);
|
||||
searchHits.forEach(function(hit) {
|
||||
const page = window.bookSearch.pages[hit];
|
||||
const li = document.createElement('li'),
|
||||
a = li.appendChild(document.createElement('a'));
|
||||
|
||||
a.href = page.href;
|
||||
a.textContent = page.title;
|
||||
@@ -54,7 +41,7 @@
|
||||
}
|
||||
|
||||
function loadScript(src, callback) {
|
||||
const script = document.createElement("script");
|
||||
const script = document.createElement('script');
|
||||
script.defer = true;
|
||||
script.async = false;
|
||||
script.src = src;
|
||||
|
||||
Reference in New Issue
Block a user