roadmap/js/util.9825137f5e7825e8553c68...

7 lines
3.9 KiB
JavaScript

const removeMarkdown=(c,b={listUnicodeChar:!1,stripListLeaders:!0,gfm:!0,useImgAltText:!1,preserveLinks:!1})=>{let a=c||"";a=a.replace(/^(-\s*?|\*\s*?|_\s*?){3,}\s*$/gm,"");try{b.stripListLeaders&&(b.listUnicodeChar?a=a.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm,b.listUnicodeChar+" $1"):a=a.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm,"$1")),b.gfm&&(a=a.replace(/\n={2,}/g,"\n").replace(/~{3}.*\n/g,"").replace(/~~/g,"").replace(/`{3}.*\n/g,"")),b.preserveLinks&&(a=a.replace(/\[(.*?)\][\[\(](.*?)[\]\)]/g,"$1 ($2)")),a=a.replace(/<[^>]*>/g,"").replace(/^[=\-]{2,}\s*$/g,"").replace(/\[\^.+?\](\: .*?$)?/g,"").replace(/(#{1,6})\s+(.+)\1?/g,"<b>$2</b>").replace(/\s{0,2}\[.*?\]: .*?$/g,"").replace(/\!\[(.*?)\][\[\(].*?[\]\)]/g,b.useImgAltText?"$1":"").replace(/\[(.*?)\][\[\(].*?[\]\)]/g,"<a>$1</a>").replace(/!?\[\[\S[^\[\]\|]*(?:\|([^\[\]]*))?\S\]\]/g,"<a>$1</a>").replace(/^\s{0,3}>\s?/g,"").replace(/(^|\n)\s{0,3}>\s?/g,"\n\n").replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g,"").replace(/([\*_]{1,3})(\S.*?\S{0,1})\1/g,"$2").replace(/([\*_]{1,3})(\S.*?\S{0,1})\1/g,"$2").replace(/(`{3,})(.*?)\1/gm,"$2").replace(/`(.+?)`/g,"$1").replace(/\n{2,}/g,"\n\n").replace(/\[![a-zA-Z]+\][-\+]? /g,"")}catch(a){return console.error(a),c}return a},highlight=(a,c)=>{const b=20,e=a.indexOf(c);if(e!==-1){const d=b,f=a.substring(0,e).split(" ").slice(-d),g=a.substring(e+c.length,a.length-2).split(" ").slice(0,d);return(f.length==d?`...${f.join(" ")}`:f.join(" "))+`<span class="search-highlight">${c}</span>`+g.join(" ")}const l=c.split(/\s+/).filter(a=>a!==""),d=a.split(/\s+/).filter(a=>a!==""),g=a=>l.some(b=>a.toLowerCase().startsWith(b.toLowerCase())),h=d.map(g);let i=0,j=0;for(let a=0;a<Math.max(h.length-b,0);a++){const d=h.slice(a,a+b),c=d.reduce((a,b)=>a+b,0);c>=i&&(i=c,j=a)}const f=Math.max(j-b,0),k=Math.min(f+2*b,d.length),m=d.slice(f,k).map(a=>g(a)?`<span class="search-highlight">${a}</span>`:a).join(" ").replaceAll('</span> <span class="search-highlight">'," ");return`${f===0?"":"..."}${m}${k===d.length?"":"..."}`},resultToHTML=({url:a,title:b,content:c})=>`<button class="result-card" id="${a}">
<h3>${b}</h3>
<p>${c}</p>
</button>`,redir=(a,b)=>{window.Million.navigate(new URL(`${BASE_URL.replace(/\/$/g,"")}${a}#:~:text=${encodeURIComponent(b)}/`),".singlePage"),closeSearch()};function openSearch(){const b=document.getElementById("search-bar"),c=document.getElementById("results-container"),a=document.getElementById("search-container");a.style.display==="none"||a.style.display===""?(b.value="",c.innerHTML="",a.style.display="block",b.focus()):a.style.display="none"}function closeSearch(){const a=document.getElementById("search-container");a.style.display="none"}const registerHandlers=c=>{const a=document.getElementById("search-bar"),d=document.getElementById("search-container");let e;a.addEventListener("keyup",a=>{if(a.key==="Enter"){const a=document.getElementsByClassName("result-card")[0];redir(a.id,e)}}),a.addEventListener("input",c),document.addEventListener("keydown",a=>{a.key==="k"&&(a.ctrlKey||a.metaKey)&&(a.preventDefault(),openSearch()),a.key==="Escape"&&(a.preventDefault(),closeSearch())});const b=document.getElementById("search-icon");b.addEventListener("click",a=>{openSearch()}),b.addEventListener("keydown",a=>{openSearch()}),d.addEventListener("click",a=>{closeSearch()}),document.getElementById("search-space").addEventListener("click",a=>{a.stopPropagation()})},displayResults=(a,c=!1)=>{const b=document.getElementById("results-container");if(a.length===0)b.innerHTML=`<button class="result-card">
<h3>No results.</h3>
<p>Try another search term?</p>
</button>`;else{b.innerHTML=a.map(a=>c?resultToHTML({url:a.url,title:highlight(a.title,term),content:highlight(removeMarkdown(a.content),term)}):resultToHTML(a)).join("\n");const d=[...document.getElementsByClassName("result-card")];d.forEach(a=>{a.onclick=()=>redir(a.id,term)})}}