Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 51 additions & 48 deletions app/lib/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,26 @@ export interface SectionRevision {
path: string;
}

const publicFileCache = new Map<string, Promise<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();
if (publicFileCache.has(path)) {
return publicFileCache.get(path)!;
}
return await res.text();
const p = (async () => {
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();
})();
publicFileCache.set(path, p);
return p;
} else {
return await readFile(join(process.cwd(), "public", path), "utf-8");
}
Expand Down Expand Up @@ -167,15 +175,27 @@ export async function getPagesList(): Promise<LanguageEntry[]> {
);
}

export async function getSectionsList(
export async function getRevisions(
sectionId: SectionId
): 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: LangId,
page: PageSlug
): Promise<string[]> {
): Promise<MarkdownSection[]> {
if (isCloudflare()) {
const sectionsJson = await readPublicFile(
`docs/${lang}/${page}/sections.json`
);
return JSON.parse(sectionsJson) as string[];
return JSON.parse(sectionsJson) as MarkdownSection[];
} else {
function naturalSortMdFiles(a: string, b: string): number {
// -intro.md always comes first
Expand All @@ -191,48 +211,31 @@ export async function getSectionsList(
}
return a.localeCompare(b);
}
return (await readdir(join(process.cwd(), "public", "docs", lang, page)))
const files = (
await readdir(join(process.cwd(), "public", "docs", lang, page))
)
.filter((f) => f.endsWith(".md"))
.sort(naturalSortMdFiles);
}
}

export async function getRevisions(
sectionId: SectionId
): 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: LangId,
page: PageSlug
): Promise<MarkdownSection[]> {
const files = await getSectionsList(lang, page);

const sections: MarkdownSection[] = [];
for (const file of files) {
const raw = await readPublicFile(`docs/${lang}/${page}/${file}`);
if (file === "-intro.md") {
// イントロセクションはフロントマターなし・見出しなし
sections.push({
file,
id: introSectionId({ lang, page }),
level: 1,
title: "",
rawContent: raw,
md5: crypto.createHash("md5").update(raw).digest("base64"),
});
} else {
sections.push(parseFrontmatter(raw, file));
const sections: MarkdownSection[] = [];
for (const file of files) {
const raw = await readPublicFile(`docs/${lang}/${page}/${file}`);
if (file === "-intro.md") {
// イントロセクションはフロントマターなし・見出しなし
sections.push({
file,
id: introSectionId({ lang, page }),
level: 1,
title: "",
rawContent: raw,
md5: crypto.createHash("md5").update(raw).digest("base64"),
});
} else {
sections.push(parseFrontmatter(raw, file));
}
}
return sections;
}
return sections;
}
export function introSectionId(path: PagePath) {
return `${path.lang}-${path.page}-intro` as SectionId;
Expand Down
9 changes: 4 additions & 5 deletions scripts/generateDocsMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { getPagesList, getSectionsList } from "@/lib/docs";
import { getMarkdownSections, getPagesList } from "@/lib/docs";

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

Expand All @@ -17,15 +17,14 @@ console.log(

for (const lang of langEntries) {
for (const page of lang.pages) {
const files = await getSectionsList(lang.id, page.slug);
const filesJson = JSON.stringify(files);
const sections = await getMarkdownSections(lang.id, page.slug);
await writeFile(
join(docsDir, lang.id, page.slug, "sections.json"),
filesJson,
JSON.stringify(sections),
"utf-8"
);
console.log(
`Generated ${lang.id}/${page.slug}/sections.json (${files.length} files)`
`Generated ${lang.id}/${page.slug}/sections.json (${sections.length} files)`
);
}
}