22import RSS from 'rss' ;
33import { getPageMap } from '@theguild/components/server' ;
44import { AuthorId , authors } from '../../../authors' ;
5- import { isBlogPost } from '../blog-types' ;
5+ import { pagesDepthFirst } from '../../../mdx-types' ;
6+ import { coerceCaseStudyToBlog } from '../../case-studies/coerce-case-studies-to-blogs' ;
7+ import { isCaseStudy } from '../../case-studies/isCaseStudyFile' ;
8+ import { BlogFrontmatter , BlogPostFile , isBlogPost } from '../blog-types' ;
69
7- function getAuthor ( name : string ) {
8- const author = authors [ name as AuthorId ] ?. name ;
9- return author ?? name ;
10+ function getAuthor ( frontmatterAuthors : BlogFrontmatter [ 'authors' ] ) : string {
11+ const first = Array . isArray ( frontmatterAuthors ) ? frontmatterAuthors [ 0 ] : frontmatterAuthors ;
12+
13+ if ( typeof first === 'string' ) {
14+ const author = authors [ first as AuthorId ] ;
15+ return author ? author . name : 'Unknown Author' ;
16+ }
17+
18+ return first . name ;
1019}
1120
21+ export const dynamic = 'force-static' ;
22+ export const config = { runtime : 'edge' } ;
23+
1224export async function GET ( ) {
13- const [ _meta , _indexPage , ...pageMap ] = await getPageMap ( '/blog' ) ;
14- const allPosts = pageMap
15- . filter ( isBlogPost )
16- . map (
17- item =>
18- ( {
19- title : item . frontMatter . title ,
20- date : new Date ( item . frontMatter . date ) ,
21- url : `https://the-guild.dev/graphql/hive${ item . route } ` ,
22- description : ( item . frontMatter as any ) . description ?? '' ,
23- author : getAuthor (
24- typeof item . frontMatter . authors === 'string'
25- ? item . frontMatter . authors
26- : item . frontMatter . authors . at ( 0 ) ! ,
27- ) ,
28- categories : Array . isArray ( item . frontMatter . tags )
29- ? item . frontMatter . tags
30- : [ item . frontMatter . tags ] ,
31- } ) satisfies RSS . ItemOptions ,
32- )
33- . sort ( ( a , b ) => b . date . getTime ( ) - a . date . getTime ( ) ) ;
25+ let allPosts : RSS . ItemOptions [ ] = [ ] ;
26+
27+ const [ _meta , _indexPage , ...pages ] = await getPageMap ( '/' ) ;
28+ for ( const page of pagesDepthFirst ( pages ) ) {
29+ const route = ( page && 'route' in page && page . route ) || '' ;
30+ const [ dir , name ] = route . split ( '/' ) . filter ( Boolean ) ;
31+ if ( ! name ) continue ;
32+ switch ( dir ) {
33+ case 'blog' :
34+ case 'product-updates' :
35+ if ( isBlogPost ( page ) ) allPosts . push ( toRssItem ( page ) ) ;
36+ break ;
37+ case 'case-studies' :
38+ if ( isCaseStudy ( page ) ) allPosts . push ( toRssItem ( coerceCaseStudyToBlog ( page ) ) ) ;
39+ break ;
40+ }
41+ }
42+
43+ if ( allPosts . length === 0 ) {
44+ throw new Error ( 'No blog posts found for RSS feed' ) ;
45+ }
46+
47+ allPosts = allPosts . sort ( ( a , b ) => new Date ( b . date ) . getTime ( ) - new Date ( a . date ) . getTime ( ) ) ;
3448
3549 const feed = new RSS ( {
3650 title : 'Hive Blog' ,
@@ -49,5 +63,15 @@ export async function GET() {
4963 } ) ;
5064}
5165
52- export const dynamic = 'force-static' ;
53- export const config = { runtime : 'edge' } ;
66+ function toRssItem ( blogPost : BlogPostFile ) : RSS . ItemOptions {
67+ return {
68+ title : blogPost . frontMatter . title ,
69+ date : new Date ( blogPost . frontMatter . date ) ,
70+ url : `https://the-guild.dev/graphql/hive${ blogPost . route } ` ,
71+ description : blogPost . frontMatter . description ?? '' ,
72+ author : getAuthor ( blogPost . frontMatter . authors ) ,
73+ categories : Array . isArray ( blogPost . frontMatter . tags )
74+ ? blogPost . frontMatter . tags
75+ : [ blogPost . frontMatter . tags ] ,
76+ } ;
77+ }
0 commit comments