diff options
author | Alex Shpak <alex-shpak@users.noreply.github.com> | 2019-10-20 19:02:32 +0200 |
---|---|---|
committer | Alex Shpak <alex-shpak@users.noreply.github.com> | 2019-10-23 21:32:15 +0200 |
commit | b4307e7c094c3a556e267b136e591c146a41f12f (patch) | |
tree | c76047ef54bd7a15ec198f8d40542fc97afe39a7 /assets | |
parent | 4596f08e323ce7ddb67d86705387115bc7e03045 (diff) |
#80, Migrate to flexsearch
Diffstat (limited to 'assets')
-rw-r--r-- | assets/search-data.js | 28 | ||||
-rw-r--r-- | assets/search.js | 39 |
2 files changed, 30 insertions, 37 deletions
diff --git a/assets/search-data.js b/assets/search-data.js index dee01f6..6848438 100644 --- a/assets/search-data.js +++ b/assets/search-data.js @@ -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, } })(); diff --git a/assets/search.js b/assets/search.js index 6799c8d..b9cbb11 100644 --- a/assets/search.js +++ b/assets/search.js @@ -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; |