aboutsummaryrefslogtreecommitdiff
path: root/assets
diff options
context:
space:
mode:
authorAlex Shpak <alex-shpak@users.noreply.github.com>2021-05-19 20:26:21 +0200
committerAlex Shpak <alex-shpak@users.noreply.github.com>2021-05-19 20:26:21 +0200
commitd7c644f0e7062bd847911bcfd9d9eaf401f3c36d (patch)
tree7b9d3c6d1587df722aebf271737ab9c5b1d82b7c /assets
parent843144dd85e374ade722e2f2b0b3511a0e34d8f6 (diff)
Refactor search, prepare for pre-built indexing
Diffstat (limited to 'assets')
-rw-r--r--assets/search-data.js32
-rw-r--r--assets/search-data.json15
-rw-r--r--assets/search.js40
3 files changed, 35 insertions, 52 deletions
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);
}
/**