Update app.js
This commit is contained in:
parent
97e054517f
commit
58fd4a1c51
1 changed files with 178 additions and 121 deletions
299
public/app.js
299
public/app.js
|
|
@ -1,179 +1,236 @@
|
||||||
|
/* ============================================================
|
||||||
|
Self-Organizing Static Site Framework v2.3.2
|
||||||
|
============================================================ */
|
||||||
|
|
||||||
let INDEX, CURRENT_PATH = null, PATH_TO_EL = new Map();
|
let INDEX, CURRENT_PATH = null, PATH_TO_EL = new Map();
|
||||||
const treeEl = document.getElementById("tree");
|
|
||||||
const mdView = document.getElementById("mdView");
|
const treeEl = document.getElementById("tree");
|
||||||
const htmlView = document.getElementById("htmlView");
|
const mdView = document.getElementById("mdView");
|
||||||
const metaLine = document.getElementById("meta");
|
const htmlView = document.getElementById("htmlView");
|
||||||
const sortSel = document.getElementById("sort");
|
const metaLine = document.getElementById("meta");
|
||||||
|
const sortSel = document.getElementById("sort");
|
||||||
const filterSel = document.getElementById("filter");
|
const filterSel = document.getElementById("filter");
|
||||||
const searchBox = document.getElementById("search");
|
const searchBox = document.getElementById("search");
|
||||||
const prevBtn = document.getElementById("prev");
|
const prevBtn = document.getElementById("prev");
|
||||||
const nextBtn = document.getElementById("next");
|
const nextBtn = document.getElementById("next");
|
||||||
const sidebar = document.querySelector(".sidebar");
|
const sidebar = document.querySelector(".sidebar");
|
||||||
const navToggle = document.getElementById("navToggle");
|
const navToggle = document.getElementById("navToggle");
|
||||||
const overlay = document.querySelector(".overlay");
|
const overlay = document.querySelector(".overlay");
|
||||||
|
|
||||||
|
/* --- Navigation toggle --- */
|
||||||
navToggle.addEventListener("click", () => sidebar.classList.toggle("open"));
|
navToggle.addEventListener("click", () => sidebar.classList.toggle("open"));
|
||||||
overlay.addEventListener("click", () => sidebar.classList.remove("open"));
|
overlay.addEventListener("click", () => sidebar.classList.remove("open"));
|
||||||
|
|
||||||
|
/* --- Index load --- */
|
||||||
async function loadIndex() {
|
async function loadIndex() {
|
||||||
const res = await fetch("/index.json", { cache: "no-store" });
|
const res = await fetch("/index.json", { cache: "no-store" });
|
||||||
INDEX = await res.json();
|
INDEX = await res.json();
|
||||||
populateFilters();
|
populateFilters();
|
||||||
rebuildTree();
|
rebuildTree();
|
||||||
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;
|
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;
|
||||||
|
|
||||||
openPath(init);
|
openPath(init);
|
||||||
}
|
}
|
||||||
|
|
||||||
function populateFilters() {
|
function populateFilters() {
|
||||||
filterSel.innerHTML = '<option value="all">All</option>';
|
filterSel.innerHTML = '<option value="all">All</option>';
|
||||||
for (const cat of INDEX.sections) {
|
for (const cat of INDEX.sections) {
|
||||||
const opt = document.createElement("option");
|
const o = document.createElement("option");
|
||||||
opt.value = opt.textContent = cat;
|
o.value = o.textContent = cat;
|
||||||
filterSel.appendChild(opt);
|
filterSel.appendChild(o);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* --- Tree build --- */
|
||||||
function rebuildTree() {
|
function rebuildTree() {
|
||||||
treeEl.innerHTML = "";
|
treeEl.innerHTML = "";
|
||||||
PATH_TO_EL.clear();
|
PATH_TO_EL.clear();
|
||||||
const filter = filterSel.value;
|
const filter = filterSel.value;
|
||||||
const sort = sortSel.value;
|
const sort = sortSel.value;
|
||||||
const query = searchBox.value.trim().toLowerCase();
|
const query = searchBox.value.trim().toLowerCase();
|
||||||
const root = { type: "dir", children: INDEX.tree };
|
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)));
|
const pruned = filterTree(root, f =>
|
||||||
|
(filter==="all" || f.path.split("/")[0]===filter) &&
|
||||||
|
(!query || (f.title||f.name).toLowerCase().includes(query))
|
||||||
|
);
|
||||||
sortDir(pruned, sort);
|
sortDir(pruned, sort);
|
||||||
for (const c of pruned.children) treeEl.appendChild(renderNode(c));
|
for (const c of pruned.children) treeEl.appendChild(renderNode(c));
|
||||||
treeEl.querySelectorAll(".dir").forEach(d => d.classList.add("open"));
|
treeEl.querySelectorAll(".dir").forEach(d => d.classList.add("open"));
|
||||||
}
|
}
|
||||||
|
|
||||||
function filterTree(node, keep) {
|
function filterTree(node, keep) {
|
||||||
if (node.type === "file") return keep(node) ? node : null;
|
if (node.type === "file") return keep(node) ? node : null;
|
||||||
const kids = node.children.map(c => filterTree(c, keep)).filter(Boolean);
|
const kids = node.children.map(c=>filterTree(c,keep)).filter(Boolean);
|
||||||
return kids.length ? { ...node, children: kids } : null;
|
return kids.length ? {...node, children:kids} : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sortDir(node, sort) {
|
function sortDir(node, sort) {
|
||||||
const cmp = sort === "name" ? (a, b) => a.name.localeCompare(b.name) :
|
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;
|
: sort==="old" ? (a,b)=>a.mtime-b.mtime
|
||||||
node.children.sort((a, b) => (a.type === "dir" && b.type !== "dir") ? -1 : (a.type !== "dir" && b.type === "dir") ? 1 : cmp(a, b));
|
: (a,b)=>b.mtime-a.mtime;
|
||||||
node.children.forEach(c => c.type === "dir" && sortDir(c, sort));
|
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));
|
||||||
}
|
}
|
||||||
function renderNode(node) {
|
|
||||||
if (node.type === "dir") {
|
function renderNode(n) {
|
||||||
const div = document.createElement("div");
|
if (n.type==="dir") {
|
||||||
div.className = "dir";
|
const d = document.createElement("div");
|
||||||
div.setAttribute("aria-expanded", "false");
|
d.className="dir"; d.setAttribute("aria-expanded","false");
|
||||||
const lbl = document.createElement("span");
|
const lbl=document.createElement("span");
|
||||||
lbl.className = "label";
|
lbl.className="label"; lbl.textContent=n.name||"/";
|
||||||
lbl.textContent = node.name || "/";
|
lbl.addEventListener("click",()=>{
|
||||||
lbl.addEventListener("click", () => {
|
const idx=n.children.find(c=>c.type==="file"&&/^index\.(md|html)$/i.test(c.name));
|
||||||
const idx = node.children.find(c => c.type === "file" && /^index\.(md|html)$/i.test(c.name));
|
if(idx) openPath(idx.path); else d.classList.toggle("open");
|
||||||
if (idx) openPath(idx.path);
|
|
||||||
else div.classList.toggle("open");
|
|
||||||
});
|
});
|
||||||
div.appendChild(lbl);
|
d.appendChild(lbl);
|
||||||
const kids = document.createElement("div");
|
const kids=document.createElement("div");
|
||||||
kids.className = "children";
|
kids.className="children";
|
||||||
node.children.forEach(c => kids.appendChild(renderNode(c)));
|
n.children.forEach(c=>kids.appendChild(renderNode(c)));
|
||||||
div.appendChild(kids);
|
d.appendChild(kids);
|
||||||
return div;
|
return d;
|
||||||
}
|
}
|
||||||
const a = document.createElement("a");
|
const a=document.createElement("a");
|
||||||
a.className = "file";
|
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.innerHTML=`${n.pinned?'📌 ':''}${iconForExt(n.ext)} ${n.title}
|
||||||
a.addEventListener("click", e => { e.preventDefault(); openPath(node.path); });
|
<span class="meta">(${fmtDate(n.mtime)} · ${n.name})</span>`;
|
||||||
PATH_TO_EL.set(node.path, a);
|
a.addEventListener("click",e=>{e.preventDefault();openPath(n.path);});
|
||||||
|
PATH_TO_EL.set(n.path,a);
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
function iconForExt(ext) { return ext === ".md" ? "📝" : "🧩"; }
|
|
||||||
function fmtDate(ms) { return new Date(ms).toISOString().slice(0, 10); }
|
function iconForExt(ext){return ext===".md"?"📝":"🧩";}
|
||||||
function findDir(path) {
|
function fmtDate(ms){return new Date(ms).toISOString().slice(0,10);}
|
||||||
path = path.replace(/\/$/, '');
|
|
||||||
function search(node) {
|
/* --- Path openers --- */
|
||||||
if (node.type === "dir" && node.path === path) return node;
|
function findDir(p){
|
||||||
for (const c of node.children || []) {
|
p=p.replace(/\/$/,'');
|
||||||
const found = search(c);
|
function search(n){
|
||||||
if (found) return found;
|
if(n.type==="dir"&&n.path===p) return n;
|
||||||
}
|
for(const c of n.children||[]){const f=search(c);if(f)return f;}
|
||||||
}
|
}
|
||||||
return search({ children: INDEX.tree });
|
return search({children:INDEX.tree});
|
||||||
}
|
}
|
||||||
async function openPath(path) {
|
|
||||||
if (path === CURRENT_PATH) return;
|
async function openPath(path){
|
||||||
CURRENT_PATH = path;
|
if(path===CURRENT_PATH) return;
|
||||||
if (location.hash !== `#=${path}`) history.pushState(null, "", `#=${path}`);
|
CURRENT_PATH=path;
|
||||||
let f = INDEX.flat.find(x => x.path === path);
|
if(location.hash!==`#=${path}`) history.pushState(null,"",`#=${path}`);
|
||||||
if (!f) {
|
|
||||||
const dir = findDir(path);
|
let f=INDEX.flat.find(x=>x.path===path);
|
||||||
if (dir) {
|
if(!f){
|
||||||
const idx = dir.children.find(c => c.type === "file" && /^index\.(md|html)$/i.test(c.name));
|
const dir=findDir(path);
|
||||||
if (idx) return openPath(idx.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);
|
||||||
}
|
}
|
||||||
metaLine.textContent = "Path not found: " + path;
|
metaLine.textContent="Path not found: "+path;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
metaLine.textContent = `${f.pinned ? "📌 " : ""}${fmtDate(f.mtime)} • ${f.name}`;
|
|
||||||
if (f.ext === ".md") await renderMarkdown(f.path);
|
metaLine.textContent=`${f.pinned?"📌 ":""}${fmtDate(f.mtime)} • ${f.name}`;
|
||||||
|
if(f.ext===".md") await renderMarkdown(f.path);
|
||||||
else renderHTML(f.path);
|
else renderHTML(f.path);
|
||||||
setActive(path);
|
setActive(path);
|
||||||
updatePager();
|
updatePager();
|
||||||
if (window.innerWidth < 900) sidebar.classList.remove("open");
|
if(window.innerWidth<900) sidebar.classList.remove("open");
|
||||||
}
|
}
|
||||||
async function renderMarkdown(path) {
|
|
||||||
mdView.style.display = "none";
|
/* --- Markdown renderer (v2.3.2 fix) --- */
|
||||||
const res = await fetch("/" + path);
|
async function renderMarkdown(path){
|
||||||
if (!res.ok) { mdView.innerHTML = "<p>File not found: " + path + "</p>"; requestAnimationFrame(() => mdView.style.display = "block"); return; }
|
mdView.innerHTML="<p style='color:var(--muted);font-style:italic;'>Loading…</p>";
|
||||||
const text = await res.text();
|
htmlView.style.display="none";
|
||||||
let html = text.replace(/&/g, '&').replace(/</g, '<'); // Default fallback
|
mdView.style.display="block";
|
||||||
let usedFallback = true;
|
|
||||||
if (window.marked) {
|
try{
|
||||||
html = window.marked.parse(text);
|
const res=await fetch("/"+path);
|
||||||
usedFallback = false;
|
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.classList.add("fade-in");
|
||||||
|
mdView.style.display="block";
|
||||||
|
});
|
||||||
|
|
||||||
|
if(usedFallback) console.warn("Markdown rendered as plain text (marked.js missing).");
|
||||||
|
}catch(e){
|
||||||
|
mdView.innerHTML=`<p style='color:red;'>${e.message}</p>`;
|
||||||
}
|
}
|
||||||
let safe = html;
|
|
||||||
if (window.DOMPurify) safe = window.DOMPurify.sanitize(html);
|
|
||||||
mdView.innerHTML = safe;
|
|
||||||
requestAnimationFrame(() => { mdView.style.display = "block"; htmlView.style.display = "none"; });
|
|
||||||
if (usedFallback) console.warn("Markdown rendered as plain text: marked.js not loaded. Check CDN/SRI.");
|
|
||||||
}
|
}
|
||||||
function renderHTML(path) {
|
|
||||||
htmlView.src = "/" + path;
|
/* --- HTML viewer --- */
|
||||||
htmlView.style.display = "block";
|
function renderHTML(path){
|
||||||
mdView.style.display = "none";
|
htmlView.src="/"+path;
|
||||||
|
htmlView.style.display="block";
|
||||||
|
mdView.style.display="none";
|
||||||
}
|
}
|
||||||
function setActive(path) {
|
|
||||||
document.querySelectorAll(".file.active").forEach(el => el.classList.remove("active"));
|
/* --- Active / Pager --- */
|
||||||
const el = PATH_TO_EL.get(path);
|
function setActive(path){
|
||||||
if (el) {
|
document.querySelectorAll(".file.active").forEach(el=>el.classList.remove("active"));
|
||||||
|
const el=PATH_TO_EL.get(path);
|
||||||
|
if(el){
|
||||||
el.classList.add("active");
|
el.classList.add("active");
|
||||||
let p = el.parentElement;
|
let p=el.parentElement;
|
||||||
while (p && p !== treeEl) {
|
while(p&&p!==treeEl){
|
||||||
if (p.classList.contains("children")) p.parentElement.classList.add("open");
|
if(p.classList.contains("children")) p.parentElement.classList.add("open");
|
||||||
p = p.parentElement;
|
p=p.parentElement;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function updatePager() {
|
|
||||||
const query = searchBox.value.trim().toLowerCase();
|
function updatePager(){
|
||||||
const list = INDEX.flat.filter(f => (filterSel.value === "all" || f.path.split("/")[0] === filterSel.value) && (!query || f.title.toLowerCase().includes(query)));
|
const q=searchBox.value.trim().toLowerCase();
|
||||||
const cmp = sortSel.value === "name" ? (a, b) => a.name.localeCompare(b.name) :
|
const list=INDEX.flat.filter(f=>
|
||||||
sortSel.value === "old" ? (a, b) => a.mtime - b.mtime : (a, b) => b.mtime - a.mtime;
|
(filterSel.value==="all"||f.path.split("/")[0]===filterSel.value)&&
|
||||||
|
(!q||f.title.toLowerCase().includes(q))
|
||||||
|
);
|
||||||
|
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;
|
||||||
list.sort(cmp);
|
list.sort(cmp);
|
||||||
const i = list.findIndex(x => x.path === CURRENT_PATH);
|
const i=list.findIndex(x=>x.path===CURRENT_PATH);
|
||||||
prevBtn.disabled = i <= 0;
|
prevBtn.disabled=i<=0;
|
||||||
nextBtn.disabled = i >= list.length - 1 || i < 0;
|
nextBtn.disabled=i>=list.length-1||i<0;
|
||||||
prevBtn.onclick = () => i > 0 && openPath(list[i - 1].path);
|
prevBtn.onclick=()=>i>0&&openPath(list[i-1].path);
|
||||||
nextBtn.onclick = () => i < list.length - 1 && openPath(list[i + 1].path);
|
nextBtn.onclick=()=>i<list.length-1&&openPath(list[i+1].path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* --- Search / Filter / Sort --- */
|
||||||
let searchTimer;
|
let searchTimer;
|
||||||
searchBox.addEventListener("input", () => { clearTimeout(searchTimer); searchTimer = setTimeout(rebuildTree, 300); });
|
searchBox.addEventListener("input",()=>{
|
||||||
sortSel.addEventListener("change", rebuildTree);
|
clearTimeout(searchTimer);
|
||||||
filterSel.addEventListener("change", rebuildTree);
|
searchTimer=setTimeout(rebuildTree,300);
|
||||||
document.body.addEventListener("click", e => {
|
});
|
||||||
const a = e.target.closest("a[href]");
|
sortSel.addEventListener("change",rebuildTree);
|
||||||
if (!a) return;
|
filterSel.addEventListener("change",rebuildTree);
|
||||||
const href = a.getAttribute("href");
|
|
||||||
if (href.startsWith("/") && !href.startsWith("//") && !a.target) {
|
/* --- Internal link interception --- */
|
||||||
|
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){
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
openPath(href.replace(/^\//, ""));
|
openPath(href.replace(/^\//,""));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
window.addEventListener("resize", () => { if (window.innerWidth < 900) sidebar.classList.remove("open"); });
|
|
||||||
window.addEventListener("DOMContentLoaded", loadIndex);
|
window.addEventListener("resize",()=>{ if(window.innerWidth<900) sidebar.classList.remove("open"); });
|
||||||
|
window.addEventListener("DOMContentLoaded",loadIndex);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue