thefoldwithin-earth/tools/generate-index.mjs

105 lines
3.1 KiB
JavaScript
Raw Normal View History

2025-11-08 09:05:04 -06:00
import { promises as fs } from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
2025-11-08 10:00:24 -06:00
const PUBLIC = path.resolve(__dirname, "../public");
2025-11-08 10:37:54 -06:00
const ROOTS = ["pinned","posts"];
const ALLOWED = new Set([".md",".html"]);
const MAX_BYTES = 64 * 1024; // read head for title parse
2025-11-08 09:05:04 -06:00
2025-11-08 10:37:54 -06:00
// --- 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){
const m = name.match(/^(\d{4}-\d{2}-\d{2})/);
if (!m) return null;
const d = new Date(m[1]); const t = d.getTime();
return Number.isFinite(t) ? t : null;
}
async function readHead(abs){
const fh = await fs.open(abs,"r");
const buf = Buffer.alloc(MAX_BYTES);
const { bytesRead } = await fh.read(buf, 0, MAX_BYTES, 0);
await fh.close();
return buf.slice(0, bytesRead).toString("utf8");
}
function parseTitle(raw, ext){
if (ext===".md"){
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;
}
// --- walker -------------------------------------------------------
async function walk(absDir, relBase){
const out = [];
const ents = await fs.readdir(absDir, { withFileTypes: true });
for (const e of ents){
2025-11-08 10:00:24 -06:00
if (e.name.startsWith(".")) continue;
2025-11-08 10:37:54 -06:00
const abs = path.join(absDir, e.name);
const rel = posixJoin(relBase, e.name);
2025-11-08 10:00:24 -06:00
const st = await fs.stat(abs);
2025-11-08 09:05:04 -06:00
2025-11-08 10:37:54 -06:00
if (e.isDirectory()){
2025-11-08 10:00:24 -06:00
const children = await walk(abs, rel);
2025-11-08 10:37:54 -06:00
out.push({ type:"dir", name:e.name, path:rel, children });
2025-11-08 09:05:04 -06:00
} else {
2025-11-08 10:00:24 -06:00
const ext = path.extname(e.name);
2025-11-08 10:37:54 -06:00
if (!ALLOWED.has(ext)) continue;
const raw = await readHead(abs);
const title = parseTitle(raw, ext) || e.name;
const dated = dateFromName(e.name);
out.push({
type:"file",
name:e.name,
title,
path:rel,
2025-11-08 09:05:04 -06:00
ext,
2025-11-08 10:37:54 -06:00
pinned: relBase.startsWith("pinned"),
mtime: dated ?? st.mtimeMs
2025-11-08 09:05:04 -06:00
});
}
}
2025-11-08 10:37:54 -06:00
return out;
2025-11-08 09:05:04 -06:00
}
2025-11-08 10:37:54 -06:00
function flatten(node, list){
if (node.type==="file") list.push(node);
else node.children.forEach(c=>flatten(c,list));
}
// --- build --------------------------------------------------------
async function build(){
2025-11-08 10:00:24 -06:00
const tree = [];
const flat = [];
2025-11-08 09:05:04 -06:00
2025-11-08 10:37:54 -06:00
for (const root of ROOTS){
2025-11-08 10:00:24 -06:00
const abs = path.join(PUBLIC, root);
2025-11-08 10:37:54 -06:00
const st = await statSafe(abs);
if (!st?.isDirectory()){
console.warn(`Warning: skipping missing ${root}/`);
continue;
2025-11-08 09:05:04 -06:00
}
2025-11-08 10:37:54 -06:00
const children = await walk(abs, root);
const dirNode = { type:"dir", name:root, path:root, children };
tree.push(dirNode);
flatten(dirNode, flat);
2025-11-08 09:05:04 -06:00
}
2025-11-08 10:00:24 -06:00
const data = { generatedAt: Date.now(), tree, flat };
2025-11-08 10:37:54 -06:00
const outPath = path.join(PUBLIC, "index.json");
await fs.writeFile(outPath, JSON.stringify(data, null, 2));
console.log(`✅ index.json generated (${flat.length} files)`);
2025-11-08 09:05:04 -06:00
}
2025-11-08 10:37:54 -06:00
await build();