This commit is contained in:
@ -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<string>({
|
||||
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
|
||||
? {
|
||||
|
||||
Reference in New Issue
Block a user