hugo-book-theme/exampleSite/resources/_gen/assets/js/search.js_db9e329433ffca0798ed12c88043b3b3.content
2019-08-06 11:33:42 +02:00

6 lines
No EOL
805 B
Text

addEventListener("load",function(){let input=document.querySelector("#book-search");let results=document.querySelector("#book-search-results");Promise.all([loadScript("/example/lunr.min.js"),loadScript("/example/index.json")]).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);});}});