|
| 1 | +/** |
| 2 | + * Simple TOC scroll-based highlighting |
| 3 | + * Highlights current section in purple text |
| 4 | + */ |
| 5 | + |
| 6 | +(function () { |
| 7 | + "use strict"; |
| 8 | + |
| 9 | + let tocLinks = []; |
| 10 | + let headings = []; |
| 11 | + |
| 12 | + function init() { |
| 13 | + // Find TOC links |
| 14 | + tocLinks = Array.from( |
| 15 | + document.querySelectorAll(".md-nav--secondary .md-nav__link") |
| 16 | + ); |
| 17 | + if (tocLinks.length === 0) return; |
| 18 | + |
| 19 | + // Find corresponding headings |
| 20 | + headings = tocLinks |
| 21 | + .map((link) => { |
| 22 | + const href = link.getAttribute("href"); |
| 23 | + return href && href.startsWith("#") |
| 24 | + ? document.getElementById(href.substring(1)) |
| 25 | + : null; |
| 26 | + }) |
| 27 | + .filter((h) => h !== null); |
| 28 | + |
| 29 | + if (headings.length === 0) return; |
| 30 | + |
| 31 | + // Listen to scroll |
| 32 | + window.addEventListener("scroll", updateHighlight, { passive: true }); |
| 33 | + updateHighlight(); |
| 34 | + } |
| 35 | + |
| 36 | + function updateHighlight() { |
| 37 | + const scrollTop = window.pageYOffset; |
| 38 | + const windowHeight = window.innerHeight; |
| 39 | + const documentHeight = document.documentElement.scrollHeight; |
| 40 | + |
| 41 | + // If near bottom, highlight last item |
| 42 | + if (scrollTop + windowHeight >= documentHeight - 50) { |
| 43 | + setActive(tocLinks.length - 1); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // Find current section |
| 48 | + let activeIndex = -1; |
| 49 | + for (let i = headings.length - 1; i >= 0; i--) { |
| 50 | + if (headings[i] && headings[i].getBoundingClientRect().top <= 80) { |
| 51 | + activeIndex = i; |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + setActive(activeIndex); |
| 57 | + } |
| 58 | + |
| 59 | + function setActive(index) { |
| 60 | + // Remove all active classes |
| 61 | + tocLinks.forEach((link) => { |
| 62 | + link.classList.remove("md-nav__link--active", "is-active"); |
| 63 | + }); |
| 64 | + |
| 65 | + // Add active class to current item and scroll into view if needed |
| 66 | + if (index >= 0 && index < tocLinks.length) { |
| 67 | + const activeLink = tocLinks[index]; |
| 68 | + activeLink.classList.add("md-nav__link--active", "is-active"); |
| 69 | + // Scroll the active link into view within the sidebar |
| 70 | + // Only if not already fully visible |
| 71 | + if (typeof activeLink.scrollIntoView === "function") { |
| 72 | + activeLink.scrollIntoView({ block: "nearest", behavior: "smooth" }); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Initialize when ready |
| 78 | + if (document.readyState === "loading") { |
| 79 | + document.addEventListener("DOMContentLoaded", init); |
| 80 | + } else { |
| 81 | + init(); |
| 82 | + } |
| 83 | + |
| 84 | + // Re-initialize on page changes |
| 85 | + let currentUrl = location.href; |
| 86 | + new MutationObserver(() => { |
| 87 | + if (location.href !== currentUrl) { |
| 88 | + currentUrl = location.href; |
| 89 | + setTimeout(init, 100); |
| 90 | + } |
| 91 | + }).observe(document, { childList: true, subtree: true }); |
| 92 | +})(); |
0 commit comments