ar.falsy.cat/js/util.00639692264b21bc3ee219733d38a8be.min.js

7 lines
4.0 KiB
JavaScript
Raw Normal View History

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