aboutsummaryrefslogtreecommitdiffhomepage
path: root/astro.config.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'astro.config.mjs')
-rw-r--r--astro.config.mjs45
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({