From d7c644f0e7062bd847911bcfd9d9eaf401f3c36d Mon Sep 17 00:00:00 2001 From: Alex Shpak Date: Wed, 19 May 2021 20:26:21 +0200 Subject: Refactor search, prepare for pre-built indexing --- assets/search-data.js | 32 -------------------------------- assets/search-data.json | 15 +++++++++++++++ assets/search.js | 40 ++++++++++++++++++++-------------------- 3 files changed, 35 insertions(+), 52 deletions(-) delete mode 100644 assets/search-data.js create mode 100644 assets/search-data.json (limited to 'assets') diff --git a/assets/search-data.js b/assets/search-data.js deleted file mode 100644 index a93664e..0000000 --- a/assets/search-data.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict'; - -(function () { - const indexCfg = {{ with i18n "bookSearchConfig" }} - {{ . }}; - {{ else }} - {}; - {{ end }} - - indexCfg.doc = { - id: 'id', - field: ['title', 'content'], - store: ['title', 'href', 'section'], - }; - - const index = FlexSearch.create('balance', indexCfg); - window.bookSearchIndex = index; - - {{- $pages := where .Site.Pages "Kind" "in" (slice "page" "section") -}} - {{- $pages = where $pages "Params.booksearchexclude" "!=" true -}} - {{- $pages = where $pages "Content" "not in" (slice nil "") -}} - - {{ range $index, $page := $pages }} - index.add({ - 'id': {{ $index }}, - 'href': '{{ $page.RelPermalink }}', - 'title': {{ (partial "docs/title" $page) | jsonify }}, - 'section': {{ (partial "docs/title" $page.Parent) | jsonify }}, - 'content': {{ $page.Plain | jsonify }} - }); - {{- end -}} -})(); diff --git a/assets/search-data.json b/assets/search-data.json new file mode 100644 index 0000000..9a137c3 --- /dev/null +++ b/assets/search-data.json @@ -0,0 +1,15 @@ +[ +{{- $pages := where .Site.Pages "Kind" "in" (slice "page" "section") -}} +{{- $pages = where $pages "Params.booksearchexclude" "!=" true -}} +{{- $pages = where $pages "Content" "not in" (slice nil "") -}} + +{{ range $index, $page := $pages }} +{{ if gt $index 0}},{{end}} { + "id": {{ $index }}, + "href": "{{ $page.RelPermalink }}", + "title": {{ (partial "docs/title" $page) | jsonify }}, + "section": {{ (partial "docs/title" $page.Parent) | jsonify }}, + "content": {{ $page.Plain | jsonify }} +} +{{- end -}} +] diff --git a/assets/search.js b/assets/search.js index 3635a1f..4e1c1da 100644 --- a/assets/search.js +++ b/assets/search.js @@ -1,9 +1,19 @@ 'use strict'; -{{ $searchDataFile := printf "%s.search-data.js" .Language.Lang }} -{{ $searchData := resources.Get "search-data.js" | resources.ExecuteAsTemplate $searchDataFile . | resources.Minify | resources.Fingerprint }} +{{ $searchDataFile := printf "%s.search-data.json" .Language.Lang }} +{{ $searchData := resources.Get "search-data.json" | resources.ExecuteAsTemplate $searchDataFile . | resources.Minify | resources.Fingerprint }} +{{ $searchConfig := i18n "bookSearchConfig" | default "{}" }} (function () { + const searchDataURL = '{{ $searchData.RelPermalink }}'; + const indexConfig = Object.assign({{ $searchConfig }}, { + doc: { + id: 'id', + field: ['title', 'content'], + store: ['title', 'href', 'section'] + } + }); + const input = document.querySelector('#book-search-input'); const results = document.querySelector('#book-search-results'); @@ -46,11 +56,14 @@ input.removeEventListener('focus', init); // init once input.required = true; - loadScript('{{ "flexsearch.min.js" | relURL }}'); - loadScript('{{ $searchData.RelPermalink }}', function () { - input.required = false; - search(); - }); + fetch(searchDataURL) + .then(pages => pages.json()) + .then(pages => { + window.bookSearchIndex = FlexSearch.create('balance', indexConfig); + window.bookSearchIndex.add(pages); + }) + .then(() => input.required = false) + .then(search); } function search() { @@ -74,19 +87,6 @@ results.appendChild(li); }); } - - /** - * @param {String} src - * @param {Function} callback - */ - function loadScript(src, callback) { - const script = document.createElement('script'); - script.defer = true; - script.async = false; - script.src = src; - script.onload = callback; - - document.head.appendChild(script); } /** -- cgit v1.2.3