From d450e3cb68b47ef022d5ae93685e1314af439da5 Mon Sep 17 00:00:00 2001 From: Guoqi Sun Date: Mon, 13 Jan 2025 05:36:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20support=20multiple=20languages=20?= =?UTF-8?q?=E2=80=8B=E2=80=8Bfor=20rss.xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/astro/header.astro | 9 ++++-- src/pages/[lang]/rss.xml.ts | 34 +++++++++++++++++++++ src/pages/{atom/index.xml.ts => rss.xml.ts} | 5 +-- 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 src/pages/[lang]/rss.xml.ts rename src/pages/{atom/index.xml.ts => rss.xml.ts} (79%) diff --git a/src/components/astro/header.astro b/src/components/astro/header.astro index bd8ee72..84caf75 100644 --- a/src/components/astro/header.astro +++ b/src/components/astro/header.astro @@ -10,14 +10,19 @@ const config = lang === "zh" ? zh : en ---
- +
{config.siteName}
{ config.rss && ( - + ) diff --git a/src/pages/[lang]/rss.xml.ts b/src/pages/[lang]/rss.xml.ts new file mode 100644 index 0000000..5bdd1ef --- /dev/null +++ b/src/pages/[lang]/rss.xml.ts @@ -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: "", + }) +} diff --git a/src/pages/atom/index.xml.ts b/src/pages/rss.xml.ts similarity index 79% rename from src/pages/atom/index.xml.ts rename to src/pages/rss.xml.ts index 142aa73..fe9e3e7 100644 --- a/src/pages/atom/index.xml.ts +++ b/src/pages/rss.xml.ts @@ -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,