-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocs.ts
More file actions
229 lines (215 loc) · 6.34 KB
/
docs.ts
File metadata and controls
229 lines (215 loc) · 6.34 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { getCloudflareContext } from "@opennextjs/cloudflare";
import { readFile, readdir } from "node:fs/promises";
import { join } from "node:path";
import yaml from "js-yaml";
import { isCloudflare } from "./detectCloudflare";
import { notFound } from "next/navigation";
import crypto from "node:crypto";
export interface MarkdownSection {
file: string; // ファイル名
id: string;
level: number;
title: string;
rawContent: string; // 見出しも含めたもとのmarkdownの内容
md5: string; // mdファイル全体のmd5
}
export interface PageEntry {
index: number;
slug: string;
name: string;
title: string;
}
export interface LanguageEntry {
id: string;
name: string;
description: string;
pages: PageEntry[];
}
interface IndexYml {
name: string;
description: string;
pages: {
slug: string;
name: string;
title: string;
}[];
}
export interface RevisionYmlEntry {
page: string;
rev: SectionRevision[];
}
export interface SectionRevision {
md5: string; // mdファイル全体のmd5
git: string; // git上のコミットハッシュ
path: string;
}
async function readPublicFile(path: string): Promise<string> {
try {
if (isCloudflare()) {
const cfAssets = getCloudflareContext().env.ASSETS;
const res = await cfAssets!.fetch(`https://assets.local/${path}`);
if (!res.ok) {
console.error(
`Failed to fetch ${path}: ${res.status} ${await res.text()}`
);
notFound();
}
return await res.text();
} else {
return await readFile(join(process.cwd(), "public", path), "utf-8");
}
} catch (e) {
console.error(`Failed to read file ${path}: ${e}`);
notFound();
}
}
async function getLanguageIds(): Promise<string[]> {
if (isCloudflare()) {
const raw = await readPublicFile("docs/languages.json");
return JSON.parse(raw) as string[];
} else {
const docsDir = join(process.cwd(), "public", "docs");
const entries = await readdir(docsDir, { withFileTypes: true });
return entries
.filter((e) => e.isDirectory())
.map((e) => e.name)
.sort();
}
}
export async function getPagesList(): Promise<LanguageEntry[]> {
const langIds = await getLanguageIds();
return await Promise.all(
langIds.map(async (langId) => {
const raw = await readPublicFile(`docs/${langId}/index.yml`);
const data = yaml.load(raw) as IndexYml;
return {
id: langId,
name: data.name,
description: data.description,
pages: data.pages.map((p, index) => ({
...p,
index,
})),
};
})
);
}
export async function getSectionsList(
lang: string,
pageId: string
): Promise<string[]> {
if (isCloudflare()) {
const sectionsJson = await readPublicFile(
`docs/${lang}/${pageId}/sections.json`
);
return JSON.parse(sectionsJson) as string[];
} else {
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);
}
return (await readdir(join(process.cwd(), "public", "docs", lang, pageId)))
.filter((f) => f.endsWith(".md"))
.sort(naturalSortMdFiles);
}
}
export async function getRevisions(
sectionId: string
): Promise<RevisionYmlEntry | undefined> {
const revisionsYml = await readPublicFile(`docs/revisions.yml`);
return (yaml.load(revisionsYml) as Record<string, RevisionYmlEntry>)[
sectionId
];
}
/**
* public/docs/{lang}/{pageId}/ 以下のmdファイルを結合して MarkdownSection[] を返す。
*/
export async function getMarkdownSections(
lang: string,
pageId: string
): Promise<MarkdownSection[]> {
const files = await getSectionsList(lang, pageId);
const sections: MarkdownSection[] = [];
for (const file of files) {
const raw = await readPublicFile(`docs/${lang}/${pageId}/${file}`);
if (file === "-intro.md") {
// イントロセクションはフロントマターなし・見出しなし
sections.push({
file,
id: `${lang}-${pageId}-intro`,
level: 1,
title: "",
rawContent: raw,
md5: crypto.createHash("md5").update(raw).digest("base64"),
});
} else {
sections.push(parseFrontmatter(raw, file));
}
}
return sections;
}
/**
* YAMLフロントマターをパースしてid, title, level, bodyを返す。
* フロントマターがない場合はid/titleを空文字、levelを0で返す。
*/
function parseFrontmatter(content: string, file: string): MarkdownSection {
if (!content.startsWith("---\n")) {
throw new Error(`File ${file} is missing frontmatter`);
}
const endIdx = content.indexOf("\n---\n", 4);
if (endIdx === -1) {
throw new Error(`File ${file} has invalid frontmatter`);
}
const fm = yaml.load(content.slice(4, endIdx)) as {
id?: string;
title?: string;
level?: number;
};
// TODO: validation of frontmatter using zod
const rawContent = content.slice(endIdx + 5);
return {
file,
id: fm?.id ?? "",
title: fm?.title ?? "",
level: fm?.level ?? 2,
rawContent,
md5: crypto.createHash("md5").update(rawContent).digest("base64"),
};
}
export async function getRevisionOfMarkdownSection(
sectionId: string,
md5: string
): Promise<MarkdownSection> {
const revisions = await getRevisions(sectionId);
const targetRevision = revisions?.rev.find((r) => r.md5 === md5);
if (targetRevision) {
const rawRes = await fetch(
`https://raw.githubusercontent.com/ut-code/my-code/${targetRevision.git}/${targetRevision.path}`
);
if (rawRes.ok) {
const raw = await rawRes.text();
return parseFrontmatter(
raw,
`${targetRevision.git}/${targetRevision.path}`
);
} else {
throw new Error(
`Failed to fetch ${targetRevision.git}/${targetRevision.path}. ${rawRes.status}: ${await rawRes.text()}`
);
}
} else {
throw new Error(
`Revision for sectionId=${sectionId}, md5=${md5} not found`
);
}
}