fix: fetch search data as JSON rather than JS (#43)

Fixes #42
This commit is contained in:
Eric Druid 2021-01-05 16:40:02 +01:00 committed by GitHub
parent 34234c2342
commit d34fb0993e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 42 deletions

View file

@ -1,28 +0,0 @@
'use strict';
(function() {
const indexCfg = {{ with .Scratch.Get "geekdocSearchConfig" }}
{{ . | jsonify}};
{{ else }}
{};
{{ end }}
indexCfg.doc = {
id: 'id',
field: ['title', 'content'],
store: ['title', 'href', 'parent'],
};
const index = FlexSearch.create(indexCfg);
window.geekdocSearchIndex = index;
{{ range $index, $page := .Site.Pages }}
index.add({
'id': {{ $index }},
'href': '{{ $page.RelPermalink }}',
'title': {{ (partial "title" $page) | jsonify }},
'parent': {{ with $page.Parent }}{{ (partial "title" .) | jsonify }}{{ else }}''{{ end }},
'content': {{ $page.Plain | jsonify }}
});
{{- end -}}
})();

View file

@ -1,29 +1,38 @@
'use strict'; 'use strict';
{{ $searchDataFile := printf "js/%s.search-data.js" .Language.Lang }} {{ $searchDataFile := printf "%s.search-data.json" .Language.Lang }}
{{ $searchData := resources.Get "js/search-data.js" | resources.ExecuteAsTemplate $searchDataFile . | resources.Minify | resources.Fingerprint }} {{ $searchData := resources.Get "search-data.json" | resources.ExecuteAsTemplate $searchDataFile . | resources.Minify }}
(function() { (function() {
const input = document.querySelector('#gdoc-search-input'); const input = document.querySelector('#gdoc-search-input');
const results = document.querySelector('#gdoc-search-results'); const results = document.querySelector('#gdoc-search-results');
let showParent = false let showParent = {{ if .Site.Params.GeekdocSearchShowParent }}true{{ else }}false{{ end }}
{{ if .Site.Params.GeekdocSearchShowParent }}
showParent = true
{{ end }}
input.addEventListener('focus', init); input.addEventListener('focus', init);
input.addEventListener('keyup', search); input.addEventListener('keyup', search);
function init() { function init() {
input.removeEventListener('focus', init); // init once input.removeEventListener('focus', init); // init once
input.required = true;
loadScript('{{ index .Site.Data.assets "js/groupBy.min.js" | relURL }}'); loadScript('{{ index .Site.Data.assets "js/groupBy.min.js" | relURL }}');
loadScript('{{ index .Site.Data.assets "js/flexsearch.min.js" | relURL }}'); loadScript('{{ index .Site.Data.assets "js/flexsearch.min.js" | relURL }}', function() {
loadScript('{{ $searchData.RelPermalink }}', function() { const indexCfg = {{ with .Scratch.Get "geekdocSearchConfig" }}{{ . | jsonify}}{{ else }}{}{{ end }};
input.required = false; const dataUrl = "{{ $searchData.RelPermalink }}"
search();
indexCfg.doc = {
id: 'id',
field: ['title', 'content'],
store: ['title', 'href', 'parent'],
};
const index = FlexSearch.create(indexCfg);
window.geekdocSearchIndex = index;
getJson(dataUrl, function(data) {
data.forEach(obj => {
window.geekdocSearchIndex.add(obj);
});
});
}); });
} }
@ -43,13 +52,13 @@
results.classList.add("has-hits"); results.classList.add("has-hits");
if (showParent) { if (showParent === true) {
searchHits = groupBy(searchHits, hit => hit.parent); searchHits = groupBy(searchHits, hit => hit.parent);
} }
const items = []; const items = [];
if (showParent) { if (showParent === true) {
for (const section in searchHits) { for (const section in searchHits) {
const item = document.createElement('li'), const item = document.createElement('li'),
title = item.appendChild(document.createElement('span')), title = item.appendChild(document.createElement('span')),
@ -107,6 +116,23 @@
return items; return items;
} }
function fetchErrors(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}
function getJson(src, callback) {
fetch(src)
.then(fetchErrors)
.then(response => response.json())
.then(json => callback(json))
.catch(function(error) {
console.log(error);
});
}
function loadScript(src, callback) { function loadScript(src, callback) {
let script = document.createElement('script'); let script = document.createElement('script');
script.defer = true; script.defer = true;

12
assets/search-data.json Normal file
View file

@ -0,0 +1,12 @@
[
{{ range $index, $page := .Site.Pages }}
{{ if ne $index 0 }},{{ end }}
{
"id": {{ $index }},
"href": "{{ $page.RelPermalink }}",
"title": {{ (partial "title" $page) | jsonify }},
"parent": {{ with $page.Parent }}{{ (partial "title" .) | jsonify }}{{ else }}""{{ end }},
"content": {{ $page.Plain | jsonify }}
}
{{ end }}
]