aboutsummaryrefslogtreecommitdiff
path: root/src/pages/feed.xml.js
blob: 005f2d810dfabebc2ef214acdcdfe0e58d6f58d0 (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { getCollection } from "astro:content";
import MarkdownIt from "markdown-it";
import rss from "@astrojs/rss";
import sanitizeHtml from "sanitize-html";
import { config } from "../config";

const parser = new MarkdownIt({ html: true, linkify: true });

export async function GET(context) {
	const title = "RSS Feed | Valentin Popov Blog";
	const description = "Follow the latest posts from Valentin Popov via RSS.";

	const posts = (await getCollection("blog", ({ data }) => data.draft !== true)).sort((a, b) => b.data.datePublished.getTime() - a.data.datePublished.getTime());

	const feedUrl = new URL("/feed.xml", context.site).toString();

	return rss({
		title,
		description,
		site: context.site,
		xmlns: {
			atom: "http://www.w3.org/2005/Atom",
			content: "http://purl.org/rss/1.0/modules/content/",
			dc: "http://purl.org/dc/elements/1.1/",
		},
		customData: [`<language>en</language>`, `<atom:link href="${feedUrl}" rel="self" type="application/rss+xml"/>`].join(""),
		items: posts.map((post) => ({
			title: post.data.title,
			description: post.data.description,
			link: `/blog/${post.id}/`,
			pubDate: post.data.datePublished,
			author: `${config.author.email} (${config.author.name})`,
			content: sanitizeHtml(parser.render(post.body ?? ""), {
				allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img", "pre", "code", "span"]),
				allowedAttributes: {
					...sanitizeHtml.defaults.allowedAttributes,
					img: ["src", "alt", "title", "loading", "decoding"],
					code: ["class"],
					span: ["class", "style"],
					pre: ["class", "style"],
					a: ["href", "name", "target", "rel"],
				},
			}),
			customData: `<dc:language>${post.data.lang}</dc:language>`,
		})),
	});
}