Skip to content

Commit fd40281

Browse files
author
rayepeng
committed
add rss
1 parent d62b437 commit fd40281

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

astro.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { pluginCustomCopyButton } from "./src/plugins/expressive-code/custom-cop
2828
export default defineConfig({
2929
site: "https://raye.ink/",
3030
base: "/",
31-
trailingSlash: "always",
31+
trailingSlash: "ignore",
3232
integrations: [
3333
tailwind({
3434
nesting: true,

src/pages/feed.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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

Comments
 (0)