-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
311 lines (258 loc) · 9.73 KB
/
Copy pathscript.js
File metadata and controls
311 lines (258 loc) · 9.73 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Theme switching functionality.
//
// The theme is always pinned as an explicit data-theme ("light" or "dark")
// rather than left unset for light, so that toggling stays in step with the
// OS preference: style.css styles a root with no data-theme as dark when the
// OS asks for dark, and without an explicit value the first click would flip
// the label without changing anything visible.
function applyTheme(theme) {
const root = document.documentElement;
const button = document.querySelector('.theme-switcher');
root.setAttribute('data-theme', theme);
if (button) {
button.textContent = theme === 'dark' ? '🌙 Light' : '🌙 Dark';
button.title = theme === 'dark'
? 'Switch to light theme'
: 'Switch to dark theme';
}
}
function preferredTheme() {
const saved = localStorage.getItem('nimony-theme');
if (saved === 'dark' || saved === 'light') return saved;
return window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light';
}
function toggleTheme() {
const next = document.documentElement.getAttribute('data-theme') === 'dark'
? 'light'
: 'dark';
applyTheme(next);
// Save preference to localStorage
localStorage.setItem('nimony-theme', next);
}
// Sidebar toggle functionality
function toggleSidebar() {
const sidebar = document.getElementById('rightSidebar');
const toggleBtn = document.getElementById('sidebarToggle');
if (sidebar.classList.contains('collapsed')) {
sidebar.classList.remove('collapsed');
toggleBtn.style.display = 'none';
} else {
sidebar.classList.add('collapsed');
toggleBtn.style.display = 'block';
}
// Save preference to localStorage
localStorage.setItem('nimony-sidebar-collapsed', sidebar.classList.contains('collapsed'));
}
// Navigation toggle functionality
function toggleNavigation() {
const hierarchy = document.getElementById('navHierarchy');
const button = document.querySelector('button.nav-btn[onclick*="toggleNavigation"]');
if (!hierarchy || !button) return;
if (hierarchy.classList.contains('active')) {
hierarchy.classList.remove('active');
button.textContent = 'Navigation';
button.classList.remove('expanded');
} else {
hierarchy.classList.add('active');
button.textContent = 'Hide navigation';
button.classList.add('expanded');
}
}
// Scroll to section functionality
function scrollToSection(sectionId) {
const section = document.getElementById(sectionId);
if (section) {
section.scrollIntoView({ behavior: 'smooth' });
// Update URL with anchor
history.pushState(null, null, `#${sectionId}`);
}
}
// File overview functionality
function updateFileOverview() {
const sections = document.querySelectorAll('h1, h2, h3');
const sectionList = document.getElementById('sectionList');
if (!sectionList) return;
// Clear existing list
sectionList.innerHTML = '';
// Create hierarchical structure
const hierarchy = [];
let currentH1 = null;
let currentH2 = null;
// Filter out the header h1
const contentSections = Array.from(sections).filter(section => {
return !(section.tagName === 'H1' && section.closest('header'));
});
contentSections.forEach((section, index) => {
const sectionText = section.textContent.trim();
const sectionId = section.id || `section-${index}`;
const tagName = section.tagName.toLowerCase();
if (tagName === 'h1') {
currentH1 = {
text: sectionText,
id: sectionId,
children: []
};
hierarchy.push(currentH1);
currentH2 = null;
} else if (tagName === 'h2') {
if (currentH1) {
currentH2 = {
text: sectionText,
id: sectionId,
children: []
};
currentH1.children.push(currentH2);
} else {
// H2 without parent H1
currentH2 = {
text: sectionText,
id: sectionId,
children: []
};
hierarchy.push(currentH2);
}
} else if (tagName === 'h3') {
const h3Item = {
text: sectionText,
id: sectionId
};
if (currentH2) {
currentH2.children.push(h3Item);
} else if (currentH1) {
currentH1.children.push(h3Item);
} else {
// H3 without parent H1 or H2
hierarchy.push(h3Item);
}
}
});
// Build nested HTML structure
function createListItem(item) {
const li = document.createElement('li');
const a = document.createElement('a');
a.href = `#${item.id}`;
a.textContent = item.text;
a.title = item.text;
// Add click handler for smooth scrolling
a.addEventListener('click', function(e) {
e.preventDefault();
const targetSection = document.getElementById(item.id);
if (targetSection) {
targetSection.scrollIntoView({ behavior: 'smooth' });
// Update URL with anchor
history.pushState(null, null, `#${item.id}`);
}
});
li.appendChild(a);
// Add nested list if there are children
if (item.children && item.children.length > 0) {
const ul = document.createElement('ul');
item.children.forEach(child => {
ul.appendChild(createListItem(child));
});
li.appendChild(ul);
}
return li;
}
// Add all top-level items to the section list
hierarchy.forEach(item => {
sectionList.appendChild(createListItem(item));
});
// Update current section based on scroll position
updateCurrentSection();
}
// Update current section based on scroll position
function updateCurrentSection() {
const sections = document.querySelectorAll('h1, h2, h3');
const currentSection = document.getElementById('currentSection');
if (!currentSection || sections.length === 0) return;
const scrollPosition = window.scrollY + 100; // Offset for better detection
let currentSectionText = 'Introduction';
for (let i = sections.length - 1; i >= 0; i--) {
const section = sections[i];
const sectionTop = section.offsetTop;
if (scrollPosition >= sectionTop) {
currentSectionText = section.textContent.trim();
break;
}
}
currentSection.textContent = currentSectionText;
}
// Intersection Observer for better current section detection
function setupIntersectionObserver() {
const sections = document.querySelectorAll('h1, h2, h3');
const currentSection = document.getElementById('currentSection');
if (!currentSection || sections.length === 0) return;
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
currentSection.textContent = entry.target.textContent.trim();
}
});
}, {
rootMargin: '-20% 0px -70% 0px'
});
sections.forEach(section => observer.observe(section));
}
// Navigation functionality
function navigateToPage(filename) {
window.location.href = filename;
}
function getPageNavTarget(button) {
const onclick = button.getAttribute('onclick');
if (!onclick) return null;
const match = onclick.match(/navigateToPage\('([^']+)'\)/);
return match ? match[1] : null;
}
function triggerPageNavButton(button) {
if (!button || button.classList.contains('nav-btn-disabled')) return;
const target = getPageNavTarget(button);
if (target) navigateToPage(target);
}
function setupPageNavKeyboard() {
const pageNav = document.querySelector('.page-nav');
if (!pageNav) return;
const buttons = pageNav.querySelectorAll('button.nav-btn');
if (buttons.length < 2) return;
const prevBtn = buttons[0];
const nextBtn = buttons[1];
document.addEventListener('keydown', function(event) {
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
const target = event.target;
const tagName = target.tagName;
if (tagName === 'INPUT' || tagName === 'TEXTAREA' || tagName === 'SELECT' || target.isContentEditable) {
return;
}
if (event.key === 'ArrowLeft') {
if (prevBtn.classList.contains('nav-btn-disabled')) return;
event.preventDefault();
triggerPageNavButton(prevBtn);
} else if (event.key === 'ArrowRight') {
if (nextBtn.classList.contains('nav-btn-disabled')) return;
event.preventDefault();
triggerPageNavButton(nextBtn);
}
});
}
// Load saved preferences and initialize on page load
document.addEventListener('DOMContentLoaded', function() {
// Saved preference, else whatever the OS asks for
applyTheme(preferredTheme());
// Load saved sidebar preference
const savedSidebarCollapsed = localStorage.getItem('nimony-sidebar-collapsed');
const sidebar = document.getElementById('rightSidebar');
const toggleBtn = document.getElementById('sidebarToggle');
if (savedSidebarCollapsed === 'true' && sidebar && toggleBtn) {
sidebar.classList.add('collapsed');
toggleBtn.style.display = 'block';
}
// Initialize file overview
updateFileOverview();
setupIntersectionObserver();
setupPageNavKeyboard();
// Update current section on scroll
window.addEventListener('scroll', updateCurrentSection);
});