Update generate-index.mjs

This commit is contained in:
Mark Randall Havens △ The Empathic Technologist ⟁ Doctor Who 42 2025-11-08 15:47:51 -06:00 committed by GitHub
parent 2aac0388ed
commit 80f48b2cfd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,7 @@
#!/usr/bin/env node #!/usr/bin/env node
import { promises as fs } from "fs"; import { promises as fs } from "fs";
import path from "path"; import path from "path";
import pdf from "pdf-parse";
const ROOT = "public"; const ROOT = "public";
const OUT = path.join(ROOT, "index.json"); const OUT = path.join(ROOT, "index.json");
@ -42,10 +43,17 @@ async function collectFiles(relBase = "", flat = []) {
} }
const ext = path.posix.extname(e.name).toLowerCase(); const ext = path.posix.extname(e.name).toLowerCase();
if (![".md", ".html"].includes(ext)) continue; if (![".md", ".html", ".pdf"].includes(ext)) continue;
const st = await fs.stat(absPath); const st = await fs.stat(absPath);
let title;
if (ext === ".pdf") {
const buffer = await fs.readFile(absPath);
const pdfData = await pdf(buffer);
title = pdfData.info.Title || e.name.replace(/\.pdf$/, "").trim();
} else {
const raw = await readHead(absPath); const raw = await readHead(absPath);
const title = parseTitle(raw, ext) || e.name; title = parseTitle(raw, ext) || e.name.replace(new RegExp(`\\${ext}$`), "").trim();
}
const mtime = dateFromName(e.name) ?? st.mtimeMs; const mtime = dateFromName(e.name) ?? st.mtimeMs;
flat.push({ flat.push({