Update generate-index.mjs
This commit is contained in:
parent
ba71970996
commit
0910e41331
1 changed files with 8 additions and 26 deletions
|
|
@ -29,7 +29,6 @@ function parseTitle(raw, ext) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractExcerpt(raw, ext) {
|
function extractExcerpt(raw, ext) {
|
||||||
// Trim headers/metadata for cleaner excerpts
|
|
||||||
if (ext === ".md") raw = raw.replace(/^#.*\n?/, '').trim();
|
if (ext === ".md") raw = raw.replace(/^#.*\n?/, '').trim();
|
||||||
if (ext === ".html") raw = raw.replace(/<head>.*<\/head>/is, '').replace(/<[^>]+>/g, ' ').trim();
|
if (ext === ".html") raw = raw.replace(/<head>.*<\/head>/is, '').replace(/<[^>]+>/g, ' ').trim();
|
||||||
return raw.replace(/\s+/g, ' ').slice(0, EXCERPT_LENGTH);
|
return raw.replace(/\s+/g, ' ').slice(0, EXCERPT_LENGTH);
|
||||||
|
|
@ -63,7 +62,7 @@ async function collectFiles(relBase = "", flat = []) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const ext = path.posix.extname(e.name).toLowerCase();
|
const ext = path.posix.extname(e.name).toLowerCase();
|
||||||
if (![".md", ".html", ".pdf"].includes(ext) || e.name === "index.html") continue;
|
if (![".md", ".html", ".pdf"].includes(ext)) continue; // No exclude for index.*
|
||||||
const st = await fs.stat(absPath);
|
const st = await fs.stat(absPath);
|
||||||
let raw, pdfData, title;
|
let raw, pdfData, title;
|
||||||
if (ext === ".pdf") {
|
if (ext === ".pdf") {
|
||||||
|
|
@ -78,6 +77,7 @@ async function collectFiles(relBase = "", flat = []) {
|
||||||
const mtime = dateFromName(e.name) ?? st.mtimeMs;
|
const mtime = dateFromName(e.name) ?? st.mtimeMs;
|
||||||
const excerpt = extractExcerpt(raw, ext);
|
const excerpt = extractExcerpt(raw, ext);
|
||||||
const tags = extractTags(raw, ext, pdfData);
|
const tags = extractTags(raw, ext, pdfData);
|
||||||
|
const baseName = e.name.toLowerCase().replace(ext, '').trim();
|
||||||
|
|
||||||
flat.push({
|
flat.push({
|
||||||
type: "file",
|
type: "file",
|
||||||
|
|
@ -85,41 +85,23 @@ async function collectFiles(relBase = "", flat = []) {
|
||||||
title,
|
title,
|
||||||
path: rel,
|
path: rel,
|
||||||
ext,
|
ext,
|
||||||
pinned: rel.startsWith("pinned/"),
|
|
||||||
mtime,
|
mtime,
|
||||||
excerpt,
|
excerpt,
|
||||||
tags
|
tags,
|
||||||
|
isIndex: baseName === "index",
|
||||||
|
isPinned: baseName === "pinned"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return flat;
|
return flat;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function detectSections() {
|
|
||||||
const topEntries = await fs.readdir(ROOT, { withFileTypes: true });
|
|
||||||
const sections = [];
|
|
||||||
for (const e of topEntries) {
|
|
||||||
if (!e.isDirectory() || e.name.startsWith(".")) continue;
|
|
||||||
const indexPath = path.join(ROOT, e.name, "index.html");
|
|
||||||
let isStatic = false;
|
|
||||||
try {
|
|
||||||
await fs.access(indexPath);
|
|
||||||
isStatic = true;
|
|
||||||
} catch {}
|
|
||||||
// Check if dynamic (has content files) - but since flat collects them, infer from flat later
|
|
||||||
sections.push({ name: e.name, isStatic });
|
|
||||||
}
|
|
||||||
return sections.sort((a, b) => a.name.localeCompare(b.name)); // Alpha sort
|
|
||||||
}
|
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const flat = await collectFiles();
|
const flat = await collectFiles();
|
||||||
const sections = await detectSections();
|
const sections = [...new Set(flat.map(f => f.path.split("/")[0]))].sort();
|
||||||
// Filter sections to those with content or static
|
|
||||||
const activeSections = sections.filter(s => s.isStatic || flat.some(f => f.path.split("/")[0] === s.name));
|
|
||||||
const allTags = [...new Set(flat.flatMap(f => f.tags))].sort();
|
const allTags = [...new Set(flat.flatMap(f => f.tags))].sort();
|
||||||
await fs.writeFile(OUT, JSON.stringify({ flat, sections: activeSections, tags: allTags }, null, 2));
|
await fs.writeFile(OUT, JSON.stringify({ flat, sections, tags: allTags }, null, 2));
|
||||||
console.log(`index.json built: ${flat.length} files, ${activeSections.length} sections (${activeSections.filter(s => s.isStatic).length} static), ${allTags.length} tags.`);
|
console.log(`index.json built: ${flat.length} files, ${sections.length} sections, ${allTags.length} tags.`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Build failed:", e);
|
console.error("Build failed:", e);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue