1- import rss from '@astrojs/rss' ;
1+ import rss , { pagesGlobToRssItems } from '@astrojs/rss' ;
22import { getCollection } from 'astro:content' ;
33import { SITE_TITLE , SITE_DESCRIPTION } from '../consts' ;
44
55export async function GET ( context ) {
6- const posts = await getCollection ( 'blog' ) ;
7- return rss ( {
8- title : SITE_TITLE ,
9- description : SITE_DESCRIPTION ,
10- site : context . site ,
11- items : posts . map ( ( post ) => ( {
12- ...post . data ,
13- link : `/blog/${ post . id } /` ,
14- } ) ) ,
15- } ) ;
16- }
6+ // Get the raw site URL and remove a trailing slash, if any.
7+ const rawSite =
8+ typeof context . site === 'string' ? context . site : context . site . href ;
9+ const trimmedSite = rawSite . replace ( / \/ $ / , '' ) ;
10+
11+ // Append your base from astro.config.mjs.
12+ const basePath = '/cookbook/' ;
13+ const fullSiteUrl = trimmedSite + basePath ; // e.g. "https://nicholasdbrady.github.io/cookbook/"
14+
15+ // Fetch your blog posts from your content collection.
16+ const posts = await getCollection ( 'blog' , ( { data } ) => ! data . draft ) ;
17+
18+ // Process each post to create an RSS item.
19+ const items = await Promise . all (
20+ posts . map ( async ( post ) => {
21+ const { Content } = await post . render ( ) ;
22+ // Use fullSiteUrl to resolve heroImage.
23+ const heroImageHTML = post . data . heroImage
24+ ? `<p><img src="${ new URL ( post . data . heroImage , fullSiteUrl ) . href } " alt="${ post . data . title } Hero Image" /></p>`
25+ : '' ;
26+ return {
27+ title : post . data . title ,
28+ // Use fullSiteUrl to construct the item link.
29+ link : new URL ( `blog/${ post . slug } /` , fullSiteUrl ) . href ,
30+ pubDate : post . data . pubDate ,
31+ description : post . data . description ,
32+ content : heroImageHTML + post . body ,
33+ categories : post . data . tags || [ ] ,
34+ author : post . data . author || undefined ,
35+ } ;
36+ } )
37+ ) ;
38+
39+ return rss ( {
40+ title : SITE_TITLE ,
41+ description : SITE_DESCRIPTION ,
42+ site : fullSiteUrl , // This ensures the channel <link> is "https://nicholasdbrady.github.io/cookbook/"
43+ items,
44+ trailingSlash : true , // To match your astro.config.mjs trailingSlash: "always"
45+ customData : `<language>en-us</language>` ,
46+ } ) ;
47+ }
0 commit comments