feat: support multiple languages for rss.xml
This commit is contained in:
parent
085175509b
commit
d450e3cb68
3 changed files with 44 additions and 4 deletions
|
|
@ -10,14 +10,19 @@ const config = lang === "zh" ? zh : en
|
|||
---
|
||||
|
||||
<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>
|
||||
</a>
|
||||
|
||||
<div class="flex items-center gap-6">
|
||||
{
|
||||
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 />
|
||||
</a>
|
||||
)
|
||||
|
|
|
|||
34
src/pages/[lang]/rss.xml.ts
Normal file
34
src/pages/[lang]/rss.xml.ts
Normal 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: "",
|
||||
})
|
||||
}
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
import rss from "@astrojs/rss"
|
||||
import { zh as config } from "~/config"
|
||||
import { defaultLanguage, en, zh } from "~/config"
|
||||
import { getPostsByLocale } from "~/utils"
|
||||
|
||||
export async function GET() {
|
||||
const posts = await getPostsByLocale("zh")
|
||||
const posts = await getPostsByLocale(defaultLanguage)
|
||||
const config = defaultLanguage === "en" ? en : zh
|
||||
|
||||
return rss({
|
||||
title: config.meta.title,
|
||||
Loading…
Add table
Reference in a new issue