Update generate-index.mjs

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

View file

@ -1,105 +1,67 @@
import { promises as fs } from "fs"; #!/usr/bin/env node
import fs from "fs/promises";
import path from "path"; import path from "path";
import { fileURLToPath } from "url"; const ROOT = "public";
const OUT = path.join(ROOT, "index.json");
const __dirname = path.dirname(fileURLToPath(import.meta.url)); const STATIC_TOPLEVEL = new Set(["about","contact","legal"]);
const PUBLIC = path.resolve(__dirname, "../public"); const MAX_BYTES = 64 * 1024;
const ROOTS = ["pinned","posts"];
const ALLOWED = new Set([".md",".html"]);
const MAX_BYTES = 64 * 1024; // read head for title parse
// --- helpers ------------------------------------------------------
async function statSafe(p){ try{ return await fs.stat(p); }catch{ return null; } }
function posixJoin(...xs){ return path.posix.join(...xs); }
function dateFromName(name){ function dateFromName(name){
const m = name.match(/^(\d{4}-\d{2}-\d{2})/); const m=name.match(/^(\d{4}-\d{2}-\d{2})/);
if (!m) return null; return m?new Date(m[1]).getTime():null;
const d = new Date(m[1]); const t = d.getTime();
return Number.isFinite(t) ? t : 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);
const { bytesRead } = await fh.read(buf, 0, MAX_BYTES, 0); const {bytesRead}=await fh.read(buf,0,MAX_BYTES,0);
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"){ if(ext===".html") return raw.match(/<title[^>]*>([^<]+)<\/title>/i)?.[1].trim();
const m = raw.match(/^\s*#\s+(.+?)\s*$/m);
if (m) return m[1].trim();
} else if (ext===".html"){
const m = raw.match(/<title[^>]*>([^<]+)<\/title>/i);
if (m) return m[1].trim();
}
return null; return null;
} }
// --- walker ------------------------------------------------------- async function walk(relBase=""){
async function walk(absDir, relBase){ const abs=path.join(ROOT,relBase);
const out = []; const entries=await fs.readdir(abs,{withFileTypes:true});
const ents = await fs.readdir(absDir, { withFileTypes: true }); const dir={type:"dir",name:path.basename(relBase)||"",path:relBase,children:[]};
for (const e of ents){ for(const e of entries){
if (e.name.startsWith(".")) continue; if(e.name.startsWith(".")) continue;
const abs = path.join(absDir, e.name); const rel=path.posix.join(relBase,e.name);
const rel = posixJoin(relBase, e.name); const absPath=path.join(ROOT,rel);
const st = await fs.stat(abs); if(e.isDirectory()){
const top=rel.split("/")[0];
if (e.isDirectory()){ if(STATIC_TOPLEVEL.has(top)){continue;}
const children = await walk(abs, rel); const child=await walk(rel);
out.push({ type:"dir", name:e.name, path:rel, children }); dir.children.push(child);
} else { continue;
const ext = path.extname(e.name); }
if (!ALLOWED.has(ext)) continue; const ext=path.extname(e.name);
if(![".md",".html"].includes(ext)) continue;
const raw = await readHead(abs); const st=await fs.stat(absPath);
const title = parseTitle(raw, ext) || e.name; const raw=await readHead(absPath);
const dated = dateFromName(e.name); const title=parseTitle(raw,ext)||e.name;
out.push({ const date=dateFromName(e.name);
dir.children.push({
type:"file", type:"file",
name:e.name, name:e.name,
title, title,
path:rel, path:rel,
ext, ext,
pinned: relBase.startsWith("pinned"), pinned:rel.startsWith("pinned/"),
mtime: dated ?? st.mtimeMs mtime:date||st.mtimeMs
}); });
} }
} return dir;
return out;
} }
function flatten(node, list){ (async()=>{
if (node.type==="file") list.push(node); const tree=await walk();
else node.children.forEach(c=>flatten(c,list)); const flat=[];
} (function flatten(n){for(const c of n.children){if(c.type==="file")flat.push(c);else flatten(c);}})(tree);
const sections=[...new Set(flat.map(f=>f.path.split("/")[0]))];
// --- build -------------------------------------------------------- await fs.writeFile(OUT,JSON.stringify({tree:tree.children,flat,sections},null,2));
async function build(){ console.log("index.json built:",OUT);
const tree = []; })();
const flat = [];
for (const root of ROOTS){
const abs = path.join(PUBLIC, root);
const st = await statSafe(abs);
if (!st?.isDirectory()){
console.warn(`Warning: skipping missing ${root}/`);
continue;
}
const children = await walk(abs, root);
const dirNode = { type:"dir", name:root, path:root, children };
tree.push(dirNode);
flatten(dirNode, flat);
}
const data = { generatedAt: Date.now(), tree, flat };
const outPath = path.join(PUBLIC, "index.json");
await fs.writeFile(outPath, JSON.stringify(data, null, 2));
console.log(`✅ index.json generated (${flat.length} files)`);
}
await build();