Commit d89b400

HPCesia <me@hpcesia.com>
2025-07-24 19:02:46
fix: wiki links in rss
1 parent ba4c552
Changed files (2)
src/pages/rss.xml.ts
@@ -1,21 +1,69 @@
 import { siteConfig } from '@/config';
+import { getContainerRenderer as mdxContainerRenderer } from '@astrojs/mdx';
 import rss from '@astrojs/rss';
-import { getSortedPosts } from '@utils/content-utils';
+import Markdown from '@components/utils/Markdown.astro';
+import { getAllReferences, getSortedPosts } from '@utils/content-utils';
 import type { APIRoute } from 'astro';
-import sanitizeHtml from 'sanitize-html';
+import { experimental_AstroContainer } from 'astro/container';
+import { loadRenderers } from 'astro:container';
+import { render } from 'astro:content';
+
+const renderers = await loadRenderers([mdxContainerRenderer()]);
+const container = await experimental_AstroContainer.create({
+  renderers,
+});
 
 export const GET: APIRoute = async function (context) {
   const posts = await getSortedPosts();
-  return rss({
-    title: siteConfig.title,
-    description: siteConfig.subtitle,
-    site: context.site || '',
-    items: posts.map((post) => ({
+  const allReferences = await getAllReferences();
+  const rssItems = await Promise.all(
+    posts.map(async (post) => ({
       title: post.data.title,
       pubDate: post.data.published,
       description: post.data.description,
       link: `/posts/${post.data.slug}`,
-      content: sanitizeHtml(post.rendered?.html || ''),
-    })),
+      content: await container.renderToString(Markdown, {
+        slots: {
+          default: await container.renderToString(
+            // eslint-disable-next-line @typescript-eslint/no-explicit-any
+            (await render({ collection: 'posts', ...post } as any)).Content
+          ),
+        },
+        props: {
+          'bidirectional-references': await (async () => {
+            const allRefByCurrent = allReferences.filter((it) => it.refBy.id === post.id);
+            const { remarkPluginFrontmatter } = await render({
+              collection: 'posts',
+              ...post,
+              // eslint-disable-next-line @typescript-eslint/no-explicit-any
+            } as any);
+
+            const references: {
+              reference: string;
+              context: string;
+              id: string;
+            }[] =
+              (
+                remarkPluginFrontmatter.references as {
+                  reference: string;
+                  context: string;
+                  id: string;
+                }[]
+              )?.map((it) => ({
+                reference: it.reference.split('#')[0],
+                context: it.context,
+                id: it.id,
+              })) || [];
+            return { references, allRefByCurrent };
+          })(),
+        },
+      }),
+    }))
+  );
+  return rss({
+    title: siteConfig.title,
+    description: siteConfig.subtitle,
+    site: context.site || '',
+    items: rssItems,
   });
 };
src/types/data.d.ts
@@ -18,6 +18,10 @@ export type BlogPost = {
   id: string;
   body: string;
   data: BlogPostData;
-  rendered?: RenderedContent;
+  rendered?: RenderedContent & {
+    headings?: MarkdownHeading[];
+    // eslint-disable-next-line @typescript-eslint/no-explicit-any
+    frontmatter?: Record<string, any>;
+  };
   filePath?: string;
 };