From a5788d71fa7ac1cd6e5ed9a98f95f6ca1bddcbb4 Mon Sep 17 00:00:00 2001 From: Alex Shpak Date: Sun, 27 Oct 2019 11:58:58 +0100 Subject: [PATCH] #80, Add search index configuration --- README.md | 5 +++ assets/search-data.js | 47 ++++++++++------------ assets/search.js | 14 +++---- exampleSite/config.toml | 8 +++- exampleSite/config.yaml | 9 ++++- exampleSite/content/docs/example/search.md | 17 -------- static/flexsearch.light.js | 20 --------- static/flexsearch.min.js | 42 +++++++++++++++++++ 8 files changed, 88 insertions(+), 74 deletions(-) delete mode 100644 exampleSite/content/docs/example/search.md delete mode 100644 static/flexsearch.light.js create mode 100644 static/flexsearch.min.js diff --git a/README.md b/README.md index b87b1c0..30d4274 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,11 @@ disableKinds = ['taxonomy', 'taxonomyTerm'] # (Optional, default true) Enables search function with lunr.js, # Index is built on fly, therefore it might slowdown your website. BookSearch = true + + # (Optional, default none) Search config for flexsearch index. + # Read documentation at https://github.com/nextapps-de/flexsearch#usage. By default 'balance' preset is used. + # This option allows to set specific configs for languages support. Must be valid JavaScript object. + BookSearchConfig = '{ cache: true }' ``` ### Page Configuration diff --git a/assets/search-data.js b/assets/search-data.js index 6848438..a3fd51b 100644 --- a/assets/search-data.js +++ b/assets/search-data.js @@ -1,30 +1,25 @@ +'use strict'; + (function() { - const pages = [ - {{ range $index, $page := .Site.Pages }} - {{- if $index -}},{{- end }} - { - 'idx': {{ $index }}, - 'href': '{{ $page.RelPermalink }}', - 'title': {{ (partial "docs/title" $page) | jsonify }}, - 'content': {{ $page.Plain | jsonify }} - } - {{- end -}} - ]; + const indexCfg = {{ with .Site.Params.BookSearchConfig }} + {{ . }} + {{ end }}; - var index = new FlexSearch({ - cache: true, - encode: 'balance', - /* tokenize: function(str) { - return str.replace(/[\x00-\x7F]/g, ' ').split(''); - } */ + indexCfg.doc = { + id: 'id', + field: ['title', 'content'], + store: ['title', 'href'], + }; + + const index = FlexSearch.create('balance', indexCfg); + window.bookSearchIndex = index; + + {{ range $index, $page := .Site.Pages }} + index.add({ + 'id': {{ $index }}, + 'href': '{{ $page.RelPermalink }}', + 'title': {{ (partial "docs/title" $page) | jsonify }}, + 'content': {{ $page.Plain | jsonify }} }); - - pages.forEach(function(page, x) { - index.add(x, pages[x].content); - }) - - window.bookSearch = { - pages: pages, - index: index, - } + {{- end -}} })(); diff --git a/assets/search.js b/assets/search.js index b9cbb11..89302b1 100644 --- a/assets/search.js +++ b/assets/search.js @@ -1,5 +1,6 @@ -{{- $searchData := resources.Get "search-data.js" | resources.ExecuteAsTemplate "search-data.js" . | resources.Minify | resources.Fingerprint }} +'use strict'; +{{- $searchData := resources.Get "search-data.js" | resources.ExecuteAsTemplate "search-data.js" . | resources.Minify | resources.Fingerprint }} (function() { const input = document.querySelector('#book-search-input'); const results = document.querySelector('#book-search-results'); @@ -8,10 +9,10 @@ input.addEventListener('keyup', search); function init() { - input.removeEventListener('focus', init); //init once + input.removeEventListener('focus', init); // init once input.required = true; - loadScript('{{ "flexsearch.light.js" | relURL }}'); + loadScript('{{ "flexsearch.min.js" | relURL }}'); loadScript('{{ $searchData.RelPermalink }}', function() { input.required = false; search(); @@ -27,9 +28,8 @@ return; } - let searchHits = window.bookSearch.index.search(input.value, 10); - searchHits.forEach(function(hit) { - const page = window.bookSearch.pages[hit]; + const searchHits = window.bookSearchIndex.search(input.value, 10); + searchHits.forEach(function(page) { const li = document.createElement('li'), a = li.appendChild(document.createElement('a')); @@ -47,6 +47,6 @@ script.src = src; script.onload = callback; - document.head.append(script); + document.head.appendChild(script); } })(); diff --git a/exampleSite/config.toml b/exampleSite/config.toml index 32ab022..779c74f 100644 --- a/exampleSite/config.toml +++ b/exampleSite/config.toml @@ -1,10 +1,9 @@ -# hugo server --minify --themesDir ... --baseURL=http://0.0.0.0:1313/example +# hugo server --minify --themesDir ... --baseURL=http://0.0.0.0:1313/theme/hugo-book/ baseURL = 'https://example.com/' title = 'Hugo Book' theme = 'hugo-book' disableKinds = ['taxonomy', 'taxonomyTerm'] -# themesDir = '../..' # Book configuration disablePathToLower = true @@ -49,3 +48,8 @@ pygmentsCodeFences = true # (Optional, default true) Enables search function with lunr.js, # Index is built on fly, therefore it might slowdown your website. BookSearch = true + + # (Optional, default none) Search config for flexsearch index. + # Read documentation at https://github.com/nextapps-de/flexsearch#usage. By default 'balance' preset is used. + # This option allows to set specific configs for languages support. Must be valid JavaScript object. + BookSearchConfig = '{ cache: true }' diff --git a/exampleSite/config.yaml b/exampleSite/config.yaml index 8719742..36b3b5d 100644 --- a/exampleSite/config.yaml +++ b/exampleSite/config.yaml @@ -1,10 +1,9 @@ -# hugo server --minify --themesDir ... --baseURL=http://0.0.0.0:1313/example +# hugo server --minify --themesDir ... --baseURL=http://0.0.0.0:1313/theme/hugo-book/ baseURL: https://example.com/ title: Hugo Book theme: hugo-book disableKinds: ['taxonomy', 'taxonomyTerm'] -# themesDir: ../.. # Book configuration disablePathToLower: true @@ -49,3 +48,9 @@ params: # (Optional, default true) Enables search function with lunr.js, # Index is built on fly, therefore it might slowdown your website. BookSearch: true + + # (Optional, default none) Search config for flexsearch index. + # Read documentation at https://github.com/nextapps-de/flexsearch#usage. By default 'balance' preset is used. + # This option allows to set specific configs for languages support. Must be valid JavaScript object. + BookSearchConfig: | + { cache: true } diff --git a/exampleSite/content/docs/example/search.md b/exampleSite/content/docs/example/search.md deleted file mode 100644 index 3e242df..0000000 --- a/exampleSite/content/docs/example/search.md +++ /dev/null @@ -1,17 +0,0 @@ ---- -title: 結任了出興自園更市標告之絕 -bookHidden: true ---- - -# 結任了出興自園更市標告之絕 -問希和企一我……開父天營歡母不黨信現人發。 - -加你區然沒一破?感花好千現學稱好?計現生,響時加過法素種際說國……陸國教快這展四冷論機人語力減社,路調好……臉水不國清司的態已及力如未三團身;大力看國叫晚線不式。校精到過,個亞有可放世來年和於後。 - -功細能色,色是任金言!學門他應全品原表便家人大就。 - -牛生美服食方未問始沒害被般不……響具線? - -而技爸技推立出少屋須?男才太西個輕明深應工、原而克說國快我檢為的單調請出消動教處負四無空、三電引,力二約說空表消字代!到人岸傳、著人野中活! - -去一作情離論才,再男反整。設如大點重主理個故……面目定辦我些!的我頭來裡品生有適西管以依越同究民國自情以助受人:要家的受好客童把叫的通,預把工那子電作,根品行消……高華然片內聞員、有是老道世心,皮急結告,商這同給……遠蘭裡:化委長教人再;再的她適認是國有評都的城一識小廣分方提爾現馬多,數曾突為備長竟重深知舉書校母印的兒部竟法由念,學代品道量百著別旅!參還理登你下府意生有公生將題必今的用能從父人見動北死作國! diff --git a/static/flexsearch.light.js b/static/flexsearch.light.js deleted file mode 100644 index 2b772fa..0000000 --- a/static/flexsearch.light.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - FlexSearch v0.6.30 - Copyright 2019 Nextapps GmbH - Author: Thomas Wilkerling - Released under the Apache 2.0 Licence - https://github.com/nextapps-de/flexsearch -*/ -'use strict';(function(n,C,D){let t;(t=D.define)&&t.amd?t([],function(){return C}):(t=D.modules)?t[n.toLowerCase()]=C:"object"===typeof exports?module.exports=C:D[n]=C})("FlexSearch",function(){function n(a,b){const c=b?b.id:a&&a.id;this.id=c||0===c?c:T++;this.init(a,b);D(this,"index",function(){return Object.keys(this.c)});D(this,"length",function(){return this.index.length})}function C(a,b){const c=a.length,g=H(b),d=[];for(let h=0,f=0;h=f&&(a=a[e-(d+.5>>0)],a=a[c]||(a[c]=[]),a[a.length]=g);return d}function M(a,b){if(a){const c=Object.keys(a);for(let g=0,d=c.length;ga?1:a?-1:0}function N(a,b,c){return a?{page:a,next:b?""+b:null,result:c}:c}function E(a){return"string"===typeof a}function H(a){return"function"===typeof a}function J(a){return"undefined"===typeof a}function P(a){const b=Array(a);for(let c=0;cl;x--)k=h.substring(l,x),I(p,g,k,a,G,e,r,w-1)}break;default:if(f=I(p,g,h,a,1,e,r,w-1),v&&1=r)for(f=g._ctx[h]||(g._ctx[h]=y()),h=this.f[h]||(this.f[h]=P(w-(r||0))),e=q-v,k=q+v+1,0>e&&(e=0),k>d&&(k=d);eA&&(e=0),x=e||0,z=x+b,z=d&&(this.u=this.g),this.u===this.g&&(this.cache&&this.j.set(c,this.o),this.F&&this.F(this.o)));return this}function S(a){const c=B();for(const b in a)if(a.hasOwnProperty(b)){const d=a[b];F(d)?c[b]=d.slice(0):G(d)?c[b]=S(d):c[b]=d}return c}function W(a,c){const b=a.length,d=O(c),e=[];for(let f=0,h=0;f=h&&(a=a[g-(e+.5>>0)],a=a[b]||(a[b]=[]), +a[a.length]=d);return e}function ba(a,c){if(a){const b=Object.keys(a);for(let d=0,e=b.length;da?1:a?-1:0}function pa(a,c){a=a[M];c=c[M];return ac?1:0}function oa(a,c){const b=M.length;for(let d=0;dc?1:0}function T(a,c,b){return a?{page:a,next:c?""+c:null,result:b}:b}function ha(a,c,b,d,e,f,h){let g,k=[];if(!0===b){b="0";var l=""}else l=b&&b.split(":");const p=a.length;if(1h&&(l=0),l=l||0,g=l+c,g=this.m.length&&(this.C=0),this.m[this.C].postMessage({add:!0,id:a, +content:c}),this.c[f]=""+this.C,b&&b(),this;if(!e){if(this.async&&"function"!==typeof importScripts){let t=this;f=new Promise(function(v){setTimeout(function(){t.add(a,c,null,d,!0);t=null;v()})});if(b)f.then(b);else return f;return this}if(b)return this.add(a,c,null,d,!0),b(),this}c=this.encode(c);if(!c.length)return this;b=this.f;e=O(b)?b(c):c.split(this.split);this.filter&&(e=W(e,this.filter));const n=B();n._ctx=B();const m=e.length,u=this.threshold,q=this.depth,A=this.b,z=this.i,y=this.D;for(let t= +0;tp;x--)l=h.substring(p,x),V(z,n,l,a,v,k,u,A-1)}break;default:if(g=V(z,n,h,a,1,k,u,A-1),q&&1=u)for(g=n._ctx[h]||(n._ctx[h]=B()),h=this.h[h]||(this.h[h]=ia(A-(u||0))),k=t-q,l=t+q+1,0>k&&(k=0),l> +m&&(l=m);kh;d--)e=g[d-1],g[d]=e,f[e]=d;g[h]=c;f[c]=h}}}return b};return a}();return w}(function(){const K={},R="undefined"!==typeof Blob&& +"undefined"!==typeof URL&&URL.createObjectURL;return function(w,L,S,W,P){S=R?URL.createObjectURL(new Blob(["("+S.toString()+")()"],{type:"text/javascript"})):w+".min.js";w+="-"+L;K[w]||(K[w]=[]);K[w][P]=new Worker(S);K[w][P].onmessage=W;return K[w][P]}}()),this);