amethyst/quartz/js/util.c69e233dc1cc331a30b0f670e657b425.min.js

13 lines
4.0 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?)\1/g,"$2").replace(/([*_]{1,3})(\S.*?\S?)\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)=>{const n=PRODUCTION&&SEARCH_ENABLED,s=n?"":BASE_URL.replace(/\/$/g,""),o=`${s}${e}#:~:text=${encodeURIComponent(t)}/`;window.Million.navigate(new URL(o),".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"),n=document.getElementById("search-container");let s;t.addEventListener("keyup",e=>{if(e.key==="Enter"){const e=document.getElementsByClassName("result-card")[0];redir(e.id,s)}}),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 o=document.getElementsByClassName("quartz-search");for(let e of o)e.addEventListener("click",e=>{openSearch()}),e.addEventListener("keydown",e=>{openSearch()}),n.addEventListener("click",e=>{closeSearch()}),document.getElementById("search-space").addEventListener("click",e=>{e.stopPropagation()})},displayResults=(e,t,n=!1)=>{const s=document.getElementById("results-container");if(t.length===0)s.innerHTML=`<button class="result-card">
<h3>No results.</h3>
<p>Try another search term?</p>
</button>`;else{s.innerHTML=t.map(t=>resultToHTML(n?{url:t.url,title:highlight(t.title,e),content:highlight(removeMarkdown(t.content),e)}:t)).join(`
`),LATEX_ENABLED&&renderMathInElement(s,{delimiters:[{left:"$$",right:"$$",display:!1},{left:"$",right:"$",display:!1}],throwOnError:!1});const o=[...document.getElementsByClassName("result-card")];o.forEach(t=>{t.onclick=()=>redir(t.id,e)})}}