From 1589916e5815e029038635f29dd7d2a983de35e7 Mon Sep 17 00:00:00 2001 From: Henk Verlinde Date: Fri, 25 Jun 2021 13:29:22 +0200 Subject: [PATCH] feat: update to flexsearch v0.7.1 --- assets/js/index.js | 65 ++++++++++++++-------- layouts/index.html | 2 +- layouts/partials/footer/script-footer.html | 2 +- package-lock.json | 14 ++--- package.json | 2 +- 5 files changed, 52 insertions(+), 33 deletions(-) diff --git a/assets/js/index.js b/assets/js/index.js index 8c87d5f..0df2fd2 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -64,37 +64,54 @@ Source: (function(){ - var index = new FlexSearch({ - preset: 'score', - cache: true, - doc: { - id: 'id', - field: [ - 'title', - 'description', - 'content', - ], - store: [ - 'href', - 'title', - 'description', - ], - }, + var index = new FlexSearch.Document({ + tokenize: "forward", + cache: 100, + document: { + id: 'id', + store: [ + "href", "title", "description" + ], + index: ["title", "description", "content"] + } }); + + // Not yet supported: https://github.com/nextapps-de/flexsearch#complex-documents + + /* var docs = [ {{ range $index, $page := (where .Site.Pages "Section" "docs") -}} { id: {{ $index }}, - href: "{{ .RelPermalink | relURL }}", + href: "{{ .Permalink }}", title: {{ .Title | jsonify }}, description: {{ .Params.description | jsonify }}, content: {{ .Content | jsonify }} }, {{ 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); suggestions.addEventListener('click', accept_suggestion, true); @@ -102,13 +119,13 @@ Source: function show_results(){ 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 i = 0, len = results.length; suggestions.classList.remove('d-none'); - results.forEach(function(page) { + results.forEach(function(results) { entry = document.createElement('div'); @@ -118,9 +135,11 @@ Source: t = entry.querySelector('span:first-child'), d = entry.querySelector('span:nth-child(2)'); - a.href = page.href; - t.textContent = page.title; - d.textContent = page.description; + // console.log(results); + + a.href = results.result[i].doc.href; + t.textContent = results.result[i].doc.title; + d.textContent = results.result[i].doc.description; suggestions.appendChild(entry); diff --git a/layouts/index.html b/layouts/index.html index 87f3caa..9f0c752 100644 --- a/layouts/index.html +++ b/layouts/index.html @@ -7,7 +7,7 @@

{{ .Params.lead | safeHTML }}

Get started -

Open-source MIT Licensed. GitHub v{{ .Site.Data.doks.version }}

+

Open-source MIT Licensed. GitHub v{{ $data := getJSON "https://raw.githubusercontent.com/h-enk/doks/master/package.json" }}{{ $data.version }}

diff --git a/layouts/partials/footer/script-footer.html b/layouts/partials/footer/script-footer.html index c7b893b..76cf0d5 100644 --- a/layouts/partials/footer/script-footer.html +++ b/layouts/partials/footer/script-footer.html @@ -33,7 +33,7 @@ {{ end -}} {{ 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 -}} {{ end -}} diff --git a/package-lock.json b/package-lock.json index 4e7bff0..50f64a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,7 +20,7 @@ "clipboard": "^2.0", "eslint": "^7.29", "exec-bin": "^1.0.0", - "flexsearch": "^0.6", + "flexsearch": "^0.7", "highlight.js": "^11.0", "hugo-installer": "^3.0.1", "instant.page": "^5.1", @@ -4465,9 +4465,9 @@ "dev": true }, "node_modules/flexsearch": { - "version": "0.6.32", - "resolved": "https://registry.npmjs.org/flexsearch/-/flexsearch-0.6.32.tgz", - "integrity": "sha512-EF1BWkhwoeLtbIlDbY/vDSLBen/E5l/f1Vg7iX5CDymQCamcx1vhlc3tIZxIDplPjgi0jhG37c67idFbjg+v+Q==", + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/flexsearch/-/flexsearch-0.7.1.tgz", + "integrity": "sha512-+nuTYgqWMA8JawcReboPniMuvy0ClxqU66oMh4YzNiQQrbzH6VdJZMF/kzAEPnpL64FiZfbGRixv+cz5YponhA==", "dev": true }, "node_modules/for-in": { @@ -13484,9 +13484,9 @@ "dev": true }, "flexsearch": { - "version": "0.6.32", - "resolved": "https://registry.npmjs.org/flexsearch/-/flexsearch-0.6.32.tgz", - "integrity": "sha512-EF1BWkhwoeLtbIlDbY/vDSLBen/E5l/f1Vg7iX5CDymQCamcx1vhlc3tIZxIDplPjgi0jhG37c67idFbjg+v+Q==", + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/flexsearch/-/flexsearch-0.7.1.tgz", + "integrity": "sha512-+nuTYgqWMA8JawcReboPniMuvy0ClxqU66oMh4YzNiQQrbzH6VdJZMF/kzAEPnpL64FiZfbGRixv+cz5YponhA==", "dev": true }, "for-in": { diff --git a/package.json b/package.json index f1c692e..f242ad1 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "eslint": "^7.29", "exec-bin": "^1.0.0", "hugo-installer": "^3.0.1", - "flexsearch": "^0.6", + "flexsearch": "^0.7", "highlight.js": "^11.0", "instant.page": "^5.1", "katex": "^0.13",