roadmap/js/util.9825137f5e7825e8553c68...

13 lines
3.8 KiB
JavaScript

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