@@ -3,33 +3,45 @@ import { getCollection } from 'astro:content';
33import { SITE_TITLE , SITE_DESCRIPTION } from '../consts' ;
44
55export 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