summaryrefslogtreecommitdiff
path: root/src/pages/micro/rss.xml.ts
blob: 1fd6f53712cc359c95fd104b4d69dd58ebfcd5e9 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { getCollection } from "astro:content";
import rss from "@astrojs/rss";
import type { APIContext } from "astro";
import { siteConfig } from "@/site.config";

export const GET = async (context: APIContext) => {
	// Get only Pleroma posts
	const allMicro = await getCollection("micro").catch(() => []); // Fallback to empty array if micro collection fails

	// Sort all micro posts
	const allMicroPosts = allMicro.sort(
		(a, b) => b.data.publishDate.getTime() - a.data.publishDate.getTime(),
	);

	// Generate RSS items with full content and images
	const items = allMicroPosts.map((post) => {
		// Get the pre-rendered HTML from the post
		let fullContent = post.rendered?.html || post.body || "";

		// Append images if available
		if (post.data.attachments && post.data.attachments.length > 0) {
			const imagesHtml = post.data.attachments
				.map(
					(att: { url: string; type: string }) =>
						`<p><img src="${att.url}" alt="Attachment" style="max-width: 100%; height: auto;" /></p>`,
				)
				.join("");
			fullContent += imagesHtml;
		}

		return {
			title: post.data.title,
			pubDate: post.data.publishDate,
			link: `micro/${post.id}/`,
			description: post.data.description,
			content: fullContent,
		};
	});

	const site = context.site || import.meta.env.SITE;

	return rss({
		title: siteConfig.title,
		description: siteConfig.description,
		site,
		items,
		customData: `<atom:link href="${site}micro/rss.xml" rel="self" type="application/rss+xml" xmlns:atom="http://www.w3.org/2005/Atom" />`,
	});
};