diff options
author | Alex Shpak <alex-shpak@users.noreply.github.com> | 2019-10-27 11:58:58 +0100 |
---|---|---|
committer | Alex Shpak <alex-shpak@users.noreply.github.com> | 2019-10-27 11:58:58 +0100 |
commit | a5788d71fa7ac1cd6e5ed9a98f95f6ca1bddcbb4 (patch) | |
tree | 269e971fd25dd3f3f7e2327950f5bec2ef107957 /assets | |
parent | b4307e7c094c3a556e267b136e591c146a41f12f (diff) |
#80, Add search index configuration
Diffstat (limited to 'assets')
-rw-r--r-- | assets/search-data.js | 45 | ||||
-rw-r--r-- | assets/search.js | 14 |
2 files changed, 27 insertions, 32 deletions
diff --git a/assets/search-data.js b/assets/search-data.js index 6848438..a3fd51b 100644 --- a/assets/search-data.js +++ b/assets/search-data.js @@ -1,30 +1,25 @@ +'use strict'; + (function() { - const pages = [ - {{ range $index, $page := .Site.Pages }} - {{- if $index -}},{{- end }} - { - 'idx': {{ $index }}, - 'href': '{{ $page.RelPermalink }}', - 'title': {{ (partial "docs/title" $page) | jsonify }}, - 'content': {{ $page.Plain | jsonify }} - } - {{- end -}} - ]; + const indexCfg = {{ with .Site.Params.BookSearchConfig }} + {{ . }} + {{ end }}; - var index = new FlexSearch({ - cache: true, - encode: 'balance', - /* tokenize: function(str) { - return str.replace(/[\x00-\x7F]/g, ' ').split(''); - } */ - }); + indexCfg.doc = { + id: 'id', + field: ['title', 'content'], + store: ['title', 'href'], + }; - pages.forEach(function(page, x) { - index.add(x, pages[x].content); - }) + const index = FlexSearch.create('balance', indexCfg); + window.bookSearchIndex = index; - window.bookSearch = { - pages: pages, - index: index, - } + {{ range $index, $page := .Site.Pages }} + index.add({ + 'id': {{ $index }}, + 'href': '{{ $page.RelPermalink }}', + 'title': {{ (partial "docs/title" $page) | jsonify }}, + 'content': {{ $page.Plain | jsonify }} + }); + {{- end -}} })(); diff --git a/assets/search.js b/assets/search.js index b9cbb11..89302b1 100644 --- a/assets/search.js +++ b/assets/search.js @@ -1,5 +1,6 @@ -{{- $searchData := resources.Get "search-data.js" | resources.ExecuteAsTemplate "search-data.js" . | resources.Minify | resources.Fingerprint }} +'use strict'; +{{- $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'); @@ -8,10 +9,10 @@ input.addEventListener('keyup', search); function init() { - input.removeEventListener('focus', init); //init once + input.removeEventListener('focus', init); // init once input.required = true; - loadScript('{{ "flexsearch.light.js" | relURL }}'); + loadScript('{{ "flexsearch.min.js" | relURL }}'); loadScript('{{ $searchData.RelPermalink }}', function() { input.required = false; search(); @@ -27,9 +28,8 @@ return; } - let searchHits = window.bookSearch.index.search(input.value, 10); - searchHits.forEach(function(hit) { - const page = window.bookSearch.pages[hit]; + const searchHits = window.bookSearchIndex.search(input.value, 10); + searchHits.forEach(function(page) { const li = document.createElement('li'), a = li.appendChild(document.createElement('a')); @@ -47,6 +47,6 @@ script.src = src; script.onload = callback; - document.head.append(script); + document.head.appendChild(script); } })(); |