-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs.js
More file actions
188 lines (177 loc) Β· 6.13 KB
/
docs.js
File metadata and controls
188 lines (177 loc) Β· 6.13 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
// Sidebar highlighting and search for static HTML docs
const navLinks = document.querySelectorAll(".nav-link");
const searchInput = document.getElementById("search");
const mainContent = document.getElementById("main-content");
const sections = Array.from(mainContent.querySelectorAll("section"));
// Highlight active link on click and hashchange
function highlightActiveLink(hash) {
navLinks.forEach((link) => link.classList.remove("active"));
hash = hash || window.location.hash || "#introduction";
const active = document.querySelector(`.nav-link[href="${hash}"]`);
if (active) active.classList.add("active");
}
window.addEventListener("hashchange", () => highlightActiveLink());
navLinks.forEach((link) =>
link.addEventListener("click", function () {
highlightActiveLink(this.getAttribute("href"));
})
);
highlightActiveLink();
// Search/filter sidebar
searchInput.addEventListener("input", function () {
const val = this.value.toLowerCase();
navLinks.forEach((link) => {
const text = link.textContent.toLowerCase();
link.parentElement.style.display = text.includes(val) ? "" : "none";
});
});
// Smooth scroll to section on click
navLinks.forEach((link) => {
link.addEventListener("click", function (e) {
const targetId = link.getAttribute("href").slice(1);
const section = document.getElementById(targetId);
if (section) {
section.scrollIntoView({ behavior: "smooth" });
}
});
});
// Scrollspy: highlight sidebar as you scroll
mainContent.addEventListener("scroll", function () {
let currentSection = sections[0];
const scrollPos = mainContent.scrollTop + 80; // 80px offset for header
for (const section of sections) {
if (section.offsetTop <= scrollPos) {
currentSection = section;
}
}
highlightActiveLink("#" + currentSection.id);
});
// Also trigger scrollspy on page load
mainContent.dispatchEvent(new Event("scroll"));
// Add copy button to all code blocks
function addCopyButtons() {
// Add to <pre><code> blocks (existing logic)
document.querySelectorAll("pre > code").forEach((codeBlock) => {
const pre = codeBlock.parentElement;
if (pre.querySelector(".copy-btn")) return; // Only add once
const btn = document.createElement("button");
btn.className = "copy-btn";
btn.type = "button";
btn.title = "Copy code";
btn.innerHTML = "π";
btn.style.position = "absolute";
btn.style.top = "8px";
btn.style.right = "8px";
btn.style.background = "rgba(30,30,30,0.8)";
btn.style.color = "#fff";
btn.style.border = "none";
btn.style.borderRadius = "4px";
btn.style.padding = "0.2em 0.5em";
btn.style.cursor = "pointer";
btn.style.fontSize = "1em";
btn.style.zIndex = "2";
btn.addEventListener("click", function () {
navigator.clipboard.writeText(codeBlock.innerText).then(() => {
btn.innerHTML = "β
";
setTimeout(() => {
btn.innerHTML = "π";
}, 1200);
});
});
pre.style.position = "relative";
pre.appendChild(btn);
});
// Add to <pre> blocks that do NOT contain a <code> child
document.querySelectorAll("pre").forEach((pre) => {
// Skip if already handled above (has <code> child)
if (pre.querySelector("code")) return;
if (pre.querySelector(".copy-btn")) return; // Only add once
const btn = document.createElement("button");
btn.className = "copy-btn";
btn.type = "button";
btn.title = "Copy code";
btn.innerHTML = "π";
btn.style.position = "absolute";
btn.style.top = "8px";
btn.style.right = "8px";
btn.style.background = "rgba(30,30,30,0.8)";
btn.style.color = "#fff";
btn.style.border = "none";
btn.style.borderRadius = "4px";
btn.style.padding = "0.2em 0.5em";
btn.style.cursor = "pointer";
btn.style.fontSize = "1em";
btn.style.zIndex = "2";
btn.addEventListener("click", function () {
navigator.clipboard.writeText(pre.innerText).then(() => {
btn.innerHTML = "β
";
setTimeout(() => {
btn.innerHTML = "π";
}, 1200);
});
});
pre.style.position = "relative";
pre.appendChild(btn);
});
}
addCopyButtons();
document.getElementById("copyright-year").textContent =
new Date().getFullYear();
// Dark mode toggle logic
const darkToggles = document.querySelectorAll("#darkmode-toggle");
const html = document.documentElement;
function setTheme(theme) {
if (theme === "light") {
html.setAttribute("data-theme", "light");
darkToggles.forEach((btn) => (btn.textContent = "βοΈ"));
} else {
html.setAttribute("data-theme", "dark");
darkToggles.forEach((btn) => (btn.textContent = "π"));
}
localStorage.setItem("theme", theme);
}
function toggleTheme() {
const current = html.getAttribute("data-theme") || "dark";
setTheme(current === "dark" ? "light" : "dark");
}
darkToggles.forEach((btn) => btn.addEventListener("click", toggleTheme));
// On load, set theme from localStorage or default to dark
const savedTheme = localStorage.getItem("theme");
setTheme(savedTheme || "dark");
// Sidebar mobile toggle
const sidebarToggle = document.getElementById("sidebar-toggle");
const sidebar = document.querySelector(".sidebar");
const content = document.querySelector(".content");
const sidebarBackdrop = document.querySelector(".sidebar-backdrop");
function openSidebar() {
sidebar.classList.add("open");
document.body.classList.add("sidebar-open");
if (sidebarBackdrop) sidebarBackdrop.classList.remove("hide");
}
function closeSidebar() {
sidebar.classList.remove("open");
document.body.classList.remove("sidebar-open");
if (sidebarBackdrop) sidebarBackdrop.classList.add("hide");
}
sidebarToggle.addEventListener("click", function () {
if (sidebar.classList.contains("open")) {
closeSidebar();
} else {
openSidebar();
}
});
if (sidebarBackdrop) {
sidebarBackdrop.addEventListener("click", function () {
closeSidebar();
});
}
content.addEventListener("click", function (e) {
if (window.innerWidth <= 900 && sidebar.classList.contains("open")) {
closeSidebar();
}
});
document.addEventListener("keydown", function (e) {
if (e.key === "Escape" && sidebar.classList.contains("open")) {
closeSidebar();
}
});