Skip to content

Commit 1b377fc

Browse files
committed
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 <[email protected]>
1 parent 73e11b8 commit 1b377fc

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"rehype-autolink-headings": "^7.1.0",
3838
"rehype-raw": "^7.0.0",
3939
"rehype-slug": "^6.0.0",
40+
"rehype-stringify": "^10.0.1",
4041
"remark-frontmatter": "^5.0.0",
4142
"remark-gfm": "^4.0.1",
4243
"semver": "^7.7.1",

src/app/blog/feed.xml/route.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
1-
import { getAllPosts } from "@/blog-helpers";
1+
import { getAllPosts, getPostFileContent } from "@/blog-helpers";
22
import { Feed } from "feed";
3+
4+
import {unified} from 'unified';
5+
import remarkParse from 'remark-parse';
6+
import remarkFrontmatter from "remark-frontmatter";
7+
import remarkGfm from 'remark-gfm';
8+
import remarkRehype from 'remark-rehype';
9+
import rehypeStringify from 'rehype-stringify';
10+
311
import docsConfig from "../../../../docs-config";
412

13+
function processPostContent(content: string): string {
14+
const processor = unified()
15+
.use(remarkParse)
16+
.use(remarkFrontmatter) // Parse YAML frontmatter
17+
.use(remarkGfm) // GitHub Flavored Markdown
18+
.use(remarkRehype) // Convert Markdown to HTML
19+
.use(rehypeStringify); // Convert HTML to string
20+
21+
const result = processor.processSync(content);
22+
return result.toString();
23+
}
24+
525
export const dynamic = "force-static";
626
export async function GET() {
727
const allPosts = getAllPosts();
@@ -30,7 +50,11 @@ export async function GET() {
3050
},
3151
});
3252

33-
allPosts.forEach(({ frontmatter, path }) => {
53+
allPosts.forEach(({ frontmatter, path, params }) => {
54+
// Get the content for the post and render the markdown to HTML
55+
const rawContent = getPostFileContent(params);
56+
const renderedContent = processPostContent(rawContent);
57+
3458
feed.addItem({
3559
title: frontmatter.title,
3660
id: `${docsConfig.siteUrl}${path}`,
@@ -44,7 +68,7 @@ export async function GET() {
4468
link: `${docsConfig.siteUrl}/blog/`,
4569
},
4670
],
47-
// TODO: Include rendered Markdown as content.
71+
content: renderedContent,
4872
});
4973
});
5074
const xml = feed.atom1();

0 commit comments

Comments
 (0)