Skip to content

Commit 5729c2a

Browse files
committed
scroll spy to highlight TOC as you scroll
1 parent 260a8da commit 5729c2a

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

_layouts/default.html

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ <h2 class="text-delta">Table of contents</h2>
194194

195195
<script src="/assets/js/clipboard.min.js"></script>
196196
<script>
197-
// Add copy buttons to code blocks
198197
document.addEventListener('DOMContentLoaded', function() {
199198
document.querySelectorAll('.highlight').forEach(function(block) {
200199
var btn = document.createElement('button');
@@ -211,6 +210,46 @@ <h2 class="text-delta">Table of contents</h2>
211210
block.appendChild(btn);
212211
});
213212
});
213+
214+
document.addEventListener('DOMContentLoaded', function() {
215+
const tocLinks = document.querySelectorAll('.toc-sidebar .toc-link');
216+
if (tocLinks.length === 0) return;
217+
218+
const headingIds = Array.from(tocLinks).map(link => {
219+
const href = link.getAttribute('href');
220+
return href ? href.substring(1) : null;
221+
}).filter(Boolean);
222+
223+
const headings = headingIds.map(id => document.getElementById(id)).filter(Boolean);
224+
if (headings.length === 0) return;
225+
226+
function updateActiveSection() {
227+
const scrollPos = window.scrollY + 100;
228+
229+
let currentSection = null;
230+
231+
headings.forEach(function(heading) {
232+
if (heading.offsetTop <= scrollPos) {
233+
currentSection = heading.id;
234+
}
235+
});
236+
237+
tocLinks.forEach(function(link) {
238+
link.classList.remove('toc-active');
239+
});
240+
241+
if (currentSection) {
242+
const activeLink = document.querySelector('.toc-sidebar .toc-link[href="#' + currentSection + '"]');
243+
if (activeLink) {
244+
activeLink.classList.add('toc-active');
245+
}
246+
}
247+
}
248+
249+
window.addEventListener('scroll', updateActiveSection);
250+
251+
updateActiveSection();
252+
});
214253
</script>
215254
</body>
216255
</html>

_sass/custom/custom.scss

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,18 +362,31 @@ img {
362362
text-decoration: none;
363363
border-left: 2px solid transparent;
364364
margin-left: -1px;
365-
transition: color 0.15s ease, border-color 0.15s ease;
365+
transition: color 0.15s ease, border-color 0.15s ease, font-weight 0.15s ease;
366366

367367
&:hover {
368368
color: $link-color;
369369
border-left-color: $grey-lt-100;
370370
}
371+
372+
// Active state for scroll-spy
373+
&.toc-active {
374+
color: $link-color !important;
375+
border-left-color: $link-color !important;
376+
font-weight: 600;
377+
background-color: rgba($link-color, 0.08);
378+
}
371379
}
372380

373381
.toc-h3 .toc-link {
374382
padding-left: 1.25rem;
375383
font-size: 0.75rem;
376384
color: $grey-dk-000;
385+
386+
&.toc-active {
387+
color: $link-color;
388+
font-weight: 600;
389+
}
377390
}
378391

379392
.toc-sublist {

0 commit comments

Comments
 (0)