Skip to content

Commit ea22377

Browse files
committed
generateSections,generateLanguagesとlib/docsを統合してシンプルに
1 parent 4e7dd62 commit ea22377

File tree

6 files changed

+64
-76
lines changed

6 files changed

+64
-76
lines changed

app/lib/docs.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,43 @@ export async function getPagesList(): Promise<LanguageEntry[]> {
9090
);
9191
}
9292

93+
export async function getSectionsList(
94+
lang: string,
95+
pageId: string
96+
): Promise<string[]> {
97+
if (isCloudflare()) {
98+
const sectionsYml = await readPublicFile(
99+
`docs/${lang}/${pageId}/sections.yml`
100+
);
101+
return yaml.load(sectionsYml) as string[];
102+
} else {
103+
function naturalSortMdFiles(a: string, b: string): number {
104+
// -intro.md always comes first
105+
if (a === "-intro.md") return -1;
106+
if (b === "-intro.md") return 1;
107+
// Sort numerically by leading N1-N2 prefix
108+
const aMatch = a.match(/^(\d+)-(\d+)/);
109+
const bMatch = b.match(/^(\d+)-(\d+)/);
110+
if (aMatch && bMatch) {
111+
const n1Diff = parseInt(aMatch[1]) - parseInt(bMatch[1]);
112+
if (n1Diff !== 0) return n1Diff;
113+
return parseInt(aMatch[2]) - parseInt(bMatch[2]);
114+
}
115+
return a.localeCompare(b);
116+
}
117+
return (await readdir(join(process.cwd(), "public", "docs", lang, pageId)))
118+
.filter((f) => f.endsWith(".md"))
119+
.sort(naturalSortMdFiles);
120+
}
121+
}
93122
/**
94123
* public/docs/{lang}/{pageId}/ 以下のmdファイルを結合して MarkdownSection[] を返す。
95124
*/
96125
export async function getMarkdownSections(
97126
lang: string,
98127
pageId: string
99128
): Promise<MarkdownSection[]> {
100-
const sectionsYml = await readPublicFile(
101-
`docs/${lang}/${pageId}/sections.yml`
102-
);
103-
const files = yaml.load(sectionsYml) as string[];
129+
const files = await getSectionsList(lang, pageId);
104130

105131
const sections: MarkdownSection[] = [];
106132
for (const file of files) {

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
"packages/*"
88
],
99
"scripts": {
10-
"dev": "npm run cf-typegen && npm run generateLanguages && npm run generateSections && npm run copyAllDTSFiles && npm run removeHinting && next dev",
11-
"build": "npm run cf-typegen && npm run generateLanguages && npm run generateSections && npm run copyAllDTSFiles && npm run removeHinting && next build",
10+
"dev": "npm run cf-typegen && npm run generateDocsMeta && npm run copyAllDTSFiles && npm run removeHinting && next dev",
11+
"build": "npm run cf-typegen && npm run generateDocsMeta && npm run copyAllDTSFiles && npm run removeHinting && next build",
1212
"start": "next start",
1313
"lint": "npm run cf-typegen && next lint",
1414
"tsc": "npm run cf-typegen && tsc",
1515
"format": "prettier --write app/",
16-
"generateLanguages": "tsx ./scripts/generateLanguagesList.ts",
17-
"generateSections": "tsx ./scripts/generateSectionsList.ts",
16+
"generateDocsMeta": "tsx ./scripts/generateDocsMeta.ts",
1817
"copyAllDTSFiles": "tsx ./scripts/copyAllDTSFiles.ts",
1918
"removeHinting": "tsx ./scripts/removeHinting.ts",
2019
"cf-preview": "opennextjs-cloudflare build && opennextjs-cloudflare preview --port 3000",

scripts/generateDocsMeta.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Generates public/docs/{lang}/{pageId}/sections.yml for each page directory.
2+
// Each sections.yml lists the .md files in that directory in display order.
3+
4+
import { writeFile } from "node:fs/promises";
5+
import { join } from "node:path";
6+
import yaml from "js-yaml";
7+
import { getPagesList, getSectionsList } from "@/lib/docs";
8+
9+
const docsDir = join(process.cwd(), "public", "docs");
10+
11+
const langEntries = await getPagesList();
12+
13+
const yamlContent = yaml.dump(langEntries.map((lang) => lang.id));
14+
await writeFile(join(docsDir, "languages.yml"), yamlContent, "utf-8");
15+
console.log(`Generated languages.yml (${langEntries.length} languages: ${langEntries.map((lang) => lang.id).join(", ")})`);
16+
17+
for (const lang of langEntries) {
18+
for (const page of lang.pages) {
19+
const files = await getSectionsList(lang.id, page.slug);
20+
const yamlContent = yaml.dump(files);
21+
await writeFile(
22+
join(docsDir, lang.id, page.slug, "sections.yml"),
23+
yamlContent,
24+
"utf-8"
25+
);
26+
console.log(
27+
`Generated ${lang.id}/${page.slug}/sections.yml (${files.length} files)`
28+
);
29+
}
30+
}
31+

scripts/generateLanguagesList.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

scripts/generateSectionsList.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

scripts/updateSectionHistory.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)