import rss from '@astrojs/rss';
import { experimental_AstroContainer as AstroContainer } from 'astro/container';
import { render } from 'astro:content';
import type { APIRoute } from 'astro';
import { catalog, documentHref } from '../lib/content';
import { site } from '../lib/site';
const escapeXml = (text: string) =>
text.replace(/&/g, '&').replace(//g, '>');
export const GET: APIRoute = async ({ site: origin }) => {
const { posts } = await catalog();
const container = await AstroContainer.create();
const items = await Promise.all(
posts.map(async (entry) => {
const { Content } = await render(entry);
const page = new URL(documentHref(entry), origin);
const content = (await container.renderToString(Content)).replace(
/\b(href|src)="([^"]+)"/g,
(_match, attribute: string, value: string) =>
`${attribute}="${new URL(value.replaceAll('&', '&'), page).href.replaceAll('&', '&')}"`,
);
return {
title: entry.data.title,
description: entry.data.excerpt,
pubDate: entry.data.date,
customData: entry.authors
.map(({ author }) => `${escapeXml(author.data.name)}`)
.join(''),
link: documentHref(entry),
content,
};
}),
);
return rss({
title: site.name,
description: `Latest writing from ${site.name}.`,
site: origin!,
xmlns: { atom: 'http://www.w3.org/2005/Atom', dc: 'http://purl.org/dc/elements/1.1/' },
customData: ``,
items,
});
};