|
| 1 | +import rss from "@astrojs/rss"; |
| 2 | +import { getSortedPosts } from "@utils/content-utils"; |
| 3 | +import { url } from "@utils/url-utils"; |
| 4 | +import type { APIContext } from "astro"; |
| 5 | +import MarkdownIt from "markdown-it"; |
| 6 | +import sanitizeHtml from "sanitize-html"; |
| 7 | +import { siteConfig } from "@/config"; |
| 8 | + |
| 9 | +const parser = new MarkdownIt(); |
| 10 | + |
| 11 | +function stripInvalidXmlChars(str: string): string { |
| 12 | + return str.replace( |
| 13 | + // biome-ignore lint/suspicious/noControlCharactersInRegex: https://www.w3.org/TR/xml/#charsets |
| 14 | + /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFDD0-\uFDEF\uFFFE\uFFFF]/g, |
| 15 | + "", |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +export async function GET(context: APIContext) { |
| 20 | + const blog = await getSortedPosts(); |
| 21 | + |
| 22 | + return rss({ |
| 23 | + title: siteConfig.title, |
| 24 | + description: siteConfig.subtitle || "No description", |
| 25 | + site: context.site ?? "https://fuwari.vercel.app", |
| 26 | + items: blog.map((post) => { |
| 27 | + const content = |
| 28 | + typeof post.body === "string" ? post.body : String(post.body || ""); |
| 29 | + const cleanedContent = stripInvalidXmlChars(content); |
| 30 | + return { |
| 31 | + title: post.data.title, |
| 32 | + pubDate: post.data.published, |
| 33 | + description: post.data.description || "", |
| 34 | + link: url(`/posts/${post.slug}/`), |
| 35 | + content: sanitizeHtml(parser.render(cleanedContent), { |
| 36 | + allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]), |
| 37 | + }), |
| 38 | + }; |
| 39 | + }), |
| 40 | + customData: `<language>${siteConfig.lang}</language>`, |
| 41 | + }); |
| 42 | +} |
0 commit comments