feat: update to flexsearch v0.7.1

This commit is contained in:
Henk Verlinde 2021-06-25 13:29:22 +02:00
parent 93f7598f87
commit 1589916e58
5 changed files with 52 additions and 33 deletions

View File

@ -64,37 +64,54 @@ Source:
(function(){ (function(){
var index = new FlexSearch({ var index = new FlexSearch.Document({
preset: 'score', tokenize: "forward",
cache: true, cache: 100,
doc: { document: {
id: 'id', id: 'id',
field: [ store: [
'title', "href", "title", "description"
'description', ],
'content', index: ["title", "description", "content"]
], }
store: [
'href',
'title',
'description',
],
},
}); });
// Not yet supported: https://github.com/nextapps-de/flexsearch#complex-documents
/*
var docs = [ var docs = [
{{ range $index, $page := (where .Site.Pages "Section" "docs") -}} {{ range $index, $page := (where .Site.Pages "Section" "docs") -}}
{ {
id: {{ $index }}, id: {{ $index }},
href: "{{ .RelPermalink | relURL }}", href: "{{ .Permalink }}",
title: {{ .Title | jsonify }}, title: {{ .Title | jsonify }},
description: {{ .Params.description | jsonify }}, description: {{ .Params.description | jsonify }},
content: {{ .Content | jsonify }} content: {{ .Content | jsonify }}
}, },
{{ end -}} {{ end -}}
]; ];
*/
index.add(docs); // https://discourse.gohugo.io/t/range-length-or-last-element/3803/2
{{ $list := (where .Site.Pages "Section" "docs") -}}
{{ $len := (len $list) -}}
index.add(
{{ range $index, $element := $list -}}
{
id: {{ $index }},
href: "{{ .Permalink }}",
title: {{ .Title | jsonify }},
description: {{ .Params.description | jsonify }},
content: {{ .Content | jsonify }}
})
{{ if ne (add $index 1) $len -}}
.add(
{{ end -}}
{{ end -}}
;
userinput.addEventListener('input', show_results, true); userinput.addEventListener('input', show_results, true);
suggestions.addEventListener('click', accept_suggestion, true); suggestions.addEventListener('click', accept_suggestion, true);
@ -102,13 +119,13 @@ Source:
function show_results(){ function show_results(){
var value = this.value; var value = this.value;
var results = index.search(value, 5); var results = index.search(value, { limit: 5, index: ["content"], enrich: true });
var entry, childs = suggestions.childNodes; var entry, childs = suggestions.childNodes;
var i = 0, len = results.length; var i = 0, len = results.length;
suggestions.classList.remove('d-none'); suggestions.classList.remove('d-none');
results.forEach(function(page) { results.forEach(function(results) {
entry = document.createElement('div'); entry = document.createElement('div');
@ -118,9 +135,11 @@ Source:
t = entry.querySelector('span:first-child'), t = entry.querySelector('span:first-child'),
d = entry.querySelector('span:nth-child(2)'); d = entry.querySelector('span:nth-child(2)');
a.href = page.href; // console.log(results);
t.textContent = page.title;
d.textContent = page.description; a.href = results.result[i].doc.href;
t.textContent = results.result[i].doc.title;
d.textContent = results.result[i].doc.description;
suggestions.appendChild(entry); suggestions.appendChild(entry);

View File

@ -7,7 +7,7 @@
<div class="col-lg-9 col-xl-8 text-center"> <div class="col-lg-9 col-xl-8 text-center">
<p class="lead">{{ .Params.lead | safeHTML }}</p> <p class="lead">{{ .Params.lead | safeHTML }}</p>
<a class="btn btn-primary btn-lg px-4 mb-2" href="{{ "docs/prologue/introduction/" | relURL }}" role="button">Get started</a> <a class="btn btn-primary btn-lg px-4 mb-2" href="{{ "docs/prologue/introduction/" | relURL }}" role="button">Get started</a>
<p class="meta">Open-source MIT Licensed. <a href="https://github.com/h-enk/doks">GitHub v{{ .Site.Data.doks.version }}</a></p> <p class="meta">Open-source MIT Licensed. <a href="https://github.com/h-enk/doks">GitHub v{{ $data := getJSON "https://raw.githubusercontent.com/h-enk/doks/master/package.json" }}{{ $data.version }}</a></p>
</div> </div>
</div> </div>
</section> </section>

View File

@ -33,7 +33,7 @@
{{ end -}} {{ end -}}
{{ if .Site.Params.options.flexSearch -}} {{ if .Site.Params.options.flexSearch -}}
{{ $flexSearch := resources.Get "js/vendor/flexsearch/dist/flexsearch.min.js" -}} {{ $flexSearch := resources.Get "js/vendor/flexsearch/dist/flexsearch.bundle.js" -}}
{{ $slice = $slice | append $flexSearch -}} {{ $slice = $slice | append $flexSearch -}}
{{ end -}} {{ end -}}

14
package-lock.json generated
View File

@ -20,7 +20,7 @@
"clipboard": "^2.0", "clipboard": "^2.0",
"eslint": "^7.29", "eslint": "^7.29",
"exec-bin": "^1.0.0", "exec-bin": "^1.0.0",
"flexsearch": "^0.6", "flexsearch": "^0.7",
"highlight.js": "^11.0", "highlight.js": "^11.0",
"hugo-installer": "^3.0.1", "hugo-installer": "^3.0.1",
"instant.page": "^5.1", "instant.page": "^5.1",
@ -4465,9 +4465,9 @@
"dev": true "dev": true
}, },
"node_modules/flexsearch": { "node_modules/flexsearch": {
"version": "0.6.32", "version": "0.7.1",
"resolved": "https://registry.npmjs.org/flexsearch/-/flexsearch-0.6.32.tgz", "resolved": "https://registry.npmjs.org/flexsearch/-/flexsearch-0.7.1.tgz",
"integrity": "sha512-EF1BWkhwoeLtbIlDbY/vDSLBen/E5l/f1Vg7iX5CDymQCamcx1vhlc3tIZxIDplPjgi0jhG37c67idFbjg+v+Q==", "integrity": "sha512-+nuTYgqWMA8JawcReboPniMuvy0ClxqU66oMh4YzNiQQrbzH6VdJZMF/kzAEPnpL64FiZfbGRixv+cz5YponhA==",
"dev": true "dev": true
}, },
"node_modules/for-in": { "node_modules/for-in": {
@ -13484,9 +13484,9 @@
"dev": true "dev": true
}, },
"flexsearch": { "flexsearch": {
"version": "0.6.32", "version": "0.7.1",
"resolved": "https://registry.npmjs.org/flexsearch/-/flexsearch-0.6.32.tgz", "resolved": "https://registry.npmjs.org/flexsearch/-/flexsearch-0.7.1.tgz",
"integrity": "sha512-EF1BWkhwoeLtbIlDbY/vDSLBen/E5l/f1Vg7iX5CDymQCamcx1vhlc3tIZxIDplPjgi0jhG37c67idFbjg+v+Q==", "integrity": "sha512-+nuTYgqWMA8JawcReboPniMuvy0ClxqU66oMh4YzNiQQrbzH6VdJZMF/kzAEPnpL64FiZfbGRixv+cz5YponhA==",
"dev": true "dev": true
}, },
"for-in": { "for-in": {

View File

@ -45,7 +45,7 @@
"eslint": "^7.29", "eslint": "^7.29",
"exec-bin": "^1.0.0", "exec-bin": "^1.0.0",
"hugo-installer": "^3.0.1", "hugo-installer": "^3.0.1",
"flexsearch": "^0.6", "flexsearch": "^0.7",
"highlight.js": "^11.0", "highlight.js": "^11.0",
"instant.page": "^5.1", "instant.page": "^5.1",
"katex": "^0.13", "katex": "^0.13",