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 - +