Skip to content

Commit 0c874a6

Browse files
committed
WIP: Test setting modified on by git date
1 parent b2c1ded commit 0c874a6

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

astro.config.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import react from "@astrojs/react";
1111

1212
import { readdir } from "fs/promises";
1313
import { fileURLToPath } from "url";
14+
import { execSync } from "child_process";
15+
import { existsSync } from "fs";
1416

1517
import remarkValidateImages from "./src/plugins/remark/validate-images";
1618

@@ -59,6 +61,53 @@ const customCss = await autogenStyles();
5961

6062
const runLinkCheck = process.env.RUN_LINK_CHECK || false;
6163

64+
/**
65+
* Get the last Git modification date for a file
66+
* @param filePath - Absolute path to the file
67+
* @returns ISO date string or null if not available
68+
*/
69+
function getGitLastModified(filePath: string): string | null {
70+
try {
71+
const result = execSync(
72+
`git log -1 --format=%cI -- "${filePath}"`,
73+
{ encoding: "utf-8" }
74+
).trim();
75+
return result || null;
76+
} catch (_error) {
77+
return null;
78+
}
79+
}
80+
81+
/**
82+
* Convert a sitemap URL to the corresponding source file path
83+
* @param url - The full URL from the sitemap
84+
* @returns Absolute file path or null if not found
85+
*/
86+
function urlToFilePath(url: string): string | null {
87+
try {
88+
const urlObj = new URL(url);
89+
const pathname = urlObj.pathname.replace(/\/$/, ""); // Remove trailing slash
90+
91+
// Try different file extensions and paths
92+
const possiblePaths = [
93+
`./src/content/docs${pathname}.md`,
94+
`./src/content/docs${pathname}.mdx`,
95+
`./src/content/docs${pathname}/index.md`,
96+
`./src/content/docs${pathname}/index.mdx`,
97+
];
98+
99+
for (const path of possiblePaths) {
100+
if (existsSync(path)) {
101+
return path;
102+
}
103+
}
104+
105+
return null;
106+
} catch (_error) {
107+
return null;
108+
}
109+
}
110+
62111
// https://astro.build/config
63112
export default defineConfig({
64113
site: "https://developers.cloudflare.com",
@@ -194,7 +243,16 @@ export default defineConfig({
194243
return true;
195244
},
196245
serialize(item) {
197-
item.lastmod = new Date().toISOString();
246+
const filePath = urlToFilePath(item.url);
247+
if (filePath) {
248+
const gitDate = getGitLastModified(filePath);
249+
if (gitDate) {
250+
item.lastmod = gitDate;
251+
}
252+
} else {
253+
console.warn(`[sitemap] Could not find last modified for ${item.url} - setting to now`);
254+
item.lastmod = new Date().toISOString();
255+
}
198256
return item;
199257
},
200258
}),

0 commit comments

Comments
 (0)