-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjavascript.js
More file actions
54 lines (43 loc) · 1.54 KB
/
javascript.js
File metadata and controls
54 lines (43 loc) · 1.54 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
document.addEventListener("DOMContentLoaded", function () {
// --- TOC Generator ---
const content = document.querySelector(".main-content");
const sidebarNav = document.getElementById("sidebar-nav");
const headings = content.querySelectorAll("h2, h3, h4");
headings.forEach((heading) => {
if (!heading.id) {
heading.id = heading.textContent.trim().toLowerCase().replace(/\s+/g, "-");
}
const link = document.createElement("a");
link.href = `#${heading.id}`;
link.textContent = heading.textContent;
const listItem = document.createElement("li");
listItem.appendChild(link);
if (heading.tagName === "H2") {
listItem.classList.add("indent-h2");
} else if (heading.tagName === "H3") {
listItem.classList.add("indent-h3");
} else if (heading.tagName === "H4") {
listItem.classList.add("indent-h4");
}
sidebarNav.appendChild(listItem);
});
// --- Sidebar Resizer ---
const sidebar = document.getElementById("sidebar");
const resizer = document.getElementById("resizer");
let isResizing = false;
resizer.addEventListener("mousedown", (e) => {
isResizing = true;
document.body.style.cursor = "ew-resize";
});
document.addEventListener("mousemove", (e) => {
if (!isResizing) return;
const newWidth = e.clientX;
if (newWidth > 150 && newWidth < 500) {
sidebar.style.width = `${newWidth}px`;
}
});
document.addEventListener("mouseup", () => {
isResizing = false;
document.body.style.cursor = "default";
});
});