-
Notifications
You must be signed in to change notification settings - Fork 661
Expand file tree
/
Copy pathtoc.js
More file actions
154 lines (127 loc) · 4.57 KB
/
toc.js
File metadata and controls
154 lines (127 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Table of Contents Generator
(function() {
'use strict';
// Generate TOC from content headings
function generateTOC() {
const content = document.getElementById('content');
const tocContainers = document.querySelectorAll('.toc-content');
if (!content || tocContainers.length === 0) return;
// Find all h2 and h3 headings with IDs
const headings = content.querySelectorAll('h2[id], h3[id], h4[id], h5[id], h6[id]');
if (headings.length === 0) {
// Hide TOC wrappers if no headings found
document.querySelectorAll('.toc-wrapper').forEach(el => el.style.display = 'none');
return;
}
// Build TOC HTML
const tocHTML = buildTOCHTML(headings);
// Insert TOC into all containers
tocContainers.forEach(container => {
container.innerHTML = tocHTML;
});
// Set up scroll tracking
setupScrollTracking(headings);
}
function buildTOCHTML(headings) {
const pageLang = document.documentElement.lang;
let html = '<ul class="space-y-3 text-sm list-disc pl-5 marker:text-stone-400 dark:marker:text-stone-500">';
let currentLevel = 2;
headings.forEach(heading => {
const level = parseInt(heading.tagName.substr(1));
const text = heading.textContent;
const id = heading.id;
// Check for lang attribute on heading or its ancestors
const lang = heading.getAttribute('lang') || heading.closest('[lang]')?.getAttribute('lang');
const langAttr = (lang && lang !== pageLang) ? ` lang="${lang}"` : '';
if (level > currentLevel) {
html += '<ul class="space-y-3 pl-4 mt-2 list-disc marker:text-stone-400 dark:marker:text-stone-500">';
} else if (level < currentLevel) {
html += '</ul>';
}
html += `<li class="mb-3"${langAttr}><a href="#${id}" class="toc-link text-stone-700 dark:text-stone-300 no-underline transition-colors" data-heading-id="${id}">${text}</a></li>`;
currentLevel = level;
});
// Close any open ul tags
while (currentLevel > 2) {
html += '</ul>';
currentLevel--;
}
html += '</ul>';
return html;
}
// Set up scroll tracking
function setupScrollTracking(headings) {
// Convert NodeList to Array for easier manipulation
const headingsArray = Array.from(headings);
// Update active heading on scroll
function updateActiveHeading() {
// Threshold: top of viewport + small offset (e.g., 100px for header)
const scrollOffset = 100;
// Find the heading closest to the top of the viewport
let activeHeading = null;
for (let i = headingsArray.length - 1; i >= 0; i--) {
const heading = headingsArray[i];
const rect = heading.getBoundingClientRect();
// Check if heading is above or at the scroll offset
if (rect.top <= scrollOffset) {
activeHeading = heading;
break;
}
}
// If no heading is above the offset, use the first one if it's visible
if (!activeHeading && headingsArray.length > 0) {
const firstRect = headingsArray[0].getBoundingClientRect();
if (firstRect.top > scrollOffset) {
// We're above the first heading, no active heading
clearActiveLink();
return;
}
}
if (activeHeading) {
setActiveLink(activeHeading.id);
} else {
clearActiveLink();
}
}
// Update on scroll with throttling for performance
let ticking = false;
window.addEventListener('scroll', function() {
if (!ticking) {
window.requestAnimationFrame(function() {
updateActiveHeading();
ticking = false;
});
ticking = true;
}
});
// Update on page load and hash change
updateActiveHeading();
window.addEventListener('hashchange', function() {
setTimeout(updateActiveHeading, 100);
});
}
// Set active link in TOC
function setActiveLink(id) {
// Remove active class from all links first
document.querySelectorAll('.toc-link').forEach(link => {
link.classList.remove('toc-active');
});
// Add active class to current link
const links = document.querySelectorAll(`.toc-link[data-heading-id="${id}"]`);
links.forEach(link => {
link.classList.add('toc-active');
});
}
// Clear active link
function clearActiveLink() {
document.querySelectorAll('.toc-link').forEach(link => {
link.classList.remove('toc-active');
});
}
// Run on page load
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', generateTOC);
} else {
generateTOC();
}
})();