Cours-programmation/js/search.bc849b857f2c1b822264d40635bb67b6.min.js
2022-04-09 22:14:47 +00:00

7 lines
3.7 KiB
JavaScript

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(/\s{0,2}\[.*?\]: .*?$/g,"").replace(/!\[(.*?)\][[(].*?[\])]/g,t.useImgAltText?"$1":"").replace(/\[(.*?)\][[(].*?[\])]/g,"$1").replace(/^\s{0,3}>\s?/g,"").replace(/(^|\n)\s{0,3}>\s?/g,"\n\n").replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g,"").replace(/^(\n)?\s{0,}#{1,6}\s+| {0,}(\n)?\s{0,}#{0,} {0,}(\n)?\s{0,}$/gm,"$1$2$3").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")}catch(e){return console.error(e),n}return e};(async function(){const a=e=>e.toLowerCase().split(/([^a-z]|[^\x00-\x7F])+/),d=new FlexSearch.Document({cache:!0,charset:"latin:extra",optimize:!0,index:[{field:"content",tokenize:"reverse",encode:a},{field:"title",tokenize:"forward",encode:a}]}),{content:o}=await fetchData;for(const[t,e]of Object.entries(o))d.add({id:t,title:e.title,content:removeMarkdown(e.content)});const l=(c,l)=>{const e=20,d=l.split(/\s+/).filter(e=>e!==""),t=c.split(/\s+/).filter(e=>e!==""),i=e=>d.some(t=>e.toLowerCase().startsWith(t.toLowerCase())),s=t.map(i);let o=0,a=0;for(let t=0;t<Math.max(s.length-e,0);t++){const i=s.slice(t,t+e),n=i.reduce((e,t)=>e+t,0);n>=o&&(o=n,a=t)}const n=Math.max(a-e,0),r=Math.min(n+2*e,t.length),u=t.slice(n,r).map(e=>i(e)?`<span class="search-highlight">${e}</span>`:e).join(" ").replaceAll('</span> <span class="search-highlight">'," ");return`${n===0?"":"..."}${u}${r===t.length?"":"..."}`},m=({url:t,title:n,content:s,term:e})=>{const o=removeMarkdown(s),i=l(n,e),a=l(o,e);return`<button class="result-card" id="${t}">
<h3>${i}</h3>
<p>${a}</p>
</button>`},r=(e,t)=>{window.location.href=BASE_URL+`${e}#:~:text=${encodeURIComponent(t)}`},h=e=>({id:e,url:e,title:o[e].title,content:o[e].content}),n=document.getElementById("search-bar"),i=document.getElementById("results-container");let t;n.addEventListener("keyup",e=>{if(e.key==="Enter"){const e=document.getElementsByClassName("result-card")[0];r(e.id,t)}}),n.addEventListener("input",s=>{t=s.target.value;const o=d.search(t,[{field:"content",limit:10},{field:"title",limit:5}]),e=t=>{const e=o.filter(e=>e.field===t);return e.length===0?[]:[...e[0].result]},a=new Set([...e("title"),...e("content")]),n=[...a].map(h);if(n.length===0)i.innerHTML=`<button class="result-card">
<h3>No results.</h3>
<p>Try another search term?</p>
</button>`;else{i.innerHTML=n.map(e=>m({...e,term:t})).join("\n");const e=document.getElementsByClassName("result-card");[...e].forEach(e=>{e.onclick=()=>r(e.id,t)})}});const e=document.getElementById("search-container");function s(){e.style.display==="none"||e.style.display===""?(n.value="",i.innerHTML="",e.style.display="block",n.focus()):e.style.display="none"}function c(){e.style.display="none"}document.addEventListener("keydown",e=>{e.key==="k"&&(e.ctrlKey||e.metaKey)&&(e.preventDefault(),s()),e.key==="Escape"&&(e.preventDefault(),c())});const u=document.getElementById("search-icon");u.addEventListener("click",e=>{s()}),u.addEventListener("keydown",e=>{s()}),e.addEventListener("click",e=>{c()}),document.getElementById("search-space").addEventListener("click",e=>{e.stopPropagation()})})()