Skip to content

Commit d5fef5c

Browse files
committed
introにもmd5を追加, ymlサイズ削減、page削除はエラーとする
1 parent ae1a6f0 commit d5fef5c

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

app/lib/docs.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ interface IndexYml {
4040
}
4141

4242
export interface RevisionYmlEntry {
43-
lang?: string;
44-
page?: string;
43+
page: string;
4544
rev: SectionRevision[];
4645
}
4746
export interface SectionRevision {
4847
md5: string; // mdファイル全体のmd5
49-
commit: string;
48+
git: string; // git上のコミットハッシュ
5049
path: string;
5150
}
5251

@@ -163,7 +162,7 @@ export async function getMarkdownSections(
163162
level: 1,
164163
title: "",
165164
rawContent: raw,
166-
md5: "",
165+
md5: crypto.createHash("md5").update(raw).digest("base64"),
167166
});
168167
} else {
169168
sections.push(parseFrontmatter(raw, file));
@@ -197,7 +196,7 @@ function parseFrontmatter(content: string, file: string): MarkdownSection {
197196
title: fm?.title ?? "",
198197
level: fm?.level ?? 2,
199198
rawContent,
200-
md5: crypto.createHash("md5").update(content).digest("base64"),
199+
md5: crypto.createHash("md5").update(rawContent).digest("base64"),
201200
};
202201
}
203202

@@ -209,17 +208,17 @@ export async function getRevisionOfMarkdownSection(
209208
const targetRevision = revisions?.rev.find((r) => r.md5 === md5);
210209
if (targetRevision) {
211210
const rawRes = await fetch(
212-
`https://raw.githubusercontent.com/ut-code/my-code/${targetRevision.commit}/${targetRevision.path}`
211+
`https://raw.githubusercontent.com/ut-code/my-code/${targetRevision.git}/${targetRevision.path}`
213212
);
214213
if (rawRes.ok) {
215214
const raw = await rawRes.text();
216215
return parseFrontmatter(
217216
raw,
218-
`${targetRevision.commit}/${targetRevision.path}`
217+
`${targetRevision.git}/${targetRevision.path}`
219218
);
220219
} else {
221220
throw new Error(
222-
`Failed to fetch ${targetRevision.commit}/${targetRevision.path}. ${rawRes.status}: ${await rawRes.text()}`
221+
`Failed to fetch ${targetRevision.git}/${targetRevision.path}. ${rawRes.status}: ${await rawRes.text()}`
223222
);
224223
}
225224
} else {

scripts/updateDocsRevisions.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,18 @@ const revisions = yaml.load(revisionsPrevYml) as Record<
2525
RevisionYmlEntry
2626
>;
2727

28-
for (const id in revisions) {
29-
delete revisions[id].lang;
30-
delete revisions[id].page;
31-
}
32-
3328
for (const lang of langEntries) {
3429
for (const page of lang.pages) {
3530
const sections = await getMarkdownSections(lang.id, page.slug);
3631
for (const section of sections) {
37-
if (section.file === "-intro.md") continue;
3832
if (section.id in revisions) {
39-
revisions[section.id].lang = lang.id;
40-
revisions[section.id].page = page.slug;
33+
revisions[section.id].page = `${lang.id}/${page.slug}`;
4134
if (!revisions[section.id].rev.some((r) => r.md5 === section.md5)) {
4235
// ドキュメントが変更された場合
4336
console.log(`${section.id} has new md5: ${section.md5}`);
4437
revisions[section.id].rev.push({
4538
md5: section.md5,
46-
commit,
39+
git: commit,
4740
path: `public/docs/${lang.id}/${page.slug}/${section.file}`,
4841
});
4942
}
@@ -54,22 +47,33 @@ for (const lang of langEntries) {
5447
rev: [
5548
{
5649
md5: section.md5,
57-
commit,
50+
git: commit,
5851
path: `public/docs/${lang.id}/${page.slug}/${section.file}`,
5952
},
6053
],
61-
lang: lang.id,
62-
page: page.slug,
54+
page: `${lang.id}/${page.slug}`,
6355
};
6456
}
6557
}
6658
}
6759
}
6860

69-
const revisionsYml = yaml.dump(revisions);
61+
for (const id in revisions) {
62+
if (!existsSync(join(docsDir, revisions[id].page))) {
63+
throw new Error(
64+
`The page slug ${revisions[id].page} previously used by section ${id} does not exist. ` +
65+
`Please replace 'page: ${revisions[id].page}' in public/docs/revisions.yml with new page path manually.`
66+
);
67+
}
68+
}
69+
70+
const revisionsYml = yaml.dump(revisions, {
71+
sortKeys: true,
72+
noArrayIndent: true,
73+
});
7074
await writeFile(
7175
join(docsDir, "revisions.yml"),
72-
"# This file will be updated by scripts/updateDocsRevisions.ts. Do not edit manually.\n" +
76+
"# This file will be updated by CI. Do not edit manually, unless CI failed.\n" +
7377
revisionsYml,
7478
"utf-8"
7579
);

0 commit comments

Comments
 (0)