-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1 lines (1 loc) · 4.46 KB
/
Copy pathscript.js
File metadata and controls
1 lines (1 loc) · 4.46 KB
1
(()=>{const $=s=>document.querySelector(s),guide=$('#guide'),toc=$('#toc'),nav=$('#nav'),shade=$('#shade');const slug=s=>s.toLowerCase().replace(/&/g,'and').replace(/[^a-z0-9]+/g,'-').replace(/^-|-$/g,'');const inline=s=>s.replace(/\[([^\]]+)\]\(([^)]+)\)/g,'<a href="$2">$1</a>').replace(/\*\*([^*]+)\*\*/g,'<strong>$1</strong>').replace(/\*([^*]+)\*/g,'<em>$1</em>').replace(/`([^`]+)`/g,'<code>$1</code>');function render(md){let out=[],lines=md.replace(/\r/g,'').split('\n'),i=0,list=null,quote=[];const close=()=>{if(list){out.push(`</${list}>`);list=null}if(quote.length){out.push(`<blockquote>${quote.map(x=>`<p>${inline(x)}</p>`).join('')}</blockquote>`);quote=[]}};while(i<lines.length){let l=lines[i];if(l.startsWith('```')){close();let code=[],lang=l.slice(3);while(++i<lines.length&&!lines[i].startsWith('```'))code.push(lines[i]);out.push(`<pre><code class="language-${lang}">${code.join('\n').replace(/&/g,'&').replace(/</g,'<')}</code></pre>`);i++;continue}if(l.startsWith('> ')){if(list){out.push(`</${list}>`);list=null}quote.push(l.slice(2));i++;continue}else if(quote.length)close();let m=l.match(/^(#{1,4}) (.+)$/);if(m){close();let n=m[1].length,id=slug(m[2].replace(/[*`]/g,''));out.push(`<h${n} id="${id}" tabindex="-1">${inline(m[2])}</h${n}>`);i++;continue}if(/^---+$/.test(l)){close();out.push('<hr>');i++;continue}m=l.match(/^[-*] (.+)$/);if(m){if(list!=='ul'){close();list='ul';out.push('<ul>')}out.push(`<li>${inline(m[1])}</li>`);i++;continue}m=l.match(/^\d+\. (.+)$/);if(m){if(list!=='ol'){close();list='ol';out.push('<ol>')}out.push(`<li>${inline(m[1])}</li>`);i++;continue}if(!l.trim()){close();i++;continue}close();let p=[l];while(i+1<lines.length&&lines[i+1].trim()&&!/^(#{1,4}) |^[-*] |^\d+\. |^> |^```|^---+$/.test(lines[i+1]))p.push(lines[++i]);out.push(`<p>${inline(p.join(' '))}</p>`);i++}close();return out.join('\n')}Promise.all(['guide-1.md','guide-rest.md'].map(x=>fetch(x).then(r=>{if(!r.ok)throw Error(r.status);return r.text()}))).then(parts=>parts.join('\n')).then(md=>{guide.innerHTML=render(md);const heads=[...guide.querySelectorAll('h1,h2,h3')].slice(1);heads.forEach(h=>{let a=document.createElement('a');a.href='#'+h.id;a.textContent=h.textContent;a.className=h.tagName.toLowerCase();a.onclick=()=>open(false);toc.append(a)});const obs=new IntersectionObserver(es=>{const v=es.find(e=>e.isIntersecting);if(v)toc.querySelectorAll('a').forEach(a=>a.classList.toggle('active',a.hash==='#'+v.target.id))},{rootMargin:'-18% 0px -72%'});heads.forEach(h=>obs.observe(h))}).catch(()=>guide.innerHTML='<p>Guide failed to load. Open the Markdown source files.</p>');function open(v){nav.classList.toggle('open',v);shade.classList.toggle('open',v);$('#menu').setAttribute('aria-expanded',v);document.body.style.overflow=v?'hidden':''}$('#menu').onclick=()=>open(true);$('#close').onclick=shade.onclick=()=>open(false);document.onkeydown=e=>{if(e.key==='Escape')open(false)};$('#navSearch').oninput=e=>toc.querySelectorAll('a').forEach(a=>a.hidden=!a.textContent.toLowerCase().includes(e.target.value.toLowerCase()));const setTheme=t=>{document.documentElement.dataset.theme=t;localStorage.theme=t;$('#theme').textContent=t==='dark'?'◐':'◑'};setTheme(localStorage.theme||matchMedia('(prefers-color-scheme:light)').matches?'light':'dark');$('#theme').onclick=()=>setTheme(document.documentElement.dataset.theme==='dark'?'light':'dark');onscroll=()=>$('#progress').style.width=100*scrollY/(document.documentElement.scrollHeight-innerHeight)+'%';let timer;$('#search').oninput=e=>{clearTimeout(timer);timer=setTimeout(()=>{guide.querySelectorAll('mark').forEach(m=>m.replaceWith(m.textContent));guide.normalize();let q=e.target.value.trim(),n=0;$('#clear').hidden=!q;if(q.length<2){$('#status').textContent='';return}let re=new RegExp(q.replace(/[.*+?^${}()|[\]\\]/g,'\\$&'),'gi'),w=document.createTreeWalker(guide,NodeFilter.SHOW_TEXT);let nodes=[];while(w.nextNode())nodes.push(w.currentNode);nodes.forEach(node=>{if(!re.test(node.data))return;re.lastIndex=0;let f=document.createDocumentFragment(),last=0;for(const m of node.data.matchAll(re)){f.append(node.data.slice(last,m.index));let mark=document.createElement('mark');mark.textContent=m[0];f.append(mark);last=m.index+m[0].length;n++}f.append(node.data.slice(last));node.replaceWith(f)});$('#status').textContent=`${n} match${n===1?'':'es'}`;guide.querySelector('mark')?.scrollIntoView({behavior:'smooth',block:'center'})},180)};$('#clear').onclick=()=>{$('#search').value='';$('#search').dispatchEvent(new Event('input'));$('#clear').hidden=true}})();