Implement search, loading on demand

This commit is contained in:
Alex Shpak
2019-07-16 14:34:02 +02:00
parent dda0a0eab1
commit 1f3addcca5
16 changed files with 119 additions and 69 deletions
+2 -4
View File
@@ -14,9 +14,6 @@ enableGitInfo = true
# pygmentsStyle = 'monokailight'
pygmentsCodeFences = true
[outputs]
home = ["HTML", "JSON"]
[params]
# (Optional, default 6) Set how many table of contents levels to be showed on page.
# Use false to hide ToC, note that 0 will default to 6 (https://gohugo.io/functions/default/)
@@ -48,5 +45,6 @@ pygmentsCodeFences = true
# - In blog posts
BookDateFormat = 'Jan 2, 2006'
# (Optional, default true) Enables or disables search function with lunr.js
# (Optional, default true) Enables search function with lunr.js,
# Index is built on fly, therefore it might slowdown your website.
BookSearch = true
+4
View File
@@ -44,3 +44,7 @@ params:
# - In git information
# - In blog posts
BookDateFormat: 'Jan 2, 2006'
# (Optional, default true) Enables search function with lunr.js,
# Index is built on fly, therefore it might slowdown your website.
BookSearch: true
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
{"Target":"search-data.min.cab714d6cdc1fd81a6e2d8e454a03c6b8c74b4680118a20d06f2f4cc11365cdf.js","MediaType":"application/javascript","Data":{"Integrity":"sha256-yrcU1s3B/YGm4tjkVKA8a4x0tGgBGKINBvL0zBE2XN8="}}
@@ -1,6 +1,5 @@
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);});}});
(function(){const input=document.querySelector("#book-search-input");const results=document.querySelector("#book-search-results");const dummy=document.querySelector("#book-search-dummy");input.addEventListener("focus",init);function init(){loadScript("/example/search-data.min.cab714d6cdc1fd81a6e2d8e454a03c6b8c74b4680118a20d06f2f4cc11365cdf.js",function(){input.disabled=false;input.addEventListener("keyup",search);search();});input.removeEventListener("focus",init);}
function search(){while(results.firstChild){results.removeChild(results.firstChild);}
if(input.value){const hits=window.bookSearch.idx.search(`${input.value}*`);hits.slice(0,10).forEach(function(hit){const page=window.bookSearch.pages[hit.ref];const li=dummy.querySelector("li").cloneNode(true),a=li.querySelector("a");a.href=page.href;a.textContent=page.title;results.appendChild(li);});}}
function newLi(href,title){return li;}
function loadScript(src,callback){const script=document.createElement("script");script.defer=true;script.src=src;script.onload=callback;document.head.append(script);}})();
@@ -1 +1 @@
{"Target":"search.min.f37565c4baa28364f7b8e1e53c35bdb0ab92f2215165bd3f083f3f4f1ae78c71.js","MediaType":"application/javascript","Data":{"Integrity":"sha256-83VlxLqig2T3uOHlPDW9sKuS8iFRZb0/CD8/TxrnjHE="}}
{"Target":"search.min.5b3c1973db27e97251bca8f705d321f8de35004cd65d2aaf71ca544590ce6b9d.js","MediaType":"application/javascript","Data":{"Integrity":"sha256-WzwZc9sn6XJRvKj3BdMh+N41AEzWXSqvccpURZDOa50="}}
File diff suppressed because one or more lines are too long
@@ -1 +1 @@
{"Target":"book.min.60422b170f7beee5a981f3288523ee2bf275a0b48eba3a8983a3736189b9027f.css","MediaType":"text/css","Data":{"Integrity":"sha256-YEIrFw977uWpgfMohSPuK/J1oLSOujqJg6NzYYm5An8="}}
{"Target":"book.min.d43e03103ef0f496eb5dfc0fb35ee914237fdcac29cb9b8e2764dac6fdc45ba1.css","MediaType":"text/css","Data":{"Integrity":"sha256-1D4DED7w9JbrXfwPs17pFCN/3Kwpy5uOJ2Taxv3EW6E="}}