Spaces:
Running
Running
File size: 6,053 Bytes
0ed5922 | 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 | (() => {
document.addEventListener("DOMContentLoaded", () => {
// ---- Inject floating logos into .background ----
const bg = document.querySelector(".background");
if (bg && !bg.querySelector(".floating-logo")) {
const isPost = !!document.querySelector(".article");
const logoSrc = isPost ? "../logo.svg" : "logo.svg";
for (let i = 0; i < 5; i++) {
const img = document.createElement("img");
img.src = logoSrc;
img.className = "floating-logo";
img.alt = "";
img.setAttribute("aria-hidden", "true");
bg.appendChild(img);
}
}
// ---- Copy buttons on <pre> ----
document.querySelectorAll("pre").forEach((pre) => {
if (pre.querySelector(".copy-btn")) return;
const code = pre.querySelector("code") || pre;
const btn = document.createElement("button");
btn.className = "copy-btn";
btn.textContent = "Copy";
btn.addEventListener("click", async () => {
try {
await navigator.clipboard.writeText(code.innerText);
btn.textContent = "Copied!";
setTimeout(() => (btn.textContent = "Copy"), 1200);
} catch {
btn.textContent = "Error";
setTimeout(() => (btn.textContent = "Copy"), 1200);
}
});
pre.appendChild(btn);
});
// ---- Article-only features: sidebar + collapsible code ----
const article = document.querySelector(".article");
if (!article) return;
// Collect headings for TOC
const headings = article.querySelectorAll("h1, h2, h3");
const codeBlocks = article.querySelectorAll("pre");
// Determine filenames from preceding h2
const codeFiles = [];
codeBlocks.forEach((pre, idx) => {
let name = null;
let el = pre.previousElementSibling;
while (el) {
if (el.tagName === "H2" || el.tagName === "H3") {
const text = el.textContent.trim();
if (text.match(/\.\w{1,5}$/) || text.match(/\.\w{1,5}\s/)) {
name = text.replace(/\s*\(.*\)/, "").trim();
}
break;
}
el = el.previousElementSibling;
}
if (!name) name = `code-block-${idx + 1}.txt`;
codeFiles.push({ name, pre });
});
// Make code blocks collapsible
codeBlocks.forEach((pre, idx) => {
const wrapper = document.createElement("div");
wrapper.className = "code-collapsible";
const toggle = document.createElement("button");
toggle.className = "code-toggle";
const fileName = codeFiles[idx].name;
toggle.innerHTML = `<span class="code-toggle-arrow">▶</span> <span class="code-toggle-name">${fileName}</span>`;
toggle.setAttribute("aria-expanded", "false");
pre.parentNode.insertBefore(wrapper, pre);
wrapper.appendChild(toggle);
wrapper.appendChild(pre);
pre.classList.add("collapsed");
toggle.addEventListener("click", () => {
const expanded = pre.classList.toggle("collapsed");
toggle.setAttribute("aria-expanded", !expanded);
toggle.querySelector(".code-toggle-arrow").textContent = expanded ? "▶" : "▼";
});
});
// Build sidebar
const sidebar = document.createElement("nav");
sidebar.className = "article-sidebar";
// TOC section
const tocTitle = document.createElement("div");
tocTitle.className = "sidebar-title";
tocTitle.textContent = "Contents";
sidebar.appendChild(tocTitle);
const tocList = document.createElement("ul");
tocList.className = "sidebar-toc";
headings.forEach((h, i) => {
if (!h.id) h.id = "heading-" + i;
const li = document.createElement("li");
li.className = "toc-" + h.tagName.toLowerCase();
const a = document.createElement("a");
a.href = "#" + h.id;
a.textContent = h.textContent;
a.addEventListener("click", (e) => {
e.preventDefault();
h.scrollIntoView({ behavior: "smooth", block: "start" });
});
li.appendChild(a);
tocList.appendChild(li);
});
sidebar.appendChild(tocList);
// Files section
if (codeFiles.length > 0) {
const filesTitle = document.createElement("div");
filesTitle.className = "sidebar-title";
filesTitle.textContent = "Files";
sidebar.appendChild(filesTitle);
const fileList = document.createElement("ul");
fileList.className = "sidebar-files";
codeFiles.forEach(({ name, pre }) => {
const li = document.createElement("li");
const btn = document.createElement("button");
btn.className = "sidebar-file-btn";
btn.innerHTML = `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg> ${name}`;
btn.addEventListener("click", () => {
const code = pre.querySelector("code") || pre;
const text = code.innerText;
const blob = new Blob([text], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = name;
a.click();
URL.revokeObjectURL(url);
});
li.appendChild(btn);
fileList.appendChild(li);
});
sidebar.appendChild(fileList);
}
document.body.appendChild(sidebar);
// Highlight active TOC item on scroll
const tocLinks = tocList.querySelectorAll("a");
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
tocLinks.forEach((l) => l.classList.remove("active"));
const link = tocList.querySelector(`a[href="#${entry.target.id}"]`);
if (link) link.classList.add("active");
}
});
},
{ rootMargin: "-80px 0px -70% 0px" }
);
headings.forEach((h) => observer.observe(h));
});
})();
|