初始提交:时之刃的小站
基于 VermilionVoid(朱墨留白)主题的个人博客。 - 站点配置集中在 src/data/site.ts 与 src/data/profile.ts - 30 篇文章、8 条偶得、相册 - 评论使用自托管 twikoo - 部署走 deploy.sh(rsync 到树莓派 + 云服务器)
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import rss from "@astrojs/rss"
|
||||
import type { APIContext } from "astro"
|
||||
import MarkdownIt from "markdown-it"
|
||||
import sanitizeHtml from "sanitize-html"
|
||||
import { getSortedPosts } from "@/lib/posts"
|
||||
import { homeTitle, site } from "@/data/site"
|
||||
|
||||
const parser = new MarkdownIt()
|
||||
const FULL_CONTENT_LIMIT = 10
|
||||
|
||||
function stripInvalidXmlChars(str: string): string {
|
||||
return str.replace(
|
||||
/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFDD0-\uFDEF\uFFFE\uFFFF]/g,
|
||||
"",
|
||||
)
|
||||
}
|
||||
|
||||
export async function GET(context: APIContext) {
|
||||
const posts = await getSortedPosts(true)
|
||||
const publicPosts = posts.filter((post) => !post.data.encrypted)
|
||||
|
||||
return rss({
|
||||
title: homeTitle,
|
||||
description: site.description,
|
||||
site: context.site ?? "https://example.com",
|
||||
items: publicPosts.map((post, index) => {
|
||||
const content = typeof post.body === "string" ? post.body : String(post.body || "")
|
||||
const cleanedContent = stripInvalidXmlChars(content)
|
||||
const item = {
|
||||
title: post.data.title,
|
||||
pubDate: post.data.published,
|
||||
description: post.data.description || "",
|
||||
link: `/posts/${post.slug}/`,
|
||||
}
|
||||
|
||||
if (index >= FULL_CONTENT_LIMIT) return item
|
||||
|
||||
return {
|
||||
...item,
|
||||
content: sanitizeHtml(parser.render(cleanedContent), {
|
||||
allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]),
|
||||
}),
|
||||
}
|
||||
}),
|
||||
customData: `<language>${site.lang}</language>`,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user