Refactor search, prepare for pre-built indexing
This commit is contained in:
parent
843144dd85
commit
d7c644f0e7
6 changed files with 47 additions and 63 deletions
|
@ -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 -}}
|
||||
})();
|
15
assets/search-data.json
Normal file
15
assets/search-data.json
Normal file
|
@ -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 -}}
|
||||
]
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1 +1 @@
|
|||
{"Target":"book.min.88999ddd76b75d2f982298d6a6ebd4b60f2068f6f112c9439a998f77b12ded54.css","MediaType":"text/css","Data":{"Integrity":"sha256-iJmd3Xa3XS+YIpjWpuvUtg8gaPbxEslDmpmPd7Et7VQ="}}
|
||||
{"Target":"book.min.e246e6a45b940b6ff71d8216783f10a80d910290a7c364c8bfd5d305d8545d62.css","MediaType":"text/css","Data":{"Integrity":"sha256-4kbmpFuUC2/3HYIWeD8QqA2RApCnw2TIv9XTBdhUXWI="}}
|
|
@ -13,19 +13,20 @@
|
|||
|
||||
{{- range .Translations }}
|
||||
<link rel="alternate" hreflang="{{ .Language.Lang }}" href="{{ .Permalink }}" title="{{ partial "docs/title" . }}">
|
||||
{{ end -}}
|
||||
{{- end -}}
|
||||
|
||||
<!-- Theme stylesheet, you can customize scss by creating `assets/custom.scss` in your website -->
|
||||
{{- $styles := resources.Get "book.scss" | resources.ExecuteAsTemplate "book.scss" . | resources.ToCSS | resources.Minify | resources.Fingerprint }}
|
||||
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Integrity }}">
|
||||
|
||||
{{- if default true .Site.Params.BookSearch }}
|
||||
{{- if default true .Site.Params.BookSearch -}}
|
||||
{{- $searchJSFile := printf "%s.search.js" .Language.Lang }}
|
||||
{{- $searchJS := resources.Get "search.js" | resources.ExecuteAsTemplate $searchJSFile . | resources.Minify | resources.Fingerprint }}
|
||||
<script defer src="{{ "flexsearch.min.js" | relURL }}"></script>
|
||||
<script defer src="{{ $searchJS.RelPermalink }}" integrity="{{ $searchJS.Data.Integrity }}"></script>
|
||||
{{ end -}}
|
||||
|
||||
{{- if .Site.Params.BookServiceWorker }}
|
||||
{{- if .Site.Params.BookServiceWorker -}}
|
||||
{{- $swJS := resources.Get "sw-register.js" | resources.ExecuteAsTemplate "sw.js" . | resources.Minify | resources.Fingerprint }}
|
||||
<script defer src="{{ $swJS.RelPermalink }}" integrity="{{ $swJS.Data.Integrity }}"></script>
|
||||
{{ end -}}
|
||||
|
|
Loading…
Reference in a new issue