master
 1import { siteConfig } from '@/config';
 2import { getContainerRenderer as mdxContainerRenderer } from '@astrojs/mdx';
 3import rss from '@astrojs/rss';
 4import Markdown from '@components/utils/Markdown.astro';
 5import { getAllReferences, getSortedPosts } from '@utils/content-utils';
 6import type { APIRoute } from 'astro';
 7import { experimental_AstroContainer } from 'astro/container';
 8import { loadRenderers } from 'astro:container';
 9import { render } from 'astro:content';
10import sanitizeHtml from 'sanitize-html';
11
12const renderers = await loadRenderers([mdxContainerRenderer()]);
13const container = await experimental_AstroContainer.create({
14  renderers,
15});
16
17export const GET: APIRoute = async function (context) {
18  const posts = await getSortedPosts();
19  const allReferences = await getAllReferences();
20  const rssItems = await Promise.all(
21    posts.map(async (post) => ({
22      title: post.data.title,
23      pubDate: post.data.published,
24      description: post.data.description,
25      link: `/posts/${post.data.slug}`,
26      content: sanitizeHtml(
27        await container.renderToString(Markdown, {
28          slots: {
29            default: await container.renderToString(
30              // eslint-disable-next-line @typescript-eslint/no-explicit-any
31              (await render({ collection: 'posts', ...post } as any)).Content
32            ),
33          },
34          props: {
35            'bidirectional-references': await (async () => {
36              const allRefByCurrent = allReferences.filter((it) => it.refBy.id === post.id);
37              const { remarkPluginFrontmatter } = await render({
38                collection: 'posts',
39                ...post,
40                // eslint-disable-next-line @typescript-eslint/no-explicit-any
41              } as any);
42
43              const references: {
44                reference: string;
45                context: string;
46                id: string;
47              }[] =
48                (
49                  remarkPluginFrontmatter.references as {
50                    reference: string;
51                    context: string;
52                    id: string;
53                  }[]
54                )?.map((it) => ({
55                  reference: it.reference.split('#')[0],
56                  context: it.context,
57                  id: it.id,
58                })) || [];
59              return { references, allRefByCurrent };
60            })(),
61          },
62        })
63      ),
64    }))
65  );
66  return rss({
67    title: siteConfig.title,
68    description: siteConfig.subtitle,
69    site: context.site || '',
70    items: rssItems,
71  });
72};