diff options
| author | Dawid Rycerz <dawid@rycerz.xyz> | 2026-01-13 19:06:55 +0100 |
|---|---|---|
| committer | Dawid Rycerz <dawid@rycerz.xyz> | 2026-01-13 19:06:55 +0100 |
| commit | 42662ae3db2a2f2366942ab7d68f9441e03b29bb (patch) | |
| tree | cbeea04301500f180fc1c6973e3d7222469e5f45 /src | |
| parent | c4afcab46587ace0a9f3b22592f6d17c62ff9e0e (diff) | |
Add full content render for rss feed
Diffstat (limited to 'src')
| -rw-r--r-- | src/loaders/pleroma.ts | 16 | ||||
| -rw-r--r-- | src/pages/rss.xml.ts | 23 | ||||
| -rw-r--r-- | src/pages/tags/[tag]/rss.xml.ts | 23 | ||||
| -rw-r--r-- | src/utils/markdown.ts | 11 |
4 files changed, 44 insertions, 29 deletions
diff --git a/src/loaders/pleroma.ts b/src/loaders/pleroma.ts index 36ea4d6..83f60c2 100644 --- a/src/loaders/pleroma.ts +++ b/src/loaders/pleroma.ts @@ -1,6 +1,6 @@ import type { Loader } from "astro/loaders"; -import { marked } from "marked"; import TurndownService from "turndown"; +import { markdownToHtml } from "@/utils/markdown"; interface Logger { info: (message: string) => void; @@ -606,20 +606,6 @@ function replacePleromaLinks( return modifiedContent; } -function markdownToHtml(markdown: string): string { - // Configure marked options for safe rendering - marked.setOptions({ - breaks: true, // Convert line breaks to <br> - gfm: true, // GitHub flavored markdown - }); - - // Convert markdown to HTML - const html = marked.parse(markdown); - - // Return as string (marked.parse can return string or Promise<string>) - return typeof html === "string" ? html : ""; -} - function extractTitle(content: string): string { // Extract first line or first sentence as title const firstLine = content.split("\n")[0]; diff --git a/src/pages/rss.xml.ts b/src/pages/rss.xml.ts index d428cc9..61adea7 100644 --- a/src/pages/rss.xml.ts +++ b/src/pages/rss.xml.ts @@ -1,7 +1,9 @@ import rss from "@astrojs/rss"; import type { APIContext } from "astro"; +import sanitizeHtml from "sanitize-html"; import { getAllPosts } from "@/data/post"; import { siteConfig } from "@/site.config"; +import { markdownToHtml } from "@/utils/markdown"; export const GET = async (context: APIContext) => { const posts = await getAllPosts(); @@ -10,13 +12,20 @@ export const GET = async (context: APIContext) => { title: siteConfig.title, description: siteConfig.description, site: context.site || import.meta.env.SITE, - items: posts.map((post) => ({ - title: post.data.title, - description: post.data.description, - pubDate: post.data.publishDate, - link: `posts/${post.id}/`, - author: post.data.author, - })), + items: posts.map((post) => { + const htmlContent = post.rendered?.html || markdownToHtml(post.body || ""); + + return { + title: post.data.title, + description: post.data.description, + pubDate: post.data.publishDate, + link: `posts/${post.id}/`, + author: post.data.author, + content: sanitizeHtml(htmlContent, { + allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]), + }), + }; + }), customData: `<atom:link href="${context.site}rss.xml" rel="self" type="application/rss+xml" xmlns:atom="http://www.w3.org/2005/Atom" />`, }); }; diff --git a/src/pages/tags/[tag]/rss.xml.ts b/src/pages/tags/[tag]/rss.xml.ts index 3a61414..89e158c 100644 --- a/src/pages/tags/[tag]/rss.xml.ts +++ b/src/pages/tags/[tag]/rss.xml.ts @@ -1,7 +1,9 @@ import rss from "@astrojs/rss"; import type { APIContext } from "astro"; +import sanitizeHtml from "sanitize-html"; import { getAllPosts, getUniqueTags } from "@/data/post"; import { siteConfig } from "@/site.config"; +import { markdownToHtml } from "@/utils/markdown"; export async function getStaticPaths() { // Get all posts (including archived, now includes pleroma too) @@ -37,13 +39,20 @@ export const GET = async (context: APIContext) => { title: `${siteConfig.title} - ${tag}`, description: `Posts tagged with ${tag}`, site, - items: sortedPosts.map((post) => ({ - title: post.data.title, - description: post.data.description, - pubDate: post.data.publishDate, - link: `posts/${post.id}/`, - author: post.data.author, - })), + items: sortedPosts.map((post) => { + const htmlContent = post.rendered?.html || markdownToHtml(post.body || ""); + + return { + title: post.data.title, + description: post.data.description, + pubDate: post.data.publishDate, + link: `posts/${post.id}/`, + author: post.data.author, + content: sanitizeHtml(htmlContent, { + allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]), + }), + }; + }), customData: `<atom:link href="${site}tags/${tag}/rss.xml" rel="self" type="application/rss+xml" xmlns:atom="http://www.w3.org/2005/Atom" />`, }); }; diff --git a/src/utils/markdown.ts b/src/utils/markdown.ts new file mode 100644 index 0000000..946d8d8 --- /dev/null +++ b/src/utils/markdown.ts @@ -0,0 +1,11 @@ +import { marked } from "marked"; + +marked.setOptions({ + breaks: true, + gfm: true, +}); + +export function markdownToHtml(markdown: string): string { + const html = marked.parse(markdown); + return typeof html === "string" ? html : ""; +} |
