Update generate-index.mjs
This commit is contained in:
parent
ac36ad3f25
commit
a070458d34
1 changed files with 76 additions and 35 deletions
|
|
@ -4,61 +4,102 @@ import { fileURLToPath } from "url";
|
||||||
|
|
||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
const PUBLIC = path.resolve(__dirname, "../public");
|
const PUBLIC = path.resolve(__dirname, "../public");
|
||||||
const ROOTS = ["pinned", "posts"];
|
const ROOTS = ["pinned","posts"];
|
||||||
const exts = [".html", ".md"];
|
const ALLOWED = new Set([".md",".html"]);
|
||||||
|
const MAX_BYTES = 64 * 1024; // read head for title parse
|
||||||
|
|
||||||
async function walk(dir, base) {
|
// --- helpers ------------------------------------------------------
|
||||||
const entries = await fs.readdir(dir, { withFileTypes: true });
|
async function statSafe(p){ try{ return await fs.stat(p); }catch{ return null; } }
|
||||||
const nodes = [];
|
|
||||||
for (const e of entries) {
|
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){
|
||||||
if (e.name.startsWith(".")) continue;
|
if (e.name.startsWith(".")) continue;
|
||||||
const abs = path.join(dir, e.name);
|
const abs = path.join(absDir, e.name);
|
||||||
const rel = path.posix.join(base, e.name);
|
const rel = posixJoin(relBase, e.name);
|
||||||
const st = await fs.stat(abs);
|
const st = await fs.stat(abs);
|
||||||
|
|
||||||
if (e.isDirectory()) {
|
if (e.isDirectory()){
|
||||||
const children = await walk(abs, rel);
|
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 {
|
} else {
|
||||||
const ext = path.extname(e.name);
|
const ext = path.extname(e.name);
|
||||||
if (!exts.includes(ext)) continue;
|
if (!ALLOWED.has(ext)) continue;
|
||||||
nodes.push({
|
|
||||||
type: "file",
|
const raw = await readHead(abs);
|
||||||
name: e.name,
|
const title = parseTitle(raw, ext) || e.name;
|
||||||
path: rel,
|
const dated = dateFromName(e.name);
|
||||||
|
out.push({
|
||||||
|
type:"file",
|
||||||
|
name:e.name,
|
||||||
|
title,
|
||||||
|
path:rel,
|
||||||
ext,
|
ext,
|
||||||
pinned: base.startsWith("pinned"),
|
pinned: relBase.startsWith("pinned"),
|
||||||
mtime: st.mtimeMs
|
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 tree = [];
|
||||||
const flat = [];
|
const flat = [];
|
||||||
|
|
||||||
for (const root of ROOTS) {
|
for (const root of ROOTS){
|
||||||
const abs = path.join(PUBLIC, root);
|
const abs = path.join(PUBLIC, root);
|
||||||
try {
|
const st = await statSafe(abs);
|
||||||
const children = await walk(abs, root);
|
if (!st?.isDirectory()){
|
||||||
const node = { type: "dir", name: root, path: root, children };
|
console.warn(`Warning: skipping missing ${root}/`);
|
||||||
tree.push(node);
|
continue;
|
||||||
for (const c of children) flatten(c, flat);
|
|
||||||
} catch {
|
|
||||||
console.warn(`Skipping missing ${root}/`);
|
|
||||||
}
|
}
|
||||||
|
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 data = { generatedAt: Date.now(), tree, flat };
|
||||||
await fs.writeFile(path.join(PUBLIC, "index.json"), JSON.stringify(data, null, 2));
|
const outPath = path.join(PUBLIC, "index.json");
|
||||||
console.log("✅ index.json generated:", flat.length, "files.");
|
await fs.writeFile(outPath, JSON.stringify(data, null, 2));
|
||||||
|
console.log(`✅ index.json generated (${flat.length} files)`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function flatten(node, arr) {
|
await build();
|
||||||
if (node.type === "file") arr.push(node);
|
|
||||||
else node.children.forEach(c => flatten(c, arr));
|
|
||||||
}
|
|
||||||
|
|
||||||
await buildIndex();
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue