Improve TOC handling

This commit is contained in:
2025-11-18 15:28:51 +13:00
parent caad696191
commit 3e742ba952

View File

@@ -38,14 +38,11 @@
} }
// get all titles in the page // get all titles in the page
const titles = document.querySelectorAll('h2, h3') const titles = document.querySelectorAll('h1, h2, h3')
const toc = document.getElementById('toc') as Element const toc = document.getElementById('toc') as Element
const toc_cnt = document.getElementById('toc-container') as Element const toc_cnt = document.getElementById('toc-container') as Element
console.log(titles) if (titles.length > 0) {
// if there's more than two titles (ie, there's more than just the article title and TOC title, then appropriate to show)
if (titles.length > 2) {
toc_cnt.classList.add('lg:block') toc_cnt.classList.add('lg:block')
} }
@@ -54,19 +51,7 @@
// Assign IDs in case they haven't been (by me) // Assign IDs in case they haven't been (by me)
t.id = t.innerHTML t.id = t.innerHTML
let depth = t.tagName.substring(1) as number
let depth = 1
// determine what depth to use
switch (t.tagName) {
case 'H2':
depth = 1
break
case 'H3':
depth = 2
break
}
addLink(t, depth) addLink(t, depth)
} }
</script> </script>