From c6bc10d6cd4f732718730ea59618d52385737ff5 Mon Sep 17 00:00:00 2001 From: passthem Date: Wed, 1 Apr 2026 02:40:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20RSS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/apis/legacy/blog.ts | 6 ++++++ src/pages/rss.xml.ts | 35 ++++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/lib/apis/legacy/blog.ts b/src/lib/apis/legacy/blog.ts index b484619..231383a 100644 --- a/src/lib/apis/legacy/blog.ts +++ b/src/lib/apis/legacy/blog.ts @@ -13,6 +13,12 @@ export type ListBlogItemType = { featured_image: null | { image_url: string } + + author: { + id: number + username: string + nickname: string + } } export const listBlogs = async ({ diff --git a/src/pages/rss.xml.ts b/src/pages/rss.xml.ts index 59223ca..aae61aa 100644 --- a/src/pages/rss.xml.ts +++ b/src/pages/rss.xml.ts @@ -1,6 +1,13 @@ -import rss from '@astrojs/rss' +import rss, { type RSSFeedItem } from '@astrojs/rss' import type { APIRoute } from 'astro' -import { listBlogs, type ListBlogItemType } from '../lib/apis/legacy/blog' +import { + getBlog, + listBlogs, + type ListBlogItemType, +} from '../lib/apis/legacy/blog' +import { ensureShikiEngine } from '../lib/markdown' +import { toMarkdocTree } from '../lib/markdoc' +import Markdoc from '@markdoc/markdoc' export const prerender = false @@ -19,16 +26,30 @@ export const GET = (async (context) => { } const site = context.site || 'https://passthem.top' + await ensureShikiEngine() return rss({ title: '小帕的小窝', description: '小帕和他朋友们的博客', site, - items: blogs.map((blog) => ({ - title: blog.title, - link: `${site}/blogs/${blog.id}`, - pubDate: new Date(blog.created_at), - })), + items: await Promise.all( + blogs.map(async (blog) => { + const blogContent = + (await getBlog(blog.id))?.content || '博客内容暂不可用' + const blogTree = await toMarkdocTree(blogContent) + const html = Markdoc.renderers.html(blogTree) + + const rssItem: RSSFeedItem = { + title: blog.title, + description: `一篇由 ${blog.author.nickname} 写的博客`, + link: `${site}/blogs/${blog.id}`, + pubDate: new Date(blog.created_at), + content: html, + } + + return rssItem + }), + ), customData: `zh-hans`, }) }) satisfies APIRoute