Start work on search feature with lunr

This commit is contained in:
Alex Shpak
2019-07-15 18:25:21 +02:00
parent b199d72e5f
commit dda0a0eab1
26 changed files with 148 additions and 75 deletions
+41
View File
@@ -0,0 +1,41 @@
addEventListener("load", function() {
let input = document.querySelector("#book-search");
let results = document.querySelector("#book-search-results");
Promise.all([
loadScript("{{ "lunr.min.js" | relURL }}"),
loadScript("{{ "index.json" | relURL }}")
]).then(enableLunr);
function enableLunr() {
results.idx = lunr(function() {
this.ref('href')
this.field('title')
this.field('content')
window.lunrData.forEach(function (page) {
this.add(page)
}, this)
});
input.addEventListener("keyup", search);
}
function search() {
if (input.value) {
var hits = results.idx.search(`${input.value}*`);
results.innerHTML = JSON.stringify(hits);
} else {
results.innerHTML = '';
}
}
function loadScript(src) {
return new Promise(function(resolve, reject) {
let script = document.createElement('script');
script.src = src;
script.onload = () => resolve(script);
document.head.append(script);
});
}
});