Update generate-index.mjs

This commit is contained in:
Mark Randall Havens △ The Empathic Technologist ⟁ Doctor Who 42 2025-11-08 10:00:24 -06:00 committed by GitHub
parent 9f55ba2a7c
commit 8c28b66feb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,125 +1,64 @@
/**
* generate-index.mjs (v2.0.0)
* Scans /public/{pinned,posts} for .html/.md files, emits public/index.json
* Deterministic, POSIX paths, reverse-chron default ordering handled client-side.
*/
import { promises as fs } from "fs"; import { promises as fs } from "fs";
import path from "path"; import path from "path";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url)); const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PUBLIC_DIR = path.resolve(__dirname, "../public"); const PUBLIC = path.resolve(__dirname, "../public");
const ROOTS = ["pinned", "posts"]; const ROOTS = ["pinned", "posts"];
const ALLOWED = new Set([".html", ".md"]); const exts = [".html", ".md"];
const MAX_TITLE_BYTES = 64 * 1024;
const posix = path.posix;
async function statSafe(p) { try { return await fs.stat(p); } catch { return null; } }
function isHidden(rel) { return /(^|\/)\./.test(rel); } // hide dotfiles/dirs
function toPosix(rel) { return rel.split(path.sep).join("/"); }
async function readFirstChunk(abs) {
const fh = await fs.open(abs, "r");
const { size } = await fh.stat();
const len = Math.min(MAX_TITLE_BYTES, size);
const buf = Buffer.alloc(len);
await fh.read(buf, 0, len, 0);
await fh.close();
return buf.toString("utf8");
}
function parseTitleFromHTML(raw) {
const m = raw.match(/<title[^>]*>([\s\S]*?)<\/title>/i);
return m ? m[1].trim() : null;
}
function parseTitleFromMD(raw) {
const m = raw.match(/^\s*#\s+(.+)\s*$/m);
return m ? m[1].trim() : null;
}
async function walkDir(absRoot, relRoot) {
const st = await statSafe(absRoot);
const node = {
type: "dir",
name: path.basename(absRoot),
path: "/" + toPosix(relRoot),
mtime: st ? st.mtimeMs : 0,
children: []
};
if (!st?.isDirectory()) return node;
const entries = await fs.readdir(absRoot, { withFileTypes: true });
entries.sort((a, b) => a.name.localeCompare(b.name));
async function walk(dir, base) {
const entries = await fs.readdir(dir, { withFileTypes: true });
const nodes = [];
for (const e of entries) { for (const e of entries) {
const abs = path.join(absRoot, e.name); if (e.name.startsWith(".")) continue;
const rel = toPosix(path.join(relRoot, e.name)); const abs = path.join(dir, e.name);
if (isHidden(rel)) continue; const rel = path.posix.join(base, e.name);
const st = await fs.stat(abs);
if (e.isDirectory()) { if (e.isDirectory()) {
node.children.push(await walkDir(abs, rel)); const children = await walk(abs, rel);
nodes.push({ type: "dir", name: e.name, path: rel, children });
} else { } else {
const ext = path.extname(e.name).toLowerCase(); const ext = path.extname(e.name);
if (!ALLOWED.has(ext)) continue; if (!exts.includes(ext)) continue;
const stFile = await statSafe(abs); nodes.push({
const raw = await readFirstChunk(abs);
let title = (ext === ".html") ? parseTitleFromHTML(raw) : parseTitleFromMD(raw);
title = title || e.name;
node.children.push({
type: "file", type: "file",
name: e.name, name: e.name,
path: rel,
ext, ext,
title, pinned: base.startsWith("pinned"),
path: "/" + rel, mtime: st.mtimeMs
mtime: stFile ? stFile.mtimeMs : 0,
size: stFile ? stFile.size : 0,
pinned: rel.startsWith("pinned/")
}); });
} }
} }
return nodes;
return node;
} }
function flatten(node, out = []) { async function buildIndex() {
if (node.type === "file") { out.push(node); return out; } const tree = [];
for (const c of node.children || []) flatten(c, out); const flat = [];
return out;
}
async function main() {
const index = {
generatedAt: new Date().toISOString(),
tree: { type: "dir", name: "/", path: "/", mtime: Date.now(), children: [] },
flat: []
};
for (const root of ROOTS) { for (const root of ROOTS) {
const abs = path.join(PUBLIC_DIR, root); const abs = path.join(PUBLIC, root);
const st = await statSafe(abs); try {
if (!st?.isDirectory()) { const children = await walk(abs, root);
console.warn(`warning: /public/${root} missing — skipping`); const node = { type: "dir", name: root, path: root, children };
continue; tree.push(node);
for (const c of children) flatten(c, flat);
} catch {
console.warn(`Skipping missing ${root}/`);
} }
const node = await walkDir(abs, root);
node.name = root;
node.path = "/" + root;
index.tree.children.push(node);
} }
// Flatten in the order roots were added const data = { generatedAt: Date.now(), tree, flat };
for (const child of index.tree.children) flatten(child, index.flat); await fs.writeFile(path.join(PUBLIC, "index.json"), JSON.stringify(data, null, 2));
console.log("✅ index.json generated:", flat.length, "files.");
const outPath = path.join(PUBLIC_DIR, "index.json");
await fs.writeFile(outPath, JSON.stringify(index, null, 2));
console.log(`wrote ${posix.relative(PUBLIC_DIR, outPath)} with ${index.flat.length} files.`);
} }
main().catch(err => { function flatten(node, arr) {
console.error(err); if (node.type === "file") arr.push(node);
process.exit(1); else node.children.forEach(c => flatten(c, arr));
}); }
await buildIndex();