feat: support non-singleton table of contents

This commit is contained in:
Jacky Zhao
2025-03-09 15:06:36 -07:00
parent 5480269d38
commit 1cd8e7f0d5
4 changed files with 14 additions and 16 deletions

View File

@ -25,16 +25,15 @@ function toggleToc(this: HTMLElement) {
}
function setupToc() {
const toc = document.getElementById("toc")
if (toc) {
const content = toc.nextElementSibling as HTMLElement | undefined
if (!content) return
toc.addEventListener("click", toggleToc)
window.addCleanup(() => toc.removeEventListener("click", toggleToc))
for (const toc of document.querySelectorAll(".toc")) {
const button = toc.querySelector(".toc-header")
const content = toc.querySelector(".toc-content")
if (!button || !content) return
button.addEventListener("click", toggleToc)
window.addCleanup(() => button.removeEventListener("click", toggleToc))
}
}
window.addEventListener("resize", setupToc)
document.addEventListener("nav", () => {
setupToc()