8000 fix: update @astrojs/rss and fast-xml-parser dependencies; enhance RS… · nicholasdbrady/cookbook@41c3ed0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 41c3ed0

Browse files
fix: update @astrojs/rss and fast-xml-parser dependencies; enhance RSS feed item processing
1 parent 337929f commit 41c3ed0

File tree

3 files changed

+59
-44
lines changed

3 files changed

+59
-44
lines changed

package-lock.json

Lines changed: 19 additions & 16 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"@astrojs/check": "^0.5.10",
1414
"@astrojs/markdoc": "^0.11.0",
1515
"@astrojs/mdx": "^2.3.1",
16-
"@astrojs/rss": "^4.0.5",
16+
"@astrojs/rss": "^4.0.11",
1717
"@astrojs/sitemap": "^3.1.4",
1818
"astro": "^4.6.4",
1919
"typescript": "^5.4.5"

src/pages/rss.xml.js

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,45 @@ import { getCollection } from 'astro:content';
33
import { SITE_TITLE, SITE_DESCRIPTION } from '../consts';
44

55
export async function GET(context) {
6-
// Retrieve all published blog posts from the "blog" collection
7-
const posts = await getCollection('blog', ({ data }) => !data.draft);
6+
// Fetch all blog posts; filter out drafts (assuming you use a `draft` flag in frontmatter)
7+
const posts = await getCollection('blog', ({ data }) => !data.draft);
88

9-
// Map each post to an RSS feed item.
10-
// Here we include the full content from the post body.
11-
// (If you need to render Markdown to HTML, you may use post.render() with appropriate rendering logic.)
12-
const items = posts.map((post) => ({
13-
title: post.data.title,
14-
// Link to the blog post page; adjust if your routing differs.
15-
link: `/blog/${post.slug}/`,
16-
// Publication date from frontmatter
17-
pubDate: post.data.pubDate,
18-
// A short summary/description for the feed item
19-
description: post.data.description,
20-
// Full post content – note that post.body is the raw Markdown.
21-
// For proper HTML, ensure your collection loader converts it, or use post.render() to generate HTML.
22-
content: post.body,
23-
// Optional: add categories if available
24-
categories: post.data.tags || [],
25-
// Optionally, include the author field if present
26-
author: post.data.author || undefined,
27-
}));
9+
// Get the absolute site URL from the context (configured in astro.config.mjs)
10+
const siteUrl = context.site;
2811

29-
return rss({
30-
title: SITE_TITLE,
31-
description: SITE_DESCRIPTION,
32-
site: context.site,
33-
items,
34-
});
12+
// Process each post to create an RSS feed item.
13+
const items = await Promise.all(posts.map(async (post) => {
14+
// Render the post (if necessary) to get the HTML.
15+
// If your loader already produces HTML in post.body, you can skip this.
16+
const { Content } = await post.render();
17+
18+
// If your frontmatter includes a heroImage (a relative path), convert it to an absolute URL
19+
const heroImageHTML = post.data.heroImage
20+
? `<p><img src="${new URL(post.data.heroImage, siteUrl).href}" alt="${post.data.title} Hero Image" /></p>`
21+
: '';
22+
23+
return {
24+
title: post.data.title,
25+
// Construct the link using your collection's URL structure
26+
link: `/blog/${post.slug}/`,
27+
pubDate: post.data.pubDate,
28+
// Use a short summary as description; if you want to include the full content, set it in content
29+
description: post.data.description,
30+
// Prepend the hero image (if available) to the post body
31+
content: heroImageHTML + post.body,
32+
// Include categories if available (from your frontmatter, e.g., tags)
33+
categories: post.data.tags || [],
34+
// (Optional) Add an author field if you want
35+
author: post.data.author || undefined,
36+
};
37+
}));
38+
39+
return rss({
40+
title: SITE_TITLE, // Your feed title
41+
description: SITE_DESCRIPTION, // A short description of your feed
42+
site: siteUrl, // The absolute base URL of your site
43+
items, // The array of RSS feed items you just created
44+
trailingSlash: false,
45+
});
3546
}
47+

0 commit comments

Comments
 (0)
0