summaryrefslogtreecommitdiff
path: root/src/pages/rss.xml.ts
blob: 61adea7101ce67d6ab482dd176b8d7d5b2ef7353 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import rss from "@astrojs/rss";
import type { APIContext } from "astro";
import sanitizeHtml from "sanitize-html";
import { getAllPosts } from "@/data/post";
import { siteConfig } from "@/site.config";
import { markdownToHtml } from "@/utils/markdown";

export const GET = async (context: APIContext) => {
	const posts = await getAllPosts();

	return rss({
		title: siteConfig.title,
		description: siteConfig.description,
		site: context.site || import.meta.env.SITE,
		items: posts.map((post) => {
			const htmlContent = post.rendered?.html || markdownToHtml(post.body || "");

			return {
				title: post.data.title,
				description: post.data.description,
				pubDate: post.data.publishDate,
				link: `posts/${post.id}/`,
				author: post.data.author,
				content: sanitizeHtml(htmlContent, {
					allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]),
				}),
			};
		}),
		customData: `<atom:link href="${context.site}rss.xml" rel="self" type="application/rss+xml" xmlns:atom="http://www.w3.org/2005/Atom" />`,
	});
};