Update generate-index.mjs
This commit is contained in:
parent
2aac0388ed
commit
80f48b2cfd
1 changed files with 11 additions and 3 deletions
|
|
@ -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);
|
||||||
const raw = await readHead(absPath);
|
let title;
|
||||||
const title = parseTitle(raw, ext) || e.name;
|
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);
|
||||||
|
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({
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue