diff --git a/static/script/toc.js b/static/script/toc.js index 826cff9..4a16e6d 100644 --- a/static/script/toc.js +++ b/static/script/toc.js @@ -14,16 +14,18 @@ if (toc) { toc.parentElement.remove(); } else { const observer = new IntersectionObserver((entries) => { - const filtered = entries.filter((i) => getId(i.target)); - filtered.forEach((entry) => { - const id = getId(entry.target); - if (id && entry.intersectionRatio > 0) { - toc.querySelector(`li.active`)?.classList.remove("active"); + const filtered = entries.filter( + (i) => getId(i.target) && i.intersectionRatio > 0 + ); + if (filtered.length > 0) { + toc.querySelector(`li.active`)?.classList.remove("active"); + filtered.forEach((entry) => { + const id = getId(entry.target); toc .querySelector(`li a[href="#${id}"]`) .parentElement.classList.add("active"); - } - }); + }); + } }); // Track all sections that have an `id` applied