diff --git a/tools/generate-index.mjs b/tools/generate-index.mjs
index ac832f4..46af349 100644
--- a/tools/generate-index.mjs
+++ b/tools/generate-index.mjs
@@ -4,61 +4,102 @@ import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PUBLIC = path.resolve(__dirname, "../public");
-const ROOTS = ["pinned", "posts"];
-const exts = [".html", ".md"];
+const ROOTS = ["pinned","posts"];
+const ALLOWED = new Set([".md",".html"]);
+const MAX_BYTES = 64 * 1024; // read head for title parse
-async function walk(dir, base) {
- const entries = await fs.readdir(dir, { withFileTypes: true });
- const nodes = [];
- for (const e of entries) {
+// --- 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>/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){
if (e.name.startsWith(".")) continue;
- const abs = path.join(dir, e.name);
- const rel = path.posix.join(base, e.name);
+ const abs = path.join(absDir, e.name);
+ const rel = posixJoin(relBase, e.name);
const st = await fs.stat(abs);
- if (e.isDirectory()) {
+ if (e.isDirectory()){
const children = await walk(abs, rel);
- nodes.push({ type: "dir", name: e.name, path: rel, children });
+ out.push({ type:"dir", name:e.name, path:rel, children });
} else {
const ext = path.extname(e.name);
- if (!exts.includes(ext)) continue;
- nodes.push({
- type: "file",
- name: e.name,
- path: rel,
+ 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,
ext,
- pinned: base.startsWith("pinned"),
- mtime: st.mtimeMs
+ pinned: relBase.startsWith("pinned"),
+ mtime: dated ?? st.mtimeMs
});
}
}
- return nodes;
+ return out;
}
-async function buildIndex() {
+function flatten(node, list){
+ if (node.type==="file") list.push(node);
+ else node.children.forEach(c=>flatten(c,list));
+}
+
+// --- build --------------------------------------------------------
+async function build(){
const tree = [];
const flat = [];
- for (const root of ROOTS) {
+ for (const root of ROOTS){
const abs = path.join(PUBLIC, root);
- try {
- const children = await walk(abs, root);
- const node = { type: "dir", name: root, path: root, children };
- tree.push(node);
- for (const c of children) flatten(c, flat);
- } catch {
- console.warn(`Skipping missing ${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 };
- 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, "index.json");
+ await fs.writeFile(outPath, JSON.stringify(data, null, 2));
+ console.log(`✅ index.json generated (${flat.length} files)`);
}
-function flatten(node, arr) {
- if (node.type === "file") arr.push(node);
- else node.children.forEach(c => flatten(c, arr));
-}
-
-await buildIndex();
\ No newline at end of file
+await build();
\ No newline at end of file