Update generate-index.mjs
This commit is contained in:
parent
e63f732151
commit
44d811e2de
1 changed files with 20 additions and 2 deletions
|
|
@ -104,10 +104,28 @@ async function collectFiles(relBase = "", flat = []) {
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const flat = await collectFiles();
|
const flat = await collectFiles();
|
||||||
|
|
||||||
|
// Build sections: folders with non-index files
|
||||||
const sections = [...new Set(flat.filter(f => !f.isIndex).map(f => f.path.split("/")[0]))].sort();
|
const sections = [...new Set(flat.filter(f => !f.isIndex).map(f => f.path.split("/")[0]))].sort();
|
||||||
|
|
||||||
|
// Build hierarchies: parent → [child] where child has index.*
|
||||||
|
const hierarchies = {};
|
||||||
|
for (const f of flat.filter(f => f.isIndex)) {
|
||||||
|
const parts = f.path.split("/");
|
||||||
|
if (parts.length > 2) { // e.g., essays/ai/index.md → parts[0]=essays, parts[1]=ai
|
||||||
|
const parent = parts[0];
|
||||||
|
const child = parts[1];
|
||||||
|
if (!hierarchies[parent]) hierarchies[parent] = [];
|
||||||
|
if (!hierarchies[parent].includes(child)) {
|
||||||
|
hierarchies[parent].push(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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, tags: allTags }, null, 2));
|
|
||||||
console.log(`index.json built: ${flat.length} files, ${sections.length} sections, ${allTags.length} tags.`);
|
await fs.writeFile(OUT, JSON.stringify({ flat, sections, tags: allTags, hierarchies }, null, 2));
|
||||||
|
console.log(`index.json built: ${flat.length} files, ${sections.length} sections, ${Object.keys(hierarchies).length} hierarchies, ${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