Skip to content

Commit 7c87b28

Browse files
committed
sections.jsonにセクションの名前だけでなく内容を全部入れる
1 parent ad343ea commit 7c87b28

File tree

2 files changed

+37
-45
lines changed

2 files changed

+37
-45
lines changed

app/lib/docs.ts

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,27 @@ export async function getPagesList(): Promise<LanguageEntry[]> {
167167
);
168168
}
169169

170-
export async function getSectionsList(
170+
export async function getRevisions(
171+
sectionId: SectionId
172+
): Promise<RevisionYmlEntry | undefined> {
173+
const revisionsYml = await readPublicFile(`docs/revisions.yml`);
174+
return (yaml.load(revisionsYml) as Record<string, RevisionYmlEntry>)[
175+
sectionId
176+
];
177+
}
178+
179+
/**
180+
* public/docs/{lang}/{pageId}/ 以下のmdファイルを結合して MarkdownSection[] を返す。
181+
*/
182+
export async function getMarkdownSections(
171183
lang: LangId,
172184
page: PageSlug
173-
): Promise<string[]> {
185+
): Promise<MarkdownSection[]> {
174186
if (isCloudflare()) {
175187
const sectionsJson = await readPublicFile(
176188
`docs/${lang}/${page}/sections.json`
177189
);
178-
return JSON.parse(sectionsJson) as string[];
190+
return JSON.parse(sectionsJson) as MarkdownSection[];
179191
} else {
180192
function naturalSortMdFiles(a: string, b: string): number {
181193
// -intro.md always comes first
@@ -191,48 +203,29 @@ export async function getSectionsList(
191203
}
192204
return a.localeCompare(b);
193205
}
194-
return (await readdir(join(process.cwd(), "public", "docs", lang, page)))
206+
const files = (await readdir(join(process.cwd(), "public", "docs", lang, page)))
195207
.filter((f) => f.endsWith(".md"))
196208
.sort(naturalSortMdFiles);
197-
}
198-
}
199209

200-
export async function getRevisions(
201-
sectionId: SectionId
202-
): Promise<RevisionYmlEntry | undefined> {
203-
const revisionsYml = await readPublicFile(`docs/revisions.yml`);
204-
return (yaml.load(revisionsYml) as Record<string, RevisionYmlEntry>)[
205-
sectionId
206-
];
207-
}
208-
209-
/**
210-
* public/docs/{lang}/{pageId}/ 以下のmdファイルを結合して MarkdownSection[] を返す。
211-
*/
212-
export async function getMarkdownSections(
213-
lang: LangId,
214-
page: PageSlug
215-
): Promise<MarkdownSection[]> {
216-
const files = await getSectionsList(lang, page);
217-
218-
const sections: MarkdownSection[] = [];
219-
for (const file of files) {
220-
const raw = await readPublicFile(`docs/${lang}/${page}/${file}`);
221-
if (file === "-intro.md") {
222-
// イントロセクションはフロントマターなし・見出しなし
223-
sections.push({
224-
file,
225-
id: introSectionId({ lang, page }),
226-
level: 1,
227-
title: "",
228-
rawContent: raw,
229-
md5: crypto.createHash("md5").update(raw).digest("base64"),
230-
});
231-
} else {
232-
sections.push(parseFrontmatter(raw, file));
210+
const sections: MarkdownSection[] = [];
211+
for (const file of files) {
212+
const raw = await readPublicFile(`docs/${lang}/${page}/${file}`);
213+
if (file === "-intro.md") {
214+
// イントロセクションはフロントマターなし・見出しなし
215+
sections.push({
216+
file,
217+
id: introSectionId({ lang, page }),
218+
level: 1,
219+
title: "",
220+
rawContent: raw,
221+
md5: crypto.createHash("md5").update(raw).digest("base64"),
222+
});
223+
} else {
224+
sections.push(parseFrontmatter(raw, file));
225+
}
233226
}
227+
return sections;
234228
}
235-
return sections;
236229
}
237230
export function introSectionId(path: PagePath) {
238231
return `${path.lang}-${path.page}-intro` as SectionId;

scripts/generateDocsMeta.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { writeFile } from "node:fs/promises";
55
import { join } from "node:path";
6-
import { getPagesList, getSectionsList } from "@/lib/docs";
6+
import { getMarkdownSections, getPagesList } from "@/lib/docs";
77

88
const docsDir = join(process.cwd(), "public", "docs");
99

@@ -17,15 +17,14 @@ console.log(
1717

1818
for (const lang of langEntries) {
1919
for (const page of lang.pages) {
20-
const files = await getSectionsList(lang.id, page.slug);
21-
const filesJson = JSON.stringify(files);
20+
const sections = await getMarkdownSections(lang.id, page.slug);
2221
await writeFile(
2322
join(docsDir, lang.id, page.slug, "sections.json"),
24-
filesJson,
23+
JSON.stringify(sections),
2524
"utf-8"
2625
);
2726
console.log(
28-
`Generated ${lang.id}/${page.slug}/sections.json (${files.length} files)`
27+
`Generated ${lang.id}/${page.slug}/sections.json (${sections.length} files)`
2928
);
3029
}
3130
}

0 commit comments

Comments
 (0)