feat: support multiple languages ​​for rss.xml

This commit is contained in:
Guoqi Sun 2025-01-13 05:36:24 +08:00
parent 085175509b
commit d450e3cb68
3 changed files with 44 additions and 4 deletions

View file

@ -10,14 +10,19 @@ const config = lang === "zh" ? zh : en
--- ---
<header class="flex h-24 w-full items-center justify-between"> <header class="flex h-24 w-full items-center justify-between">
<a href="/" aria-label={`${config.siteName} - 首页`}> <a href="/" aria-label={`${config.siteName}`}>
<div class="text-2xl font-semibold">{config.siteName}</div> <div class="text-2xl font-semibold">{config.siteName}</div>
</a> </a>
<div class="flex items-center gap-6"> <div class="flex items-center gap-6">
{ {
config.rss && ( config.rss && (
<a href="/atom.xml" target="_blank" aria-label="RSS" title="RSS"> <a
href={"/" + lang + "/rss.xml"}
target="_blank"
aria-label="RSS"
title="RSS"
>
<Rss /> <Rss />
</a> </a>
) )

View file

@ -0,0 +1,34 @@
import rss from "@astrojs/rss"
import { en, zh } from "~/config"
import { getPostsByLocale } from "~/utils"
import { getLanguagePaths } from "~/utils/langs"
export function getStaticPaths() {
return getLanguagePaths()
}
export async function GET(request: { url: URL }) {
const isEn = request.url.pathname.includes("en")
const lang = isEn ? "en" : "zh"
const config = isEn ? en : zh
const posts = await getPostsByLocale(lang)
return rss({
title: config.meta.title,
description: config.meta.description,
site:
process.env.NODE_ENV === "development"
? "http://localhost:4321"
: config.meta.url,
items: posts.map((post: any) => ({
title: post.data.title,
description: post.data.description,
pubDate: post.data.pubDate,
link: `/posts/${post.id}/`,
content: post.rendered ? post.rendered.html : post.data.description,
})),
customData: "",
})
}

View file

@ -1,9 +1,10 @@
import rss from "@astrojs/rss" import rss from "@astrojs/rss"
import { zh as config } from "~/config" import { defaultLanguage, en, zh } from "~/config"
import { getPostsByLocale } from "~/utils" import { getPostsByLocale } from "~/utils"
export async function GET() { export async function GET() {
const posts = await getPostsByLocale("zh") const posts = await getPostsByLocale(defaultLanguage)
const config = defaultLanguage === "en" ? en : zh
return rss({ return rss({
title: config.meta.title, title: config.meta.title,