search.min.2a154854f469aeb7a58abc0acf08e4219056bff7fc63c967931f0d6b8b611058.js 3.4 KB

123456
  1. let pagesIndex,searchIndex;const MAX_SUMMARY_LENGTH=30,SENTENCE_BOUNDARY_REGEX=/\b\.\s/gm,WORD_REGEX=/\b(\w*)[\W|\s|\b]?/gm;async function initSearch(){try{const e=await fetch("/index.json");pagesIndex=await e.json(),searchIndex=lunr(function(){this.field("title"),this.field("content"),this.ref("href"),pagesIndex.forEach(e=>this.add(e))})}catch(e){console.log(e)}console.log("Search index initialized");const t=new URLSearchParams(window.location.search),e=t.get("query");if(e){document.getElementById("search-input").setAttribute("value",e),console.log("search performed");let t=searchSite(e);renderSearchResults(e,t)}}initSearch();function searchSite(e){const t=e;e=getLunrSearchQuery(e);let n=getSearchResults(e);return n.length?n:e!==t?getSearchResults(t):[]}function getLunrSearchQuery(e){const t=e.split(" ");if(t.length===1)return e;e="";for(const n of t)e+=`+${n} `;return e.trim()}function getSearchResults(e){return searchIndex.search(e).flatMap(e=>{if(e.ref=="undefined")return[];let t=pagesIndex.filter(t=>t.href===e.ref)[0];return t.score=e.score,[t]})}function renderSearchResults(e,t){clearSearchResults(),updateSearchResults(e,t)}function clearSearchResults(){const e=document.querySelector("#search-results");for(;e.firstChild;)e.removeChild(e.firstChild)}function updateSearchResults(e,t){document.getElementById("results-query").innerHTML=e,document.querySelector("#search-results").innerHTML=t.map(t=>`
  2. <li class="search-result-item" data-score="${t.score.toFixed(2)}">
  3. <a href="${t.href}" class="search-result-page-title">${createTitleBlurb(e,t.title)}</a>
  4. <p>${createSearchResultBlurb(e,t.content)}</p>
  5. </li>
  6. `).join("");const n=document.querySelectorAll("#search-results li");document.getElementById("results-count").innerHTML=n.length,document.getElementById("results-count-text").innerHTML=n.length===1?"result":"results"}function createTitleBlurb(e,t){const n=new RegExp(createQueryStringRegex(e),"gmi");return t.replace(n,"<strong>$&</strong>")}function createSearchResultBlurb(e,t){const o=new RegExp(createQueryStringRegex(e),"gmi"),a=Array.from(t.matchAll(o),e=>e.index),s=Array.from(t.matchAll(SENTENCE_BOUNDARY_REGEX),e=>e.index);let n="",i=0;for(const e of a){if(e>i)for(let o=0;o<s.length;o++)if(s[o]>e){const a=o>0?s[o-1]+1:0,e=s[o];i=e,parsedSentence=t.slice(a,e).trim(),n+=`${parsedSentence} ... `;break}const o=tokenize(n),r=o.filter(e=>e.length>50);if(r.length>0&&(n=fixPageBreakers(n,r)),o.length>=MAX_SUMMARY_LENGTH)break}return ellipsize(n,MAX_SUMMARY_LENGTH).replace(o,"<strong>$&</strong>")}function createQueryStringRegex(e){const t=e.split(" ");if(t.length==1)return e;e="";for(const n of t)e+=`${n}|`;return e=e.slice(0,-1),`(${e})`}function tokenize(e){const t=Array.from(e.matchAll(WORD_REGEX),e=>e);return t.map(e=>({word:e[0],start:e.index,end:e.index+e[0].length,length:e[0].length}))}function fixPageBreakers(e,t){return t.forEach(t=>{const n=chunkify(t.word,20);e=e.replace(t.word,n)}),e}function chunkify(e,t){let s="",n=e.length/t|0,o=e.length%t>0;o&&(n+=1);for(let i=0;i<n;i++){let a=i*t,r=a+t;o&&i===n-1&&(r=e.length),s+=e.slice(a,r)+" "}return s}function ellipsize(e,t){const n=tokenize(e);return n.length<=t?e:e.slice(0,n[t].end)+"..."}String.prototype.matchAll||(String.prototype.matchAll=function(e){"use strict";function t(e,t){return e.includes(t)?e:e+t}function*n(e,n){const o=new RegExp(n,t(n.flags,"g"));let s;for(;s=o.exec(e);)s.index=o.lastIndex-s[0].length,yield s}return n(this,e)})