2025-11-08 11:29:40 -06:00
|
|
|
let INDEX, CURRENT_PATH = null, PATH_TO_EL = new Map();
|
2025-11-08 12:43:17 -06:00
|
|
|
const treeEl = document.getElementById("tree");
|
|
|
|
|
const mdView = document.getElementById("mdView");
|
|
|
|
|
const htmlView = document.getElementById("htmlView");
|
|
|
|
|
const metaLine = document.getElementById("meta");
|
|
|
|
|
const sortSel = document.getElementById("sort");
|
2025-11-08 11:29:40 -06:00
|
|
|
const filterSel = document.getElementById("filter");
|
|
|
|
|
const searchBox = document.getElementById("search");
|
2025-11-08 12:43:17 -06:00
|
|
|
const prevBtn = document.getElementById("prev");
|
|
|
|
|
const nextBtn = document.getElementById("next");
|
|
|
|
|
const sidebar = document.querySelector(".sidebar");
|
2025-11-08 12:10:39 -06:00
|
|
|
const navToggle = document.getElementById("navToggle");
|
2025-11-08 12:43:17 -06:00
|
|
|
const overlay = document.querySelector(".overlay");
|
2025-11-08 12:32:47 -06:00
|
|
|
|
2025-11-08 12:10:39 -06:00
|
|
|
navToggle.addEventListener("click", () => sidebar.classList.toggle("open"));
|
2025-11-08 12:43:17 -06:00
|
|
|
overlay.addEventListener("click", () => sidebar.classList.remove("open"));
|
2025-11-08 12:10:39 -06:00
|
|
|
|
2025-11-08 12:12:47 -06:00
|
|
|
async function loadIndex() {
|
|
|
|
|
const res = await fetch("/index.json", { cache: "no-store" });
|
|
|
|
|
INDEX = await res.json();
|
|
|
|
|
populateFilters();
|
|
|
|
|
rebuildTree();
|
2025-11-08 12:43:17 -06:00
|
|
|
window.addEventListener("popstate", () => { const hp = location.hash.startsWith("#=") ? location.hash.slice(2) : null; if (hp) openPath(hp); });
|
|
|
|
|
const init = location.hash.startsWith("#=") ? location.hash.slice(2) : INDEX.flat.sort((a, b) => b.mtime - a.mtime)[0]?.path;
|
2025-11-08 12:12:47 -06:00
|
|
|
openPath(init);
|
|
|
|
|
}
|
|
|
|
|
function populateFilters() {
|
|
|
|
|
filterSel.innerHTML = '<option value="all">All</option>';
|
|
|
|
|
for (const cat of INDEX.sections) {
|
2025-11-08 12:43:17 -06:00
|
|
|
const opt = document.createElement("option");
|
|
|
|
|
opt.value = opt.textContent = cat;
|
|
|
|
|
filterSel.appendChild(opt);
|
2025-11-08 12:12:47 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
function rebuildTree() {
|
|
|
|
|
treeEl.innerHTML = "";
|
|
|
|
|
PATH_TO_EL.clear();
|
|
|
|
|
const filter = filterSel.value;
|
2025-11-08 12:43:17 -06:00
|
|
|
const sort = sortSel.value;
|
|
|
|
|
const query = searchBox.value.trim().toLowerCase();
|
|
|
|
|
const root = { type: "dir", children: INDEX.tree };
|
|
|
|
|
const pruned = filterTree(root, f => (filter === "all" || f.path.split("/")[0] === filter) && (!query || (f.title || f.name).toLowerCase().includes(query)));
|
2025-11-08 12:12:47 -06:00
|
|
|
sortDir(pruned, sort);
|
|
|
|
|
for (const c of pruned.children) treeEl.appendChild(renderNode(c));
|
|
|
|
|
treeEl.querySelectorAll(".dir").forEach(d => d.classList.add("open"));
|
|
|
|
|
}
|
|
|
|
|
function filterTree(node, keep) {
|
|
|
|
|
if (node.type === "file") return keep(node) ? node : null;
|
2025-11-08 12:43:17 -06:00
|
|
|
const kids = node.children.map(c => filterTree(c, keep)).filter(Boolean);
|
|
|
|
|
return kids.length ? { ...node, children: kids } : null;
|
2025-11-08 12:12:47 -06:00
|
|
|
}
|
|
|
|
|
function sortDir(node, sort) {
|
2025-11-08 12:43:17 -06:00
|
|
|
const cmp = sort === "name" ? (a, b) => a.name.localeCompare(b.name) :
|
|
|
|
|
sort === "old" ? (a, b) => a.mtime - b.mtime : (a, b) => b.mtime - a.mtime;
|
|
|
|
|
node.children.sort((a, b) => (a.type === "dir" && b.type !== "dir") ? -1 : (a.type !== "dir" && b.type === "dir") ? 1 : cmp(a, b));
|
|
|
|
|
node.children.forEach(c => c.type === "dir" && sortDir(c, sort));
|
2025-11-08 12:12:47 -06:00
|
|
|
}
|
2025-11-08 12:43:17 -06:00
|
|
|
function renderNode(node) {
|
|
|
|
|
if (node.type === "dir") {
|
|
|
|
|
const div = document.createElement("div");
|
|
|
|
|
div.className = "dir";
|
|
|
|
|
div.setAttribute("aria-expanded", "false");
|
|
|
|
|
const lbl = document.createElement("span");
|
|
|
|
|
lbl.className = "label";
|
|
|
|
|
lbl.textContent = node.name || "/";
|
|
|
|
|
lbl.addEventListener("click", () => {
|
|
|
|
|
const idx = node.children.find(c => c.type === "file" && /^index\.(md|html)$/i.test(c.name));
|
|
|
|
|
if (idx) openPath(idx.path);
|
|
|
|
|
else div.classList.toggle("open");
|
2025-11-08 12:12:47 -06:00
|
|
|
});
|
2025-11-08 12:43:17 -06:00
|
|
|
div.appendChild(lbl);
|
|
|
|
|
const kids = document.createElement("div");
|
|
|
|
|
kids.className = "children";
|
|
|
|
|
node.children.forEach(c => kids.appendChild(renderNode(c)));
|
|
|
|
|
div.appendChild(kids);
|
|
|
|
|
return div;
|
2025-11-08 12:12:47 -06:00
|
|
|
}
|
2025-11-08 12:43:17 -06:00
|
|
|
const a = document.createElement("a");
|
|
|
|
|
a.className = "file";
|
|
|
|
|
a.innerHTML = `${node.pinned ? '<span class="pin">📌</span>' : ''}${iconForExt(node.ext)} ${node.title} <span class="meta">(${fmtDate(node.mtime)} · ${node.name})</span>`;
|
|
|
|
|
a.addEventListener("click", e => { e.preventDefault(); openPath(node.path); });
|
|
|
|
|
PATH_TO_EL.set(node.path, a);
|
2025-11-08 12:12:47 -06:00
|
|
|
return a;
|
|
|
|
|
}
|
2025-11-08 12:43:17 -06:00
|
|
|
function iconForExt(ext) { return ext === ".md" ? "📝" : "🧩"; }
|
|
|
|
|
function fmtDate(ms) { return new Date(ms).toISOString().slice(0, 10); }
|
|
|
|
|
function findDir(path) {
|
|
|
|
|
path = path.replace(/\/$/, '');
|
|
|
|
|
function search(node) {
|
|
|
|
|
if (node.type === "dir" && node.path === path) return node;
|
|
|
|
|
for (const c of node.children || []) {
|
|
|
|
|
const found = search(c);
|
|
|
|
|
if (found) return found;
|
|
|
|
|
}
|
2025-11-08 12:12:47 -06:00
|
|
|
}
|
2025-11-08 12:43:17 -06:00
|
|
|
return search({ children: INDEX.tree });
|
2025-11-08 12:12:47 -06:00
|
|
|
}
|
2025-11-08 12:43:17 -06:00
|
|
|
async function openPath(path) {
|
|
|
|
|
if (path === CURRENT_PATH) return;
|
|
|
|
|
CURRENT_PATH = path;
|
|
|
|
|
if (location.hash !== `#=${path}`) history.pushState(null, "", `#=${path}`);
|
|
|
|
|
let f = INDEX.flat.find(x => x.path === path);
|
|
|
|
|
if (!f) {
|
|
|
|
|
const dir = findDir(path);
|
|
|
|
|
if (dir) {
|
|
|
|
|
const idx = dir.children.find(c => c.type === "file" && /^index\.(md|html)$/i.test(c.name));
|
|
|
|
|
if (idx) return openPath(idx.path);
|
2025-11-08 12:12:47 -06:00
|
|
|
}
|
2025-11-08 12:43:17 -06:00
|
|
|
metaLine.textContent = "Path not found: " + path;
|
2025-11-08 12:12:47 -06:00
|
|
|
return;
|
|
|
|
|
}
|
2025-11-08 12:43:17 -06:00
|
|
|
metaLine.textContent = `${f.pinned ? "📌 " : ""}${fmtDate(f.mtime)} • ${f.name}`;
|
|
|
|
|
if (f.ext === ".md") await renderMarkdown(f.path);
|
2025-11-08 12:12:47 -06:00
|
|
|
else renderHTML(f.path);
|
|
|
|
|
setActive(path);
|
|
|
|
|
updatePager();
|
2025-11-08 12:43:17 -06:00
|
|
|
if (window.innerWidth < 900) sidebar.classList.remove("open");
|
2025-11-08 12:12:47 -06:00
|
|
|
}
|
2025-11-08 12:43:17 -06:00
|
|
|
async function renderMarkdown(path) {
|
|
|
|
|
mdView.innerHTML = "<p style='color:var(--muted);font-style:italic;'>Loading…</p>";
|
|
|
|
|
mdView.style.display = "block";
|
|
|
|
|
htmlView.style.display = "none";
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch("/" + path);
|
|
|
|
|
if (!res.ok) throw new Error("File not found: " + path);
|
|
|
|
|
const text = await res.text();
|
|
|
|
|
let html = text.replace(/&/g, '&').replace(/</g, '<');
|
|
|
|
|
let usedFallback = true;
|
|
|
|
|
if (window.marked) {
|
|
|
|
|
html = window.marked.parse(text);
|
|
|
|
|
usedFallback = false;
|
|
|
|
|
}
|
|
|
|
|
let safe = html;
|
|
|
|
|
if (window.DOMPurify) safe = window.DOMPurify.sanitize(html);
|
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
|
mdView.innerHTML = safe;
|
|
|
|
|
mdView.scrollTop = 0;
|
2025-11-08 12:27:31 -06:00
|
|
|
mdView.classList.add("fade-in");
|
|
|
|
|
});
|
2025-11-08 12:43:17 -06:00
|
|
|
if (usedFallback) console.warn("Markdown rendered as plain text: marked.js not loaded. Check CDN or vendor locally.");
|
|
|
|
|
} catch (e) {
|
|
|
|
|
mdView.innerHTML = `<p style='color:red;'>${e.message}</p>`;
|
2025-11-08 12:10:39 -06:00
|
|
|
}
|
2025-11-08 11:31:02 -06:00
|
|
|
}
|
2025-11-08 12:43:17 -06:00
|
|
|
function renderHTML(path) {
|
|
|
|
|
htmlView.style.display = "none";
|
|
|
|
|
mdView.style.display = "none";
|
|
|
|
|
metaLine.innerHTML += " <span style='color:var(--muted);'>Loading…</span>";
|
|
|
|
|
htmlView.src = "/" + path;
|
|
|
|
|
htmlView.onload = () => {
|
|
|
|
|
htmlView.style.display = "block";
|
|
|
|
|
htmlView.classList.add("fade-in");
|
|
|
|
|
metaLine.innerHTML = metaLine.innerHTML.replace(" Loading…", "");
|
|
|
|
|
};
|
|
|
|
|
htmlView.onerror = () => metaLine.innerHTML += " <span style='color:red;'>Load failed.</span>";
|
2025-11-08 12:12:47 -06:00
|
|
|
}
|
2025-11-08 12:43:17 -06:00
|
|
|
function setActive(path) {
|
|
|
|
|
document.querySelectorAll(".file.active").forEach(el => el.classList.remove("active"));
|
|
|
|
|
const el = PATH_TO_EL.get(path);
|
|
|
|
|
if (el) {
|
2025-11-08 12:12:47 -06:00
|
|
|
el.classList.add("active");
|
2025-11-08 12:43:17 -06:00
|
|
|
let p = el.parentElement;
|
|
|
|
|
while (p && p !== treeEl) {
|
|
|
|
|
if (p.classList.contains("children")) p.parentElement.classList.add("open");
|
|
|
|
|
p = p.parentElement;
|
2025-11-08 12:12:47 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-08 12:43:17 -06:00
|
|
|
function updatePager() {
|
|
|
|
|
const query = searchBox.value.trim().toLowerCase();
|
|
|
|
|
const list = INDEX.flat.filter(f => (filterSel.value === "all" || f.path.split("/")[0] === filterSel.value) && (!query || f.title.toLowerCase().includes(query)));
|
|
|
|
|
const cmp = sortSel.value === "name" ? (a, b) => a.name.localeCompare(b.name) :
|
|
|
|
|
sortSel.value === "old" ? (a, b) => a.mtime - b.mtime : (a, b) => b.mtime - a.mtime;
|
2025-11-08 12:12:47 -06:00
|
|
|
list.sort(cmp);
|
2025-11-08 12:43:17 -06:00
|
|
|
const i = list.findIndex(x => x.path === CURRENT_PATH);
|
|
|
|
|
prevBtn.disabled = i <= 0;
|
|
|
|
|
nextBtn.disabled = i >= list.length - 1 || i < 0;
|
|
|
|
|
prevBtn.onclick = () => i > 0 && openPath(list[i - 1].path);
|
|
|
|
|
nextBtn.onclick = () => i < list.length - 1 && openPath(list[i + 1].path);
|
2025-11-08 12:12:47 -06:00
|
|
|
}
|
|
|
|
|
let searchTimer;
|
2025-11-08 12:43:17 -06:00
|
|
|
searchBox.addEventListener("input", () => { clearTimeout(searchTimer); searchTimer = setTimeout(rebuildTree, 300); });
|
|
|
|
|
sortSel.addEventListener("change", rebuildTree);
|
|
|
|
|
filterSel.addEventListener("change", rebuildTree);
|
|
|
|
|
document.body.addEventListener("click", e => {
|
|
|
|
|
const a = e.target.closest("a[href]");
|
|
|
|
|
if (!a) return;
|
|
|
|
|
const href = a.getAttribute("href");
|
|
|
|
|
if (href.startsWith("/") && !href.startsWith("//") && !a.target) {
|
2025-11-08 12:12:47 -06:00
|
|
|
e.preventDefault();
|
2025-11-08 12:43:17 -06:00
|
|
|
openPath(href.replace(/^\//, ""));
|
2025-11-08 12:12:47 -06:00
|
|
|
}
|
|
|
|
|
});
|
2025-11-08 12:43:17 -06:00
|
|
|
window.addEventListener("resize", () => { if (window.innerWidth < 900) sidebar.classList.remove("open"); });
|
|
|
|
|
window.addEventListener("DOMContentLoaded", loadIndex);
|