diff options
| author | Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> | 2026-10-07 06:39:31 +0900 |
|---|---|---|
| committer | Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> | 2026-10-07 06:39:31 +0900 |
| commit | cb743f53e7f720d54e9dce944a3ad41617a79f83 (patch) | |
| tree | 62a3951b8f3923a31f937d3ecaecff000fecf3a1 /astro.config.mjs | |
| parent | 6ce064dbec45dac5b890c3dc9a42dc42dccecaa8 (diff) | |
astro.config.mjs, package.json, scripts/indexnow.mjs: IndexNowとサイトマップlastmodを追加
IndexNowのキーファイルと送信スクリプト、npm run indexnow、各ページの最終更新日をlastmodに反映。
Diffstat (limited to 'astro.config.mjs')
| -rw-r--r-- | astro.config.mjs | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/astro.config.mjs b/astro.config.mjs index 46174d3..f6dd237 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,3 +1,4 @@ +import { execSync } from "node:child_process"; import { existsSync } from "node:fs"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; @@ -45,6 +46,42 @@ function publicDirIndexPlugin() { }; } +// ページの最終更新日を git の履歴から引く(サイトマップの lastmod 用)。 +function buildGitDateMap() { + const map = new Map(); + try { + const out = execSync("git log --no-merges --pretty=format:__C__%cI --name-only", { + encoding: "utf-8", + maxBuffer: 256 * 1024 * 1024, + }); + let date = ""; + for (const raw of out.split("\n")) { + const line = raw.trim(); + if (line.startsWith("__C__")) { + date = line.slice(5); + } else if (line && date && !map.has(line)) { + map.set(line, date); + } + } + } catch { + // git が使えない環境では lastmod を付けない。 + } + return map; +} +const gitDates = buildGitDateMap(); + +function lastmodFor(url) { + const path = url.replace(/^https?:\/\/[^/]+/, "").replace(/^\/+|\/+$/g, ""); + const bases = path ? [path, `${path}/index`] : ["index"]; + for (const base of bases) { + for (const ext of [".mdx", ".md"]) { + const date = gitDates.get(`src/content/docs/${base}${ext}`); + if (date) return new Date(date); + } + } + return undefined; +} + export default defineConfig({ site: "https://yasutakeyohei.com", base: "/", @@ -61,7 +98,13 @@ export default defineConfig({ plugins: [publicDirIndexPlugin()], }, integrations: [ - sitemap(), + sitemap({ + serialize(item) { + const lastmod = lastmodFor(item.url); + if (lastmod) item.lastmod = lastmod; + return item; + }, + }), updatesIntegration(), ogImagePlugin(), starlight({ |
