From 5124bd703f8044e2c30df86167cb77cf60396b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mark=20Randall=20Havens=20=E2=96=B3=20The=20Empathic=20Tec?= =?UTF-8?q?hnologist=20=E2=9F=81=20Doctor=20Who=2042?= Date: Sat, 8 Nov 2025 15:24:49 -0600 Subject: [PATCH] Update generate-index.mjs --- tools/generate-index.mjs | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/tools/generate-index.mjs b/tools/generate-index.mjs index e0727e1..445cdcd 100755 --- a/tools/generate-index.mjs +++ b/tools/generate-index.mjs @@ -1,10 +1,8 @@ #!/usr/bin/env node import { promises as fs } from "fs"; import path from "path"; -import { fileURLToPath } from "url"; -const __dirname = path.dirname(fileURLToPath(import.meta.url)); -const ROOT = path.resolve(__dirname, "../public"); +const ROOT = "public"; const OUT = path.join(ROOT, "index.json"); const STATIC_TOPLEVEL = new Set(["about", "contact", "legal"]); const MAX_BYTES = 64 * 1024; @@ -13,6 +11,7 @@ function dateFromName(name) { const m = name.match(/^(\d{4}-\d{2}-\d{2})/); return m ? new Date(m[0]).getTime() : null; } + async function readHead(abs) { const fh = await fs.open(abs, "r"); const buf = Buffer.alloc(MAX_BYTES); @@ -20,9 +19,10 @@ async function readHead(abs) { await fh.close(); return buf.slice(0, bytesRead).toString("utf8"); } + function parseTitle(raw, ext) { - if (ext === ".md") return raw.match(/^\s*#\s+(.+?)\s*$/m)?.[1]?.trim(); - if (ext === ".html") return raw.match(/]*>([^<]+)<\/title>/i)?.[1]?.trim(); + if (ext === ".md") return raw.match(/^\s*#\s+(.+?)\s*$/m)?.[1].trim(); + if (ext === ".html") return raw.match(/]*>([^<]+)<\/title>/i)?.[1].trim(); return null; } @@ -35,25 +35,21 @@ async function walk(relBase = "") { if (e.name.startsWith(".")) continue; const rel = path.posix.join(relBase, e.name); const absPath = path.join(ROOT, rel); - if (e.isDirectory()) { const top = rel.split("/")[0]; if (STATIC_TOPLEVEL.has(top)) continue; - dir.children.push(await walk(rel)); + const child = await walk(rel); + dir.children.push(child); continue; } const ext = path.posix.extname(e.name).toLowerCase(); if (![".md", ".html"].includes(ext)) continue; - const st = await fs.stat(absPath); - let title = e.name; - try { - const raw = await readHead(absPath); - title = parseTitle(raw, ext) || e.name; - } catch { /* ignore parse errors */ } - + const raw = await readHead(absPath); + const title = parseTitle(raw, ext) || e.name; const mtime = dateFromName(e.name) ?? st.mtimeMs; + dir.children.push({ type: "file", name: e.name, @@ -64,9 +60,6 @@ async function walk(relBase = "") { mtime }); } - - // newest-first inside each folder for nicer UX - dir.children.sort((a, b) => (b.mtime ?? 0) - (a.mtime ?? 0)); return dir; } @@ -74,18 +67,12 @@ async function walk(relBase = "") { try { const tree = await walk(); const flat = []; - (function flatten(n) { - for (const c of n.children) { - if (c.type === "file") flat.push(c); - else flatten(c); - } - })(tree); - + (function flatten(n) { for (const c of n.children) { c.type === "file" ? flat.push(c) : flatten(c); } })(tree); const sections = [...new Set(flat.map(f => f.path.split("/")[0]))]; await fs.writeFile(OUT, JSON.stringify({ tree: tree.children, flat, sections }, null, 2)); - console.log(`✅ index.json built with ${flat.length} files across ${sections.length} sections.`); + console.log(`index.json built with ${flat.length} files across ${sections.length} sections.`); } catch (e) { - console.error("❌ Build failed:", e); + console.error("Build failed:", e); process.exit(1); } })(); \ No newline at end of file