From 1b377fc4efda1f1984a30c9d313766756b49bc80 Mon Sep 17 00:00:00 2001 From: Joe Adams Date: Tue, 3 Jun 2025 22:07:07 -0400 Subject: [PATCH] Update blog RSS feed with rendered content After migrating to Next.js in #2656, the blog RSS feed was missing the rendered markdown content for the blog. This uses the unified package, similar to how we render the blog entires, to render the entries to HTML inside the feed. Signed-off-by: Joe Adams --- package-lock.json | 16 ++++++++++++++++ package.json | 1 + src/app/blog/feed.xml/route.ts | 30 +++++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index daf4a765..3c422dd9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -30,6 +30,7 @@ "rehype-autolink-headings": "^7.1.0", "rehype-raw": "^7.0.0", "rehype-slug": "^6.0.0", + "rehype-stringify": "^10.0.1", "remark-frontmatter": "^5.0.0", "remark-gfm": "^4.0.1", "semver": "^7.7.1", @@ -8173,6 +8174,21 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/rehype-stringify": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/rehype-stringify/-/rehype-stringify-10.0.1.tgz", + "integrity": "sha512-k9ecfXHmIPuFVI61B9DeLPN0qFHfawM6RsuX48hoqlaKSF61RskNjSm1lI8PhBEM0MRdLxVVm4WmTqJQccH9mA==", + "license": "MIT", + "dependencies": { + "@types/hast": "^3.0.0", + "hast-util-to-html": "^9.0.0", + "unified": "^11.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/remark-frontmatter": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/remark-frontmatter/-/remark-frontmatter-5.0.0.tgz", diff --git a/package.json b/package.json index e389ea9f..bd94de76 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "rehype-autolink-headings": "^7.1.0", "rehype-raw": "^7.0.0", "rehype-slug": "^6.0.0", + "rehype-stringify": "^10.0.1", "remark-frontmatter": "^5.0.0", "remark-gfm": "^4.0.1", "semver": "^7.7.1", diff --git a/src/app/blog/feed.xml/route.ts b/src/app/blog/feed.xml/route.ts index 31956b01..4c8fc47c 100644 --- a/src/app/blog/feed.xml/route.ts +++ b/src/app/blog/feed.xml/route.ts @@ -1,7 +1,27 @@ -import { getAllPosts } from "@/blog-helpers"; +import { getAllPosts, getPostFileContent } from "@/blog-helpers"; import { Feed } from "feed"; + +import {unified} from 'unified'; +import remarkParse from 'remark-parse'; +import remarkFrontmatter from "remark-frontmatter"; +import remarkGfm from 'remark-gfm'; +import remarkRehype from 'remark-rehype'; +import rehypeStringify from 'rehype-stringify'; + import docsConfig from "../../../../docs-config"; +function processPostContent(content: string): string { + const processor = unified() + .use(remarkParse) + .use(remarkFrontmatter) // Parse YAML frontmatter + .use(remarkGfm) // GitHub Flavored Markdown + .use(remarkRehype) // Convert Markdown to HTML + .use(rehypeStringify); // Convert HTML to string + + const result = processor.processSync(content); + return result.toString(); +} + export const dynamic = "force-static"; export async function GET() { const allPosts = getAllPosts(); @@ -30,7 +50,11 @@ export async function GET() { }, }); - allPosts.forEach(({ frontmatter, path }) => { + allPosts.forEach(({ frontmatter, path, params }) => { + // Get the content for the post and render the markdown to HTML + const rawContent = getPostFileContent(params); + const renderedContent = processPostContent(rawContent); + feed.addItem({ title: frontmatter.title, id: `${docsConfig.siteUrl}${path}`, @@ -44,7 +68,7 @@ export async function GET() { link: `${docsConfig.siteUrl}/blog/`, }, ], - // TODO: Include rendered Markdown as content. + content: renderedContent, }); }); const xml = feed.atom1();