@@ -83,27 +83,30 @@ const tocHeadings = headings.filter(h => h.depth >= 2 && h.depth <= 3);
8383 const scrollY = window.scrollY;
8484 const windowHeight = window.innerHeight;
8585 const documentHeight = document.documentElement.scrollHeight;
86-
86+
87+ const currentHeadings = visibleHeadings();
88+ if (currentHeadings.length === 0) return;
89+
8790 // Check if we're at the bottom of the page
8891 // If the user has scrolled to near the bottom, always highlight the last item
8992 // This handles cases where the last section is too short to reach the top offset
9093 if (scrollY + windowHeight >= documentHeight - 50) {
91- const lastHeading = headings[headings .length - 1];
94+ const lastHeading = currentHeadings[currentHeadings .length - 1];
9295 const id = lastHeading?.getAttribute('id');
9396 if (id) {
9497 setActiveLink(id);
9598 return;
9699 }
97100 }
98-
101+
99102 // Find the last heading that is above or at the headerOffset
100103 // This mimics standard "scroll spy" behavior: the section you are reading
101104 // is the one whose header you most recently passed.
102- let activeHeading = headings [0];
103-
104- for (const heading of headings ) {
105+ let activeHeading = currentHeadings [0];
106+
107+ for (const heading of currentHeadings ) {
105108 const rect = heading.getBoundingClientRect();
106-
109+
107110 if (rect.top <= headerOffset) {
108111 activeHeading = heading;
109112 } else {
@@ -122,7 +125,13 @@ const tocHeadings = headings.filter(h => h.depth >= 2 && h.depth <= 3);
122125 // Update on scroll with throttling
123126 let ticking = false;
124127 let scrollEndTimer;
125-
128+
129+ // SDK-version blocks hide their content via `display: none` on the
130+ // wrapping `[data-sdk-version]` div. Headings inside a hidden block
131+ // have offsetParent === null — skip them so scrollspy never lands on
132+ // a heading the user can't see.
133+ const visibleHeadings = () => headings.filter(h => h.offsetParent !== null);
134+
126135 const handleScroll = () => {
127136 // If we are in "click mode", we purely want to detect when scrolling STOPS
128137 // We don't want to run the expensive/noisy update logic during the smooth scroll
0 commit comments