From e6f0522b60f02fdcb7a55f407cad88724bffae10 Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Wed, 4 Jun 2025 13:52:01 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(toc):=20prevent=20active=20s?= =?UTF-8?q?tate=20loss=20from=20dynamically=20loaded=20content?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- layouts/partials/toc.html | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/layouts/partials/toc.html b/layouts/partials/toc.html index 959347ac..35393cdd 100644 --- a/layouts/partials/toc.html +++ b/layouts/partials/toc.html @@ -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 }) {