-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
92 lines (80 loc) · 3.38 KB
/
Copy pathscript.js
File metadata and controls
92 lines (80 loc) · 3.38 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
document.addEventListener('DOMContentLoaded', () => {
/* copy to clipboard on every terminal block */
document.querySelectorAll('.term').forEach((term) => {
const bar = term.querySelector('.term-bar');
const body = term.querySelector('.term-body');
if (!bar || !body) return;
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'copy-btn';
btn.textContent = 'Copy';
bar.appendChild(btn);
btn.addEventListener('click', () => {
const lines = Array.from(body.querySelectorAll('.line'))
.filter((line) => line.querySelector('.prompt'))
.map((line) => {
const clone = line.cloneNode(true);
clone.querySelectorAll('.prompt').forEach((p) => p.remove());
return clone.textContent.replace(/\s+/g, ' ').trim();
});
const text = lines.length
? lines.join('\n')
: body.textContent.replace(/\s+/g, ' ').trim();
const finish = (label) => {
btn.textContent = label;
setTimeout(() => { btn.textContent = 'Copy'; }, 1500);
};
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(() => finish('Copied!')).catch(() => finish('Error'));
} else {
finish('Error');
}
});
});
/* mobile nav drawer */
const toggle = document.getElementById('navToggle');
const sidebar = document.getElementById('sidebar');
const overlay = document.getElementById('navOverlay');
const closeNav = () => {
sidebar.classList.remove('open');
overlay.classList.remove('show');
};
if (toggle && sidebar && overlay) {
toggle.addEventListener('click', () => {
sidebar.classList.toggle('open');
overlay.classList.toggle('show');
});
overlay.addEventListener('click', closeNav);
sidebar.querySelectorAll('a').forEach((a) => a.addEventListener('click', closeNav));
}
/* scrollspy: highlight current section in the TOC */
const links = Array.from(document.querySelectorAll('.toc-list a'));
const sections = links
.map((a) => document.querySelector(a.getAttribute('href')))
.filter(Boolean);
if ('IntersectionObserver' in window && sections.length) {
const io = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
const link = document.querySelector(`.toc-list a[href="#${entry.target.id}"]`);
if (!link) return;
links.forEach((l) => l.classList.remove('active'));
link.classList.add('active');
});
},
{ rootMargin: '-15% 0px -70% 0px', threshold: 0 }
);
sections.forEach((s) => io.observe(s));
}
/* back to top */
const backTop = document.getElementById('backTop');
if (backTop) {
window.addEventListener('scroll', () => {
backTop.classList.toggle('show', window.scrollY > 600);
});
backTop.addEventListener('click', () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}
});