hugo-theme-relearn/js/search.min.js

1 line
5.2 KiB
JavaScript
Raw Normal View History

window.relearn=window.relearn||{},window.relearn.runInitialSearch=function(){if(window.relearn.isSearchInit&&window.relearn.isLunrInit){var t,e=document.querySelector("#R-search-by-detail");if(!e)return;t=e.value,searchDetail(t)}};function initLunrIndex(e){if(!window.lunr)return;pagesIndex=e,lunrIndex=lunr(function(){this.use(lunr.multiLanguage.apply(null,window.relearn.contentLangs)),this.ref("index"),this.field("title",{boost:15}),this.field("tags",{boost:10}),this.field("content",{boost:5}),this.pipeline.remove(lunr.stemmer),this.searchPipeline.remove(lunr.stemmer),pagesIndex.forEach(function(e,t){e.index=t,this.add(e)},this)}),window.relearn.isLunrInit=!0,window.relearn.runInitialSearch()}function triggerSearch(){var e,t,n,o,s=document.querySelector("#R-search-by-detail");if(!s)return;n=s.value,searchDetail(n),t=new URL(window.location),o=t.searchParams.get("search-by"),n!=o&&(e=window.history.state||{},e=Object.assign({},typeof e=="object"?e:{}),t.searchParams.set("search-by",n),e.search=t.toString(),e.contentScrollTop=+elc.scrollTop,window.history.pushState(e,"",t))}window.addEventListener("popstate",function(e){if(e.state){var n,s,o,t=window.history.state||{},t=Object.assign({},typeof t=="object"?t:{});t.search&&(n=new URL(t.search),n.searchParams.has("search-by")&&(s=n.searchParams.get("search-by"),o=document.querySelectorAll("input.search-by"),o.forEach(function(e){e.value=s;var t=document.createEvent("Event");t.initEvent("input",!1,!1),e.dispatchEvent(t)}),searchDetail(s)))}});var lunrIndex,pagesIndex,input=document.querySelector("#R-search-by-detail");input&&input.addEventListener("keydown",function(e){e.key=="Escape"&&setTimeout(function(){input.focus()},0)});function initLunrJs(){if(window.relearn.index_js_url){var e=document.createElement("script");e.src=window.relearn.index_js_url,e.setAttribute("async",""),e.onload=function(){initLunrIndex(relearn_searchindex)},e.onerror=function(){console.error("Error getting Hugo index file")},document.head.appendChild(e)}}function search(e){if(!window.lunr)return[];e=e.replace(/[*:^~+-]/g," ");var t=lunr.tokenizer(e).reduce(function(e,t){return e.concat(searchPatterns(t.str))},[]).join(" ");return!t||!lunrIndex?[]:lunrIndex.search(t).map(function(e){return{index:e.ref,matches:Object.keys(e.matchData.metadata)}})}function searchPatterns(e){var t=[{len:-1,typos:1},{len:60,typos:2},{len:40,typos:3},{len:20,typos:4},{len:16,typos:3},{len:12,typos:2},{len:8,typos:1},{len:4,typos:0}];return[e+"^100",e+"*^10","*"+e+"^10",e+"~"+t.reduce(function(t,n){return e.length<n.len?n:t}).typos+"^1"]}function resolvePlaceholders(e,t){var t=t||[];return e.replace(/{([0-9]+)}/g,function(e,n){return typeof t[n]=="undefined"?e:t[n]})}function searchDetail(e){var t,n,o=document.querySelector("#R-searchresults"),s=document.querySelector(".searchhint");if(s.innerText="",o.textContent="",n=search(e),n.length?(s.innerText=resolvePlaceholders(window.T_N_results_found,[e,n.length]),n.forEach(function(t){var i,a,r,s=pagesIndex[t.index],l=10,d="(?:\\S+ +){0,"+l+"}\\S*\\b(?:"+t.matches.map(function(e){return e.replace(/\W/g,"\\$&")}).join("|")+")\\b\\S*(?: +\\S+){0,"+l+"}",c=s.content.match(new RegExp(d,"i")),n=document.createElement("a");n.className="autocomplete-suggestion",n.setAttribute("data-term",e),n.setAttribute("data-title",s.title),n.setAttribute("href",window.relearn.relBaseUri+s.uri),n.setAttribute("data-context",c),i=document.createElement("div"),i.className="title",i.innerText="» "+s.title,n.appendChild(i),a=document.createElement("div"),a.className="breadcrumbs",a.innerText=s.breadcrumb||"",n.appendChild(a),c&&(r=document.createElement("div"),r.className="context",r.innerText=c||"",n.appendChild(r)),o.appendChild(n)}),window.relearn.markSearch()):e.length&&(s.innerText=resolvePlaceholders(window.T_No_results_found,[e])),input.focus(),setTimeout(adjustContentWidth,0),t=window.history.state||{},t=Object.assign({},typeof t=="object"?t:{}),t.hasOwnProperty("contentScrollTop")){window.setTimeout(function(){elc.scrollTop=+t.contentScrollTop},10);return}}initLunrJs();function startSearch(){v