#80, Migrate to flexsearch

This commit is contained in:
Alex Shpak 2019-10-20 19:02:32 +02:00
parent 4596f08e32
commit b4307e7c09
4 changed files with 50 additions and 43 deletions

View file

@ -3,22 +3,28 @@
{{ range $index, $page := .Site.Pages }}
{{- if $index -}},{{- end }}
{
"idx": {{ $index }},
"href": "{{ $page.RelPermalink }}",
"title": {{ (partial "docs/title" $page) | jsonify }},
"content": {{ $page.Plain | jsonify }}
'idx': {{ $index }},
'href': '{{ $page.RelPermalink }}',
'title': {{ (partial "docs/title" $page) | jsonify }},
'content': {{ $page.Plain | jsonify }}
}
{{- end -}}
];
var index = new FlexSearch({
cache: true,
encode: 'balance',
/* tokenize: function(str) {
return str.replace(/[\x00-\x7F]/g, ' ').split('');
} */
});
pages.forEach(function(page, x) {
index.add(x, pages[x].content);
})
window.bookSearch = {
pages: pages,
idx: lunr(function() {
this.ref("idx");
this.field("title");
this.field("content");
pages.forEach(this.add, this);
}),
index: index,
}
})();

View file

@ -1,18 +1,18 @@
{{- $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");
const input = document.querySelector('#book-search-input');
const results = document.querySelector('#book-search-results');
input.addEventListener("focus", init);
input.addEventListener("keyup", search);
input.addEventListener('focus', init);
input.addEventListener('keyup', search);
function init() {
input.removeEventListener("focus", init); //init once
input.removeEventListener('focus', init); //init once
input.required = true;
loadScript("{{ "lunr.min.js" | relURL }}");
loadScript("{{ $searchData.RelPermalink }}", function() {
loadScript('{{ "flexsearch.light.js" | relURL }}');
loadScript('{{ $searchData.RelPermalink }}', function() {
input.required = false;
search();
});
@ -27,24 +27,11 @@
return;
}
const terms = lunr.tokenizer(input.value);
const searchHits = window.bookSearch.idx.query(function(query) {
query.term(terms, {
boost: 100
});
query.term(terms, {
boost: 10,
wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING
});
query.term(terms, {
editDistance: 2
});
});
searchHits.slice(0, 10).forEach(function(hit) {
const page = window.bookSearch.pages[hit.ref];
const li = document.createElement("li"),
a = li.appendChild(document.createElement("a"));
let searchHits = window.bookSearch.index.search(input.value, 10);
searchHits.forEach(function(hit) {
const page = window.bookSearch.pages[hit];
const li = document.createElement('li'),
a = li.appendChild(document.createElement('a'));
a.href = page.href;
a.textContent = page.title;
@ -54,7 +41,7 @@
}
function loadScript(src, callback) {
const script = document.createElement("script");
const script = document.createElement('script');
script.defer = true;
script.async = false;
script.src = src;

View file

@ -0,0 +1,20 @@
/*
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<c;h++){const e=a[h];if(g&&b(e)||!g&&!b[e])d[f++]=e}return d}
function D(a,b,c){Object.defineProperty(a,b,{get:c})}function t(a){return new RegExp(a,"g")}function K(a,b){for(let c=0;c<b.length;c+=2)a=a.replace(b[c],b[c+1]);return a}function I(a,b,c,g,d,h,f,e){if(b[c])return b[c];d=d?(e-(f||e/1.5))*h+(f||e/1.5)*d:h;b[c]=d;d>=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;g<d;g++){const h=c[g],f=a[h];if(f)for(let e=0,k=f.length;e<k;e++)if(f[e]===b){1===k?delete a[h]:f.splice(e,
1);break}else"object"===typeof f[e]&&M(f[e],b)}}}function U(a,b){a=a.length-b.length;return 0>a?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;c<a;c++)b[c]=y();return b}function y(){return Object.create(null)}const B={encode:"icase",b:"forward",split:/\W+/,cache:!1,async:!1,m:!1,i:!1,l:!1,a:9,threshold:0,depth:0},
O=[];let T=0;const Q={},R={};n.create=function(a,b){return new n(a,b)};n.registerMatcher=function(a){for(const b in a)a.hasOwnProperty(b)&&O.push(t(b),a[b]);return this};n.registerEncoder=function(a,b){F[a]=b.bind(F);return this};n.registerLanguage=function(a,b){Q[a]=b.filter;R[a]=b.stemmer;return this};n.encode=function(a,b){return F[a](b)};n.prototype.init=function(a,b){this.j=[];if(b){var c=b.preset;a=b}else a||(a=B),c=a.preset;b={};this.b=a.tokenize||b.b||this.b||B.b;this.split=J(c=a.split)?this.split||
B.split:E(c)?t(c):c;this.i=a.rtl||this.i||B.i;this.threshold=J(c=a.threshold)?b.threshold||this.threshold||B.threshold:c;this.a=J(c=a.resolution)?c=b.a||this.a||B.a:c;c<=this.threshold&&(this.a=this.threshold+1);this.depth="strict"!==this.b||J(c=a.depth)?b.depth||this.depth||B.depth:c;this.g=(c=J(c=a.encode)?b.encode||B.encode:c)&&F[c]&&F[c].bind(F)||(H(c)?c:this.g||!1);(c=a.matcher)&&this.addMatcher(c);if(c=(b=a.lang)||a.filter){E(c)&&(c=Q[c]);if(c.constructor===Array){{var g=this.g;const h=y();
for(let f=0;f<c.length;f++){const e=g?g(c[f]):c[f];h[e]=1}c=h}}this.filter=c}if(c=b||a.stemmer){var d;a=E(c)?R[c]:c;b=this.g;c=[];for(d in a)a.hasOwnProperty(d)&&(g=b?b(d):d,c.push(t(g+"($|\\W)"),b?b(a[d]):a[d]));this.stemmer=d=c}this.h=P(this.a-(this.threshold||0));this.f=y();this.c=y();return this};n.prototype.encode=function(a){a&&(O.length&&(a=K(a,O)),this.j.length&&(a=K(a,this.j)),this.g&&(a=this.g(a)),this.stemmer&&(a=K(a,this.stemmer)));return a};n.prototype.addMatcher=function(a){const b=
this.j;for(const c in a)a.hasOwnProperty(c)&&b.push(t(c),a[c]);return this};n.prototype.add=function(a,b,c,g,d){if(b&&E(b)&&(a||0===a)){const u="@"+a;if(this.c[u]&&!g)return this.update(a,b);if(!d&&c)return this.add(a,b,null,g,!0),c(),this;b=this.encode(b);if(!b.length)return this;c=this.b;b=H(c)?c(b):b.split(this.split);this.filter&&(b=C(b,this.filter));g=y();g._ctx=y();d=b.length;const r=this.threshold,v=this.depth,w=this.a,p=this.h,m=this.i;for(let q=0;q<d;q++){var h=b[q];if(h){var f=h.length,
e=(m?q+1:d-q)/d,k="";switch(c){case "reverse":case "both":for(var l=f;--l;)k=h[l]+k,I(p,g,k,a,m?1:(f-l)/f,e,r,w-1);k="";case "forward":for(l=0;l<f;l++)k+=h[l],I(p,g,k,a,m?(l+1)/f:1,e,r,w-1);break;case "full":for(l=0;l<f;l++){const G=(m?l+1:f-l)/f;for(let x=f;x>l;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<d&&f>=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);e<k;e++)e!==q&&I(h,f,b[e],a,0,w-(e<
q?q-e:e-q),r,w-1)}}}this.c[u]=1}return this};n.prototype.update=function(a,b,c){this.c["@"+a]&&E(b)&&(this.remove(a),this.add(a,b,c,!0));return this};n.prototype.remove=function(a,b,c){const g="@"+a;if(this.c[g]){if(!c&&b)return this.remove(a,null,!0),b(),this;for(b=0;b<this.a-(this.threshold||0);b++)M(this.h[b],a);this.depth&&M(this.f,a);delete this.c[g]}return this};n.prototype.search=function(a,b,c,g){b&&H(b)?(c=b,b=1E3):b||0===b||(b=1E3);var d=[],h=a;if("object"===typeof a){var f=!1;b=a.limit;
var e=a.threshold;var k=!1;a=a.query}e||(e=this.threshold||0);if(!g&&c)return c(this.search(h,b,null,!0)),this;if(!a||!E(a))return d;h=this.encode(a);if(!h.length)return d;a=this.b;a=H(a)?a(h):h.split(this.split);this.filter&&(a=C(a,this.filter));c=a.length;g=!0;h=[];var l=y(),u,r=0;1<c&&(this.depth&&"strict"===this.b?u=!0:a.sort(U));var v;if(!u||(v=this.f))for(var w=this.a;r<c;r++){var p=a[r];if(p){if(u){if(!m)if(v[p]){var m=p;l[p]=1}else if(!k)return d;if(k&&r===c-1&&!h.length)u=!1,p=m||p,l[p]=
0;else if(!m)continue}if(!l[p]){var q=[],G=!1;let z=0;if(m=u?v[m]:this.h){let S;for(let L=0;L<w-e;L++)if(S=m[L]&&m[L][p])q[z++]=S,G=!0}if(G)m=p,h[h.length]=1<z?q.concat.apply([],q):q[0];else{g=!1;break}l[p]=1}}}else g=!1;if(g)a:{let z;d=[];!0===f?(f="0",e=""):e=f&&f.split(":");k=h.length;if(1<k){u=y();m=0;w=!0;g=0;var x;for(e&&(2===e.length?e=!1:e=x=parseInt(e[0],10));m<k;m++){l=m===k-1;v=h[m];c=v.length;if(!c){d=N(f,z,v);break a}if(w)if(A){r=A.length;for(a=0;a<r;a++)u["@"+A[a]]=1;var A=null;w=!1}else{A=
v;continue}p=!1;for(a=0;a<c;a++)if(r=v[a],q="@"+r,(G=u[q]||0)&&G===m){if(l){if(!x||--x<g)if(d[g++]=r,b&&g===b){d=N(f,g+(e||0),d);break a}}else u[q]=m+1;p=!0}if(!p)break}A&&(d=A)}else k&&(d=h[0],e&&(e=parseInt(e[0],10)));b&&(A=d.length,e&&e>A&&(e=0),x=e||0,z=x+b,z<A?d=d.slice(x,z):(z=0,x&&(d=d.slice(x))));d=N(f,z,d)}return d};n.prototype.clear=function(){return this.destroy().init()};n.prototype.destroy=function(){this.h=this.f=this.c=null;return this};(function(){const a=t("\\s+"),b=t("[^a-z0-9 ]"),
c=[t("[-/]")," ",b,"",a," "];return function(g){{g=K(g.toLowerCase(),c);let h="",f="";var d="";for(let e=0;e<g.length;e++){const k=g[e];if(k!==f)if(e&&"h"===k){if(d="a"===d||"e"===d||"i"===d||"o"===d||"u"===d||"y"===d,("a"===f||"e"===f||"i"===f||"o"===f||"u"===f||"y"===f)&&d||" "===f)h+=k}else h+=k;d=e===g.length-1?"":g[e+1];f=k}g=h}return g}})();const F={icase:function(a){return a.toLowerCase()}};return n}(!1),this);

6
static/lunr.min.js vendored

File diff suppressed because one or more lines are too long