Tiny Table-of-Contents JS Snippet

from blog taylor.town, | ↗ original
const toc = document.getElementById('table-of-contents'); for (const x of document.querySelectorAll('h2, h3')) { const id = x.innerText.replaceAll(/[^a-z0-9]/gi,''); x.id = id; const item = `${x.innerText}`; switch (x.tagName.toLowerCase()) { case "h2": toc.insertAdjacentHTML('beforeend', `${item}`); break; case "h3":...