Update generate-index.mjs

This commit is contained in:
Mark Randall Havens △ The Empathic Technologist ⟁ Doctor Who 42 2025-11-08 14:34:15 -06:00 committed by GitHub
parent efdfd39560
commit eceb24d98c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,7 +1,10 @@
#!/usr/bin/env node #!/usr/bin/env node
import { promises as fs } from "fs"; import { promises as fs } from "fs";
import path from "path"; import path from "path";
const ROOT = "public"; import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.resolve(__dirname, "../public");
const OUT = path.join(ROOT, "index.json"); const OUT = path.join(ROOT, "index.json");
const STATIC_TOPLEVEL = new Set(["about", "contact", "legal"]); const STATIC_TOPLEVEL = new Set(["about", "contact", "legal"]);
const MAX_BYTES = 64 * 1024; const MAX_BYTES = 64 * 1024;
@ -10,6 +13,7 @@ function dateFromName(name) {
const m = name.match(/^(\d{4}-\d{2}-\d{2})/); const m = name.match(/^(\d{4}-\d{2}-\d{2})/);
return m ? new Date(m[0]).getTime() : null; return m ? new Date(m[0]).getTime() : null;
} }
async function readHead(abs) { async function readHead(abs) {
const fh = await fs.open(abs, "r"); const fh = await fs.open(abs, "r");
const buf = Buffer.alloc(MAX_BYTES); const buf = Buffer.alloc(MAX_BYTES);
@ -17,32 +21,48 @@ async function readHead(abs) {
await fh.close(); await fh.close();
return buf.slice(0, bytesRead).toString("utf8"); return buf.slice(0, bytesRead).toString("utf8");
} }
function parseTitle(raw, ext) { function parseTitle(raw, ext) {
if (ext === ".md") return raw.match(/^\s*#\s+(.+?)\s*$/m)?.[1].trim(); if (ext === ".md") return raw.match(/^\s*#\s+(.+?)\s*$/m)?.[1]?.trim();
if (ext === ".html") return raw.match(/<title[^>]*>([^<]+)<\/title>/i)?.[1].trim(); if (ext === ".html")
return raw.match(/<title[^>]*>([^<]+)<\/title>/i)?.[1]?.trim();
return null; return null;
} }
async function walk(relBase = "") { async function walk(relBase = "") {
const abs = path.join(ROOT, relBase); const abs = path.join(ROOT, relBase);
const entries = await fs.readdir(abs, { withFileTypes: true }); const entries = await fs.readdir(abs, { withFileTypes: true });
const dir = { type: "dir", name: path.posix.basename(relBase) || "", path: relBase, children: [] }; const dir = {
type: "dir",
name: path.posix.basename(relBase) || "",
path: relBase,
children: [],
};
for (const e of entries) { for (const e of entries) {
if (e.name.startsWith(".")) continue; if (e.name.startsWith(".")) continue; // skip hidden
const rel = path.posix.join(relBase, e.name); const rel = path.posix.join(relBase, e.name);
const absPath = path.join(ROOT, rel); const absPath = path.join(ROOT, rel);
if (e.isDirectory()) { if (e.isDirectory()) {
const top = rel.split("/")[0]; const top = rel.split("/")[0];
if (STATIC_TOPLEVEL.has(top)) continue; if (STATIC_TOPLEVEL.has(top)) continue;
const child = await walk(rel); dir.children.push(await walk(rel));
dir.children.push(child);
continue; continue;
} }
const ext = path.posix.extname(e.name).toLowerCase(); const ext = path.posix.extname(e.name).toLowerCase();
if (![".md", ".html"].includes(ext)) continue; if (![".md", ".html"].includes(ext)) continue;
const st = await fs.stat(absPath); const st = await fs.stat(absPath);
const raw = await readHead(absPath); let title = e.name;
const title = parseTitle(raw, ext) || e.name; try {
const raw = await readHead(absPath);
title = parseTitle(raw, ext) || e.name;
} catch {
/* ignore parse errors */
}
const mtime = dateFromName(e.name) ?? st.mtimeMs; const mtime = dateFromName(e.name) ?? st.mtimeMs;
dir.children.push({ dir.children.push({
type: "file", type: "file",
@ -51,9 +71,12 @@ async function walk(relBase = "") {
path: rel, path: rel,
ext, ext,
pinned: rel.startsWith("pinned/"), pinned: rel.startsWith("pinned/"),
mtime mtime,
}); });
} }
// sort newest first by mtime
dir.children.sort((a, b) => (b.mtime ?? 0) - (a.mtime ?? 0));
return dir; return dir;
} }
@ -61,12 +84,22 @@ async function walk(relBase = "") {
try { try {
const tree = await walk(); const tree = await walk();
const flat = []; 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) {
const sections = [...new Set(flat.map(f => f.path.split("/")[0]))]; for (const c of n.children) {
await fs.writeFile(OUT, JSON.stringify({ tree: tree.children, flat, sections }, null, 2)); if (c.type === "file") flat.push(c);
console.log("index.json built with", flat.length, "files across", sections.length, "sections."); else 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.`
);
} catch (e) { } catch (e) {
console.error("Build failed:", e); console.error("Build failed:", e);
process.exit(1); process.exit(1);
} }
})(); })();