Skip to content

Commit d16e95f

Browse files
Copilotna-trium-144
andcommitted
Use js-yaml for YAML parsing in page route and generateSections script
Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
1 parent 0d7ee22 commit d16e95f

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

app/[lang]/[pageId]/page.tsx

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { notFound } from "next/navigation";
33
import { getCloudflareContext } from "@opennextjs/cloudflare";
44
import { readFile } from "node:fs/promises";
55
import { join } from "node:path";
6+
import yaml from "js-yaml";
67
import { MarkdownSection } from "../[docs_id]/splitMarkdown";
78
import { PageContent } from "../[docs_id]/pageContent";
89
import { ChatHistoryProvider } from "../[docs_id]/chatHistory";
@@ -51,20 +52,18 @@ function parseFrontmatter(content: string): {
5152
if (endIdx === -1) {
5253
return { id: "", title: "", level: 0, body: content };
5354
}
54-
const fm = content.slice(4, endIdx);
55+
const fm = yaml.load(content.slice(4, endIdx)) as {
56+
id?: string;
57+
title?: string;
58+
level?: number;
59+
};
5560
const body = content.slice(endIdx + 5);
56-
57-
const id = fm.match(/^id:\s*(.+)$/m)?.[1]?.trim() ?? "";
58-
let title = fm.match(/^title:\s*(.+)$/m)?.[1]?.trim() ?? "";
59-
// YAMLクォートを除去
60-
if (
61-
(title.startsWith("'") && title.endsWith("'")) ||
62-
(title.startsWith('"') && title.endsWith('"'))
63-
) {
64-
title = title.slice(1, -1);
65-
}
66-
const level = parseInt(fm.match(/^level:\s*(\d+)$/m)?.[1] ?? "2");
67-
return { id, title, level, body };
61+
return {
62+
id: fm?.id ?? "",
63+
title: fm?.title ?? "",
64+
level: fm?.level ?? 2,
65+
body,
66+
};
6867
}
6968

7069
/**
@@ -76,11 +75,7 @@ async function getMarkdownSections(
7675
pageTitle: string
7776
): Promise<MarkdownSection[]> {
7877
const sectionsYml = await readDocFile(lang, pageId, "sections.yml");
79-
const files = sectionsYml
80-
.split("\n")
81-
.filter((l) => l.trim().startsWith("- "))
82-
.map((l) => l.trim().slice(2).trim())
83-
.filter(Boolean);
78+
const files = yaml.load(sectionsYml) as string[];
8479

8580
const sections: MarkdownSection[] = [];
8681
for (const file of files) {

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"comlink": "^4.4.2",
3636
"dotenv": "^17.2.3",
3737
"drizzle-orm": "^0.44.7",
38+
"js-yaml": "^4.1.1",
3839
"mocha": "^11.7.4",
3940
"next": "^15.5.11",
4041
"object-inspect": "^1.13.4",
@@ -56,6 +57,7 @@
5657
"devDependencies": {
5758
"@eslint/eslintrc": "^3",
5859
"@tailwindcss/postcss": "^4",
60+
"@types/js-yaml": "^4.0.9",
5961
"@types/chai": "^5.2.3",
6062
"@types/mocha": "^10.0.10",
6163
"@types/node": "^20",

scripts/generateSectionsList.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import { readdir, writeFile, stat } from "node:fs/promises";
55
import { join } from "node:path";
6+
import yaml from "js-yaml";
67

78
const docsDir = join(process.cwd(), "public", "docs");
89

@@ -37,7 +38,7 @@ for (const langId of langEntries) {
3738
.filter((f) => f.endsWith(".md"))
3839
.sort(naturalSortMdFiles);
3940

40-
const yamlContent = files.map((f) => `- ${f}`).join("\n") + "\n";
41+
const yamlContent = yaml.dump(files);
4142
await writeFile(join(pageDir, "sections.yml"), yamlContent, "utf-8");
4243
console.log(
4344
`Generated ${langId}/${pageId}/sections.yml (${files.length} files)`

0 commit comments

Comments
 (0)