|
| 1 | +/* Enhanced TOC behavior: affix, active-section highlighting, and auto-scroll */ |
| 2 | +(function () { |
| 3 | + function safeId(href) { return href && href.charAt(0) === '#' ? href.slice(1) : null } |
| 4 | + document.addEventListener('DOMContentLoaded', function () { |
| 5 | + var toc = document.getElementById('markdown-toc'); |
| 6 | + if (!toc) return; |
| 7 | + // sentinel to toggle affix class when TOC scrolls past top |
| 8 | + var sentinel = document.createElement('div'); |
| 9 | + sentinel.className = 'toc-sentinel'; |
| 10 | + toc.parentNode.insertBefore(sentinel, toc); |
| 11 | + var sentObs = new IntersectionObserver(function (entries) { |
| 12 | + entries.forEach(function (e) { |
| 13 | + if (!e.isIntersecting) toc.classList.add('affix'); else toc.classList.remove('affix'); |
| 14 | + }); |
| 15 | + }, { root: null, threshold: 0, rootMargin: '-1px 0px 0px 0px' }); |
| 16 | + sentObs.observe(sentinel); |
| 17 | + |
| 18 | + var links = Array.from(toc.querySelectorAll('a[href^="#"]')); |
| 19 | + var targets = links.map(function (a) { return document.getElementById(safeId(a.getAttribute('href'))) }).filter(Boolean); |
| 20 | + if (targets.length === 0) return; |
| 21 | + |
| 22 | + var offsets = new Map(); |
| 23 | + var visible = new Set(); |
| 24 | + var first = true; |
| 25 | + var tocContainsOverscroll = getComputedStyle(toc).overscrollBehaviorY === 'contain'; |
| 26 | + |
| 27 | + function setActive(activeEl) { |
| 28 | + links.forEach(function (a) { a.style.fontWeight = '' }); |
| 29 | + if (!activeEl) return; |
| 30 | + var selector = 'a[href="#' + activeEl.id + '"]'; |
| 31 | + var link = toc.querySelector(selector); |
| 32 | + if (link) { |
| 33 | + link.style.fontWeight = 'bold'; |
| 34 | + if (tocContainsOverscroll) { |
| 35 | + clearTimeout(link.__tocScrollTimer); |
| 36 | + link.__tocScrollTimer = setTimeout(function () { |
| 37 | + if (toc.classList.contains('affix')) link.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'nearest' }); |
| 38 | + }, 100); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + var observer = new IntersectionObserver(function (entries) { |
| 44 | + if (first) { |
| 45 | + targets.forEach(function (t) { offsets.set(t, t.getBoundingClientRect().top + window.pageYOffset); }); |
| 46 | + first = false; |
| 47 | + } |
| 48 | + entries.forEach(function (entry) { |
| 49 | + var el = entry.target; |
| 50 | + if (entry.isIntersecting) visible.add(el); else visible.delete(el); |
| 51 | + }); |
| 52 | + if (visible.size === 0) { setActive(null); return; } |
| 53 | + var active = null; |
| 54 | + visible.forEach(function (el) { if (!active || offsets.get(el) < offsets.get(active)) active = el; }); |
| 55 | + setActive(active); |
| 56 | + }, { root: null, threshold: [0, 0.1, 0.5, 1] }); |
| 57 | + |
| 58 | + targets.forEach(function (t) { observer.observe(t) }); |
| 59 | + window.addEventListener('unload', function () { observer.disconnect(); sentObs.disconnect(); }); |
| 60 | + }); |
| 61 | +})(); |
0 commit comments