thefoldwithin-earth/public/app.js

90 lines
2.4 KiB
JavaScript
Raw Normal View History

2025-11-08 09:06:40 -06:00
let INDEX = null;
let CURRENT_PATH = null;
const treeEl = document.getElementById("tree");
const metaEl = document.getElementById("meta");
2025-11-08 09:59:51 -06:00
const mdView = document.getElementById("mdView");
const htmlView = document.getElementById("htmlView");
const sortSel = document.getElementById("sortSel");
const filterSel = document.getElementById("filterSel");
const navToggle = document.getElementById("navToggle");
navToggle.addEventListener("click", () =>
document.querySelector(".sidebar").classList.toggle("open")
);
async function loadIndex() {
try {
const res = await fetch("index.json");
INDEX = await res.json();
} catch {
treeEl.innerHTML = "<p style='color:red'>index.json missing. run the build.</p>";
return;
2025-11-08 09:06:40 -06:00
}
2025-11-08 09:59:51 -06:00
rebuildTree();
autoOpenLatest();
2025-11-08 09:06:40 -06:00
}
2025-11-08 09:59:51 -06:00
function rebuildTree() {
treeEl.innerHTML = "";
const roots = ["pinned", "posts"];
for (const root of roots) {
const dir = INDEX.tree.find(d => d.name === root);
if (dir) {
const h = document.createElement("div");
h.className = "dir";
h.textContent = root;
treeEl.appendChild(h);
dir.children.forEach(f => treeEl.appendChild(renderFile(f)));
2025-11-08 09:06:40 -06:00
}
}
}
2025-11-08 09:59:51 -06:00
function renderFile(f) {
const a = document.createElement("a");
a.className = "file";
a.textContent = (f.pinned ? "📌 " : "") + f.name;
a.addEventListener("click", e => {
e.preventDefault();
openPath(f.path);
});
return a;
2025-11-08 09:06:40 -06:00
}
2025-11-08 09:59:51 -06:00
function autoOpenLatest() {
if (!INDEX.flat?.length) return;
const sorted = [...INDEX.flat].sort((a,b)=>b.mtime - a.mtime);
openPath(sorted[0].path);
2025-11-08 09:06:40 -06:00
}
2025-11-08 09:59:51 -06:00
async function openPath(path) {
if (path === CURRENT_PATH) return;
2025-11-08 09:06:40 -06:00
CURRENT_PATH = path;
2025-11-08 09:59:51 -06:00
const f = INDEX.flat.find(x => x.path === path);
if (!f) return;
2025-11-08 09:06:40 -06:00
2025-11-08 09:59:51 -06:00
metaEl.textContent = `${f.pinned ? "Pinned • " : ""}${new Date(f.mtime).toISOString().slice(0,10)}${f.name}`;
2025-11-08 09:06:40 -06:00
2025-11-08 09:59:51 -06:00
if (f.ext === ".md") await renderMarkdown(path);
2025-11-08 09:06:40 -06:00
else await renderHTML(path);
2025-11-08 09:59:51 -06:00
if (window.innerWidth < 900)
document.querySelector(".sidebar").classList.remove("open");
}
2025-11-08 09:06:40 -06:00
2025-11-08 09:59:51 -06:00
async function renderMarkdown(path) {
htmlView.style.display = "none";
mdView.style.display = "block";
const res = await fetch(path);
const text = await res.text();
const html = DOMPurify.sanitize(marked.parse(text));
mdView.innerHTML = html;
}
2025-11-08 09:06:40 -06:00
2025-11-08 09:59:51 -06:00
async function renderHTML(path) {
mdView.style.display = "none";
htmlView.style.display = "block";
htmlView.src = path;
2025-11-08 09:06:40 -06:00
}
2025-11-08 09:59:51 -06:00
window.addEventListener("DOMContentLoaded", loadIndex);