-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerateSectionsList.ts
More file actions
47 lines (40 loc) · 1.65 KB
/
generateSectionsList.ts
File metadata and controls
47 lines (40 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Generates public/docs/{lang}/{pageId}/sections.yml for each page directory.
// Each sections.yml lists the .md files in that directory in display order.
import { readdir, writeFile, stat } from "node:fs/promises";
import { join } from "node:path";
import yaml from "js-yaml";
const docsDir = join(process.cwd(), "public", "docs");
function naturalSortMdFiles(a: string, b: string): number {
// -intro.md always comes first
if (a === "-intro.md") return -1;
if (b === "-intro.md") return 1;
// Sort numerically by leading N1-N2 prefix
const aMatch = a.match(/^(\d+)-(\d+)/);
const bMatch = b.match(/^(\d+)-(\d+)/);
if (aMatch && bMatch) {
const n1Diff = parseInt(aMatch[1]) - parseInt(bMatch[1]);
if (n1Diff !== 0) return n1Diff;
return parseInt(aMatch[2]) - parseInt(bMatch[2]);
}
return a.localeCompare(b);
}
const langEntries = await readdir(docsDir);
for (const langId of langEntries) {
const langDir = join(docsDir, langId);
if (!(await stat(langDir)).isDirectory()) continue;
const pageEntries = await readdir(langDir);
for (const pageId of pageEntries) {
// Only process page directories (start with a digit to skip index.yml and other metadata files)
if (!/^\d/.test(pageId)) continue;
const pageDir = join(langDir, pageId);
if (!(await stat(pageDir)).isDirectory()) continue;
const files = (await readdir(pageDir))
.filter((f) => f.endsWith(".md"))
.sort(naturalSortMdFiles);
const yamlContent = yaml.dump(files);
await writeFile(join(pageDir, "sections.yml"), yamlContent, "utf-8");
console.log(
`Generated ${langId}/${pageId}/sections.yml (${files.length} files)`
);
}
}