From 7a3059f486060c86f039804ba2ee11bd642cb5f0 Mon Sep 17 00:00:00 2001 From: passthem Date: Tue, 7 Apr 2026 13:19:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20RSS=20=E4=BE=A7=E6=B8=B2?= =?UTF-8?q?=E6=9F=93=E5=92=8C=E6=8A=93=E5=8F=96=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/rss.xml.ts | 45 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/src/pages/rss.xml.ts b/src/pages/rss.xml.ts index 68622f4..53dca01 100644 --- a/src/pages/rss.xml.ts +++ b/src/pages/rss.xml.ts @@ -8,9 +8,45 @@ import { import { ensureShikiEngine } from '../lib/markdown' // import { toMarkdocTree } from '../lib/markdoc' import Markdoc from '@markdoc/markdoc' +import KeyV from 'keyv' +import { createHash } from 'node:crypto' export const prerender = false +const renderCache = new KeyV({ + namespace: 'markdoc-rss', +}) + +const _render = async (content: string) => { + const key = 'html:' + createHash('sha256').update(content).digest('hex') + const cached = await renderCache.get(key) + + if (cached) { + return cached + } + + const blogTree = Markdoc.transform(Markdoc.parse(content)) + const html = Markdoc.renderers.html(blogTree) + + await renderCache.set(key, html, 1000 * 60 * 60 * 24) + return html +} + +const _getBlog = async (blogId: number) => { + const key = `raw:blog-${blogId}` + const cached = await renderCache.get(key) + + if (cached) { + return cached + } + + const content = (await getBlog(blogId))?.content + if (content) { + await renderCache.set(key, content, 1000 * 60 * 60 * 12) + } + return content +} + export const GET = (async (context) => { let blogs: ListBlogItemType[] = [] let pid = 0 @@ -34,18 +70,13 @@ export const GET = (async (context) => { site, items: await Promise.all( blogs.map(async (blog) => { - const blogContent = - (await getBlog(blog.id))?.content || '博客内容暂不可用' - // const blogTree = await toMarkdocTree(blogContent) - const blogTree = Markdoc.transform(Markdoc.parse(blogContent)) - const html = Markdoc.renderers.html(blogTree) - + const blogContent = (await _getBlog(blog.id)) || '博客内容暂不可用' const rssItem: RSSFeedItem = { title: blog.title, description: `一篇由 ${blog.author.nickname} 写的博客`, link: `${site}/blogs/${blog.id}`, pubDate: new Date(blog.created_at), - content: html, + content: await _render(blogContent), author: blog.author.nickname, enclosure: blog.featured_image ? {