-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
257 lines (214 loc) · 7.98 KB
/
Copy pathscript.js
File metadata and controls
257 lines (214 loc) · 7.98 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
// Theme switching functionality
function toggleTheme() {
const root = document.documentElement;
const button = document.querySelector('.theme-switcher');
if (root.getAttribute('data-theme') === 'dark') {
// Switch to light theme
root.removeAttribute('data-theme');
button.textContent = '🌙 Dark';
button.title = 'Switch to dark theme';
} else {
// Switch to dark theme
root.setAttribute('data-theme', 'dark');
button.textContent = '🌙 Light';
button.title = 'Switch to light theme';
}
// Save preference to localStorage
localStorage.setItem('nimony-theme', root.getAttribute('data-theme') || 'light');
}
// 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;
}
// Load saved preferences and initialize on page load
document.addEventListener('DOMContentLoaded', function() {
// Load saved theme preference
const savedTheme = localStorage.getItem('nimony-theme');
const button = document.querySelector('.theme-switcher');
if (savedTheme === 'dark') {
document.documentElement.setAttribute('data-theme', 'dark');
button.textContent = '🌙 Light';
button.title = 'Switch to light theme';
} else {
button.textContent = '🌙 Dark';
button.title = 'Switch to dark theme';
}
// 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.classList.add('collapsed');
toggleBtn.style.display = 'block';
}
// Initialize file overview
updateFileOverview();
setupIntersectionObserver();
// Update current section on scroll
window.addEventListener('scroll', updateCurrentSection);
});