Merge pull request #2207 from ZhenShuo2021/fix/toc-active

🐛 Fix TOC Active State Loss with Dynamic Content
This commit is contained in:
Nuno Coração
2025-06-17 22:31:32 +01:00
committed by GitHub

View File

@@ -33,11 +33,16 @@
function getActiveAnchorId(anchors, offsetRatio) {
const threshold = window.scrollY + window.innerHeight * offsetRatio
const tocLinks = [...document.querySelectorAll('#TableOfContents a[href^="#"]')]
const tocIds = new Set(tocLinks.map(link => link.getAttribute('href').substring(1)))
for (let i = anchors.length - 1; i >= 0; i--) {
const top = anchors[i].getBoundingClientRect().top + window.scrollY
if (top <= threshold) return anchors[i].id
if (top <= threshold && tocIds.has(anchors[i].id)) {
return anchors[i].id
}
}
return anchors[0]?.id || ''
return anchors.find(anchor => tocIds.has(anchor.id))?.id || ''
}
function updateTOC({ toc, anchors, links, scrollOffset, collapseInactive }) {