Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
483 lines (413 loc) · 17.1 KB
/
Copy pathscript.js
File metadata and controls
483 lines (413 loc) · 17.1 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// 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);
}
// Mark the nav chip for the page currently open, so the nav bar reads as a
// tab bar. Every page's nav is generated from a different template (nimdoc.cfg
// twice, multipage.nim, build.nim's dagon wrapper) and sits at a different
// depth under site/, so the target is resolved against the current URL rather
// than string-compared.
function markCurrentPageNav() {
const buttons = document.querySelectorAll('.nav-controls button.nav-btn');
buttons.forEach(function(button) {
const target = getPageNavTarget(button);
if (!target) return;
const targetUrl = new URL(target, window.location.href);
// A directory URL is served by its index.html; without this the Home
// chip would never light up when the site is visited at its root.
const here = window.location.pathname.endsWith('/')
? window.location.pathname + 'index.html'
: window.location.pathname;
let current = targetUrl.pathname === here;
// The library index stands in for every module page below it.
if (!current && targetUrl.pathname.endsWith('/theindex.html')) {
const dir = targetUrl.pathname.slice(0, -'theindex.html'.length);
current = here.startsWith(dir);
}
if (current) {
button.classList.add('nav-btn-current');
button.setAttribute('aria-current', 'page');
}
});
}
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);
}
});
}
// ---------------------------------------------------------------------------
// Nightly download links.
//
// nightly.yml cuts ONE release per nimony commit, tagged
// `nightly-<version>-<short sha>`, with four assets named
// `nimony-<version>-<short>-<os>_<cpu>.<ext>`. There is deliberately no
// rolling "latest" tag — a URL someone tested keeps pointing at the build they
// tested — so the newest one has to be looked up. The `[data-nightly]` links
// therefore ship pointing at the releases page and are upgraded in place to
// direct asset URLs once the API answers. If the request fails, is rate
// limited, or JavaScript is off, the panel keeps working: the fallback href is
// the releases page, which is where the manual answer lives anyway.
// ---------------------------------------------------------------------------
const NIGHTLY_RELEASES_API =
'https://api.github.com/repos/nim-lang/nimony-website/releases?per_page=10';
function nightlyAssetPlatform(name) {
// `nimony-0.6.2-abc123def-linux_amd64.tar.xz` -> `linux_amd64`
const match = name.match(/-((?:linux|macos|windows)_(?:amd64|arm64))\./);
return match ? match[1] : null;
}
function applyNightlyRelease(release) {
const assets = {};
(release.assets || []).forEach(function(asset) {
const platform = nightlyAssetPlatform(asset.name);
if (platform) assets[platform] = asset;
});
let linked = 0;
document.querySelectorAll('a[data-nightly]').forEach(function(link) {
const asset = assets[link.getAttribute('data-nightly')];
if (!asset) return;
link.href = asset.browser_download_url;
link.title = asset.name;
linked++;
});
if (linked === 0) return false;
// `nightly-0.6.2-abc123def` -> version and commit, without trusting the
// release title, which is free text.
const tag = (release.tag_name || '').match(/^nightly-([0-9.]+)-([0-9a-f]+)$/);
const published = release.published_at
? new Date(release.published_at).toISOString().slice(0, 10)
: null;
document.querySelectorAll('[data-nightly-version]').forEach(function(el) {
if (!tag) return;
el.textContent = 'Nimony ' + tag[1] + ' — built from master @ ' +
tag[2] + (published ? ' on ' + published : '');
});
return true;
}
function loadNightlyLinks() {
if (!document.querySelector('a[data-nightly]')) return;
fetch(NIGHTLY_RELEASES_API, { headers: { Accept: 'application/vnd.github+json' } })
.then(function(response) {
if (!response.ok) throw new Error('HTTP ' + response.status);
return response.json();
})
.then(function(releases) {
if (!Array.isArray(releases)) return;
// The API orders by creation, which is NOT the order the archives
// appear in: a run that created its release and then failed to
// upload sits at the top of the list with zero assets, and a
// re-run of an older commit creates nothing new. So: keep only
// nightly tags, sort by publication date, and take the newest one
// whose assets actually cover the platforms on this page.
releases
.filter(function(release) {
return /^nightly-/.test(release.tag_name || '') &&
(release.assets || []).length > 0;
})
.sort(function(a, b) {
return Date.parse(b.published_at || b.created_at || 0) -
Date.parse(a.published_at || a.created_at || 0);
})
.some(applyNightlyRelease);
})
.catch(function() {
// Keep the fallback links. Nothing to report to the reader.
});
}
// ---------------------------------------------------------------------------
// Tab panels (`[data-tabs]`, the "Nimony for…" box on the home page).
//
// The markup ships with every pane visible and the tab bar `hidden`, so the
// content reads as a plain list without JavaScript. Here the bar is revealed
// and all panes but the selected one are hidden. Keyboard handling follows the
// WAI-ARIA tabs pattern: only the selected tab is in the tab order, the arrow
// keys and Home/End move between tabs and select as they go.
// ---------------------------------------------------------------------------
function setupTabs() {
document.querySelectorAll('[data-tabs]').forEach(function(panel) {
const tablist = panel.querySelector('[role="tablist"]');
if (!tablist) return;
const tabs = Array.from(tablist.querySelectorAll('[role="tab"]'));
if (tabs.length === 0) return;
function select(tab, focus) {
tabs.forEach(function(t) {
const selected = t === tab;
t.setAttribute('aria-selected', selected ? 'true' : 'false');
t.tabIndex = selected ? 0 : -1;
const pane = document.getElementById(t.getAttribute('aria-controls'));
if (pane) pane.hidden = !selected;
});
if (focus) tab.focus();
}
tabs.forEach(function(tab, i) {
tab.addEventListener('click', function() { select(tab, false); });
tab.addEventListener('keydown', function(event) {
let next = null;
if (event.key === 'ArrowRight') next = tabs[(i + 1) % tabs.length];
else if (event.key === 'ArrowLeft') next = tabs[(i - 1 + tabs.length) % tabs.length];
else if (event.key === 'Home') next = tabs[0];
else if (event.key === 'End') next = tabs[tabs.length - 1];
if (!next) return;
event.preventDefault();
select(next, true);
});
});
const initial = tabs.find(function(t) {
return t.getAttribute('aria-selected') === 'true';
}) || tabs[0];
select(initial, false);
tablist.hidden = false;
panel.classList.add('spice-ready');
});
}
// 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();
markCurrentPageNav();
loadNightlyLinks();
setupTabs();
// Update current section on scroll
window.addEventListener('scroll', updateCurrentSection);
});