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
22 changes: 22 additions & 0 deletions app/lib/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ export interface RevisionYmlEntry {
* `langId/pageSlug`
*/
page: string;
/**
* revisionのリストは末尾が最新のはず。
*/
rev: SectionRevision[];
}
export interface SectionRevision {
Expand Down Expand Up @@ -184,6 +187,25 @@ export async function getRevisions(
];
}

const commitDateCache = new Map<string, Promise<Date>>();
export async function getCommitDate(id: string): Promise<Date> {
if (commitDateCache.has(id)) {
return commitDateCache.get(id)!;
}
const p = (async () => {
const commitInfoYml = await readPublicFile(`docs/commits.yml`);
const commitInfo = yaml.load(commitInfoYml) as Record<string, string>;
const timestamp = commitInfo[id];
if (timestamp) {
return new Date(timestamp);
} else {
throw new Error(`Commit date for id=${id} not found`);
}
})();
commitDateCache.set(id, p);
return p;
}

/**
* public/docs/{lang}/{pageId}/ 以下のmdファイルを結合して MarkdownSection[] を返す。
*/
Expand Down
15 changes: 15 additions & 0 deletions app/robots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { MetadataRoute } from "next";

export const dynamic = "force-static";

const origin = "https://my-code.utcode.net";

export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: "*",
allow: "/",
},
sitemap: `${origin}/sitemap.xml`,
};
}
53 changes: 53 additions & 0 deletions app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { MetadataRoute } from "next";
import {
getCommitDate,
getMarkdownSections,
getPagesList,
getRevisions,
} from "./lib/docs";

export const dynamic = "force-static";

const origin = "https://my-code.utcode.net";

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const pagesList = await getPagesList();

return [
{
url: origin,
changeFrequency: "monthly",
priority: 1,
},
...(await Promise.all(
pagesList
.map((lang) =>
lang.pages.map(async (page) => {
const sections = await getMarkdownSections(lang.id, page.slug);
const sectionsDate = await Promise.all(
sections.map((s) =>
getRevisions(s.id)
.then((revisions) => revisions?.rev.at(-1))
.then((lastRev) =>
lastRev ? getCommitDate(lastRev.git) : null
)
)
);
return {
url: `${origin}/${lang.id}/${page.slug}`,
priority: 0.8,
changeFrequency: "monthly",
lastModified: sectionsDate.reduce(
(latest: Date, date: Date | null) => {
if (!date) return latest;
return date > latest ? date : latest;
},
new Date(0)
),
} satisfies MetadataRoute.Sitemap[number];
})
)
.flat()
)),
];
}
2 changes: 2 additions & 0 deletions public/docs/commits.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This file will be updated by CI. Do not edit manually.
de11512: '2026-03-08T14:27:20+09:00'
23 changes: 23 additions & 0 deletions scripts/checkDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ if (doWrite) {
}).trim();
}

let hasNewRevision = false;

const langEntries = await getPagesList();

const revisionsPrevYml = existsSync(join(docsDir, "revisions.yml"))
Expand All @@ -70,6 +72,7 @@ for (const lang of langEntries) {
// ドキュメントが変更された場合
console.warn(`${section.id} has new md5: ${section.md5}`);
if (doWrite) {
hasNewRevision = true;
revisions[section.id].rev.push({
md5: section.md5,
git: commit,
Expand All @@ -83,6 +86,7 @@ for (const lang of langEntries) {
// ドキュメントが新規追加された場合
console.warn(`${section.id} is new section`);
if (doWrite) {
hasNewRevision = true;
revisions[section.id] = {
rev: [
{
Expand Down Expand Up @@ -142,4 +146,23 @@ if (doWrite) {
revisionsYml,
"utf-8"
);

if (hasNewRevision) {
const commitsYml = yaml.load(
await readFile(join(docsDir, "commits.yml"), "utf-8")
) as Record<string, string>;
commitsYml[commit] = execFileSync(
"git",
["show", "-s", "--format=%cI", commit],
{
encoding: "utf8",
}
).trim();
await writeFile(
join(docsDir, "commits.yml"),
"# This file will be updated by CI. Do not edit manually.\n" +
yaml.dump(commitsYml, { sortKeys: true }),
"utf-8"
);
}
}
Loading