Update generate-index.mjs

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

View file

@ -13,7 +13,6 @@ 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);
@ -21,26 +20,19 @@ 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") if (ext === ".html") return raw.match(/<title[^>]*>([^<]+)<\/title>/i)?.[1]?.trim();
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 = { const dir = { type: "dir", name: path.posix.basename(relBase) || "", path: relBase, children: [] };
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; // skip hidden if (e.name.startsWith(".")) continue;
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);
@ -59,9 +51,7 @@ async function walk(relBase = "") {
try { try {
const raw = await readHead(absPath); const raw = await readHead(absPath);
title = parseTitle(raw, ext) || e.name; title = parseTitle(raw, ext) || e.name;
} catch { } catch { /* ignore parse errors */ }
/* ignore parse errors */
}
const mtime = dateFromName(e.name) ?? st.mtimeMs; const mtime = dateFromName(e.name) ?? st.mtimeMs;
dir.children.push({ dir.children.push({
@ -71,11 +61,11 @@ 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 // newest-first inside each folder for nicer UX
dir.children.sort((a, b) => (b.mtime ?? 0) - (a.mtime ?? 0)); dir.children.sort((a, b) => (b.mtime ?? 0) - (a.mtime ?? 0));
return dir; return dir;
} }
@ -90,14 +80,10 @@ async function walk(relBase = "") {
else flatten(c); else flatten(c);
} }
})(tree); })(tree);
const sections = [...new Set(flat.map((f) => f.path.split("/")[0]))];
await fs.writeFile( const sections = [...new Set(flat.map(f => f.path.split("/")[0]))];
OUT, await fs.writeFile(OUT, JSON.stringify({ tree: tree.children, flat, sections }, null, 2));
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) { } catch (e) {
console.error("❌ Build failed:", e); console.error("❌ Build failed:", e);
process.exit(1); process.exit(1);