From a5788d71fa7ac1cd6e5ed9a98f95f6ca1bddcbb4 Mon Sep 17 00:00:00 2001 From: Alex Shpak Date: Sun, 27 Oct 2019 11:58:58 +0100 Subject: #80, Add search index configuration --- assets/search-data.js | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) (limited to 'assets/search-data.js') 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 -}} })(); -- cgit v1.2.3