From 29650452a1870c5a05cda0401270a3bc0132556d Mon Sep 17 00:00:00 2001 From: KazooTTT Date: Mon, 25 Nov 2024 21:47:51 +0800 Subject: [PATCH] feat: add new rss --- package.json | 2 +- pnpm-lock.yaml | 2 +- src/components/layout/Header.astro | 2 +- src/pages/rss.xml.js | 28 ------------------------- src/pages/rss.xml.ts | 33 ++++++++++++++++++++++++++++++ src/site.config.ts | 2 +- 6 files changed, 37 insertions(+), 32 deletions(-) delete mode 100644 src/pages/rss.xml.js create mode 100644 src/pages/rss.xml.ts diff --git a/package.json b/package.json index 0e7962a..b8caa55 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "@astrojs/cloudflare": "^11.2.0", "@astrojs/mdx": "^2.1.1", "@astrojs/react": "^3.6.2", - "@astrojs/rss": "^4.0.5", + "@astrojs/rss": "^4.0.9", "@astrojs/sitemap": "^3.1.1", "@astrojs/tailwind": "^5.1.0", "@giscus/react": "^3.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9eb2147..63ee1d6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -21,7 +21,7 @@ importers: specifier: ^3.6.2 version: 3.6.3(@types/node@22.9.3)(@types/react-dom@18.3.1)(@types/react@18.3.12)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@astrojs/rss': - specifier: ^4.0.5 + specifier: ^4.0.9 version: 4.0.9 '@astrojs/sitemap': specifier: ^3.1.1 diff --git a/src/components/layout/Header.astro b/src/components/layout/Header.astro index cee1dd5..f3fca8c 100644 --- a/src/components/layout/Header.astro +++ b/src/components/layout/Header.astro @@ -15,7 +15,7 @@ import { Image } from 'astro:assets' class='mr-2 h-8 w-auto rounded-full sm:hidden' loading='eager' /> - + diff --git a/src/pages/rss.xml.js b/src/pages/rss.xml.js deleted file mode 100644 index f5866f5..0000000 --- a/src/pages/rss.xml.js +++ /dev/null @@ -1,28 +0,0 @@ -import rss from '@astrojs/rss' -import { siteConfig } from '@/site-config' -import { getAllSortedPosts } from '@/utils' - -export const GET = async () => { - const posts = await getAllSortedPosts() - - const items = await Promise.all( - posts.map(async (post) => { - const entry = post - const body = entry.body - - return { - title: post.data.title ?? '', - description: JSON.stringify(body), - pubDate: post.data.date, - link: `/blog/${post.slug}` - } - }) - ) - - return rss({ - title: siteConfig.title, - description: siteConfig.description + '\nfeedId:76245438397618182+userId:62156866798228480', - site: import.meta.env.SITE, - items - }) -} diff --git a/src/pages/rss.xml.ts b/src/pages/rss.xml.ts new file mode 100644 index 0000000..e7e130a --- /dev/null +++ b/src/pages/rss.xml.ts @@ -0,0 +1,33 @@ +import { siteConfig } from '@/site-config' +import { getAllSortedPosts } from '@/utils' +import rss from '@astrojs/rss' +import type { APIContext } from 'astro' + +export async function GET(context: APIContext) { + const blog = await getAllSortedPosts() + return rss({ + // `` field in output xml + title: siteConfig.title, + // `<description>` field in output xml + description: siteConfig.description, + // Pull in your project "site" from the endpoint context + // https://docs.astro.build/en/reference/api-reference/#contextsite + site: context.site!, + // Array of `<item>`s in output xml + // See "Generating items" section for examples using content collections and glob imports + items: blog.map((post) => ({ + title: post.data.title, + pubDate: post.data.date, + description: post.data.description ?? '', + // Compute RSS link from post `slug` + // This example assumes all posts are rendered as `/blog/[slug]` routes + link: `/blog/${post.slug}/` + })), + // Add custom XML elements + customData: ` + <follow_challenge> + <feedId>83074007039123456</feedId> + <userId>62156866798228480</userId> + </follow_challenge>` + }) +} diff --git a/src/site.config.ts b/src/site.config.ts index 5ed35b0..ad38163 100644 --- a/src/site.config.ts +++ b/src/site.config.ts @@ -5,7 +5,7 @@ export const siteConfig: SiteConfig = { // Used as both a meta property (src/components/BaseHead.astro L:31 + L:49) & the generated satori png (src/pages/og-image/[slug].png.ts) author: 'KazooTTT', // Meta property used to construct the meta title property, found in src/components/BaseHead.astro L:11 - title: 'KazooTTT Blog', + title: '声控烤箱的博客|KazooTTT Blog', // Meta property used as the default description meta property description: '声控烤箱 | 我喜欢的烤箱是声控的 | 前端小透明', // HTML lang property, found in src/layouts/Base.astro L:18