1- import rss , { pagesGlobToRssItems } from '@astrojs/rss' ;
1+ import rss from '@astrojs/rss' ;
2+ import { getCollection } from 'astro:content' ;
23import { SITE_TITLE , SITE_DESCRIPTION } from '../consts' ;
34
45export async function GET ( context ) {
5- // Ensure context.site is a string and remove any trailing slash.
6- const rawSite = typeof context . site === 'string' ? context . site : context . site . href ;
7- const trimmedSite = rawSite . replace ( / \/ $ / , '' ) ;
8- const basePath = '/cookbook/' ;
9- // Construct the full site URL: "https://nicholasdbrady.github.io/cookbook/"
10- const fullSiteUrl = trimmedSite + basePath ;
11-
12- // Use a glob pattern array to include only blog pages and exclude the dynamic route file.
13- let items = await pagesGlobToRssItems (
14- import . meta. glob ( [
15- './blog/*.{astro,md,mdx}' ,
16- '!./blog/[...slug].astro'
17- ] )
18- ) ;
19-
20- // Post-process each item so that its link and guid are built correctly.
21- items = items . map ( item => {
22- // Remove a leading slash from the item.link if present.
23- const relativePath = item . link . startsWith ( '/' ) ? item . link . substring ( 1 ) : item . link ;
24- // Construct the fixed link using the fullSiteUrl as the base.
25- const fixedLink = new URL ( relativePath , fullSiteUrl ) . href ;
26- return {
27- ...item ,
28- link : fixedLink ,
29- guid : fixedLink ,
30- } ;
31- } ) ;
32-
33- return rss ( {
34- title : SITE_TITLE ,
35- description : SITE_DESCRIPTION ,
36- site : fullSiteUrl , // Ensures the channel <link> includes the "/cookbook/" base.
37- items,
38- trailingSlash : true , // Matches your astro.config.mjs trailingSlash: "always"
39- customData : `<language>en-us</language>` ,
40- } ) ;
41- }
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+ }
0 commit comments