@xiee/utils
Version:
Miscellaneous tools and utilities to manipulate HTML pages
2 lines • 919 B
JavaScript
(e=>{const t='a[href^="#"]',o=e.querySelector(`:is(#TableOfContents, #TOC):has(${t})`);if(!o)return;const n=o.querySelectorAll(t),r={};n.forEach(e=>r[e.getAttribute("href").replace("#","")]=e);const i=Object.keys(r);let c,s;const l=Array(i.length).fill(0),f=new IntersectionObserver(e=>e.forEach(e=>{const t=e.target.id,n=i.indexOf(t);if(n<0)return;l[n]=+e.isIntersecting;const f=l.indexOf(1);if(f>-1)c=i[f];else{if(!c||e.boundingClientRect.top<0)return;c=n>0?i[n-1]:void 0}if(c===s)return;for(const e in r)r[e].classList.toggle("active",e===c);s=c;const h=r[c];if(!h||o.scrollHeight<=o.clientHeight)return;const a=h.getBoundingClientRect(),g=o.getBoundingClientRect();(a.top<g.top||a.bottom>g.bottom)&&o.scrollTo({top:h.offsetTop,behavior:"smooth"})}));e.querySelectorAll("h1,h2,h3,h4,h5,h6").forEach(e=>{1===e.nodeType&&r.hasOwnProperty(e.id)&&f.observe(e)})})(document);
//# sourceMappingURL=toc-highlight.min.js.map