This commit is contained in:
2026-01-01 19:02:59 +08:00
parent 315ca37dba
commit ac944d05ec
9 changed files with 107 additions and 56 deletions

View File

@@ -0,0 +1,64 @@
export default defineEventHandler(async (event) => {
const docs = await queryCollection(event, "content").all();
const urls: Array<{ loc: string; lastmod: string }> = [];
// 添加静态页面
urls.push({ loc: "/", lastmod: new Date().toISOString() });
urls.push({ loc: "/about", lastmod: new Date().toISOString() });
urls.push({ loc: "/archive", lastmod: new Date().toISOString() });
urls.push({ loc: "/categories", lastmod: new Date().toISOString() });
urls.push({ loc: "/tags", lastmod: new Date().toISOString() });
// 添加博客文章
docs.forEach((doc: Record<string, unknown>) => {
if (typeof doc.path === "string" && !doc.path.includes("about")) {
urls.push({
loc: doc.path,
lastmod: typeof doc._mtime === "string" ? doc._mtime : new Date().toISOString(),
});
}
});
// 获取所有分类
const categories = new Set<string>();
docs.forEach((doc: Record<string, unknown>) => {
if (doc.categories && Array.isArray(doc.categories)) {
doc.categories.forEach((cat: unknown) => {
if (typeof cat === "string") categories.add(cat);
});
} else if (doc.category && Array.isArray(doc.category)) {
doc.category.forEach((cat: unknown) => {
if (typeof cat === "string") categories.add(cat);
});
} else if (typeof doc.category === "string") {
categories.add(doc.category);
}
});
categories.forEach((category) => {
urls.push({
loc: `/categories/${category}`,
lastmod: new Date().toISOString(),
});
});
// 获取所有标签
const tags = new Set<string>();
docs.forEach((doc: Record<string, unknown>) => {
if (doc.tags && Array.isArray(doc.tags)) {
doc.tags.forEach((tag: unknown) => {
if (typeof tag === "string") tags.add(tag);
});
}
});
tags.forEach((tag) => {
urls.push({
loc: `/tags/${tag}`,
lastmod: new Date().toISOString(),
});
});
return urls;
});