修复sitemap

This commit is contained in:
lsy 2025-03-10 21:12:16 +08:00
parent 5ca6cd3799
commit 45fb87a4a2

View File

@ -15,9 +15,14 @@ import vercel from '@astrojs/vercel';
function getArticleDate(articleId) { function getArticleDate(articleId) {
try { try {
// 处理多级目录的文章路径
const mdPath = path.join(process.cwd(), 'src/content', articleId + '.md'); const mdPath = path.join(process.cwd(), 'src/content', articleId + '.md');
if (fs.existsSync(mdPath)) { const mdxPath = path.join(process.cwd(), 'src/content', articleId + '.mdx');
const content = fs.readFileSync(mdPath, 'utf-8');
let filePath = fs.existsSync(mdPath) ? mdPath : mdxPath;
if (fs.existsSync(filePath)) {
const content = fs.readFileSync(filePath, 'utf-8');
const match = content.match(/date:\s*(\d{4}-\d{2}-\d{2})/); const match = content.match(/date:\s*(\d{4}-\d{2}-\d{2})/);
if (match) { if (match) {
return new Date(match[1]).toISOString(); return new Date(match[1]).toISOString();
@ -26,7 +31,7 @@ function getArticleDate(articleId) {
} catch (error) { } catch (error) {
console.error('Error reading article date:', error); console.error('Error reading article date:', error);
} }
return null; return new Date().toISOString(); // 如果没有日期,返回当前时间
} }
// https://astro.build/config // https://astro.build/config
@ -91,13 +96,11 @@ export default defineConfig({
// 从 URL 中提取文章 ID // 从 URL 中提取文章 ID
const articleId = item.url.replace(SITE_URL + '/articles/', '').replace(/\/$/, ''); const articleId = item.url.replace(SITE_URL + '/articles/', '').replace(/\/$/, '');
const publishDate = getArticleDate(articleId); const publishDate = getArticleDate(articleId);
if (publishDate) { return {
return { ...item,
...item, priority: 0.8,
priority: 0.8, lastmod: publishDate
lastmod: publishDate };
};
}
} }
// 其他页面 // 其他页面
else { else {
@ -117,9 +120,7 @@ export default defineConfig({
priority priority
}; };
} }
}, }
// 设置较小的条目限制,这样会自动分割成多个文件
entryLimit: 5
}) })
], ],