@@ -5,6 +5,14 @@ import rehypeRaw from "rehype-raw";
5
5
import rehypeStringify from "rehype-stringify" ;
6
6
import { getCollection , type CollectionEntry } from "astro:content" ;
7
7
8
+ interface Post {
9
+ title : string ;
10
+ url : string ;
11
+ date : string ;
12
+ description : string ;
13
+ content : string ;
14
+ }
15
+
8
16
const FEED_CONFIG = {
9
17
title : "Prism Launcher" ,
10
18
subtitle :
@@ -24,7 +32,10 @@ const escapeXml = (text: string): string =>
24
32
. replace ( / > / g, ">" )
25
33
. replace ( / " / g, """ ) ;
26
34
27
- async function processPost ( post : CollectionEntry < "news" > , siteUrl : string ) {
35
+ async function processPost (
36
+ post : CollectionEntry < "news" > ,
37
+ siteUrl : string ,
38
+ ) : Promise < Post > {
28
39
const slug = post . data . slug || post . slug ;
29
40
30
41
try {
@@ -37,6 +48,7 @@ async function processPost(post: CollectionEntry<"news">, siteUrl: string) {
37
48
title : post . data . title ,
38
49
url : `${ siteUrl } news/${ slug } /` ,
39
50
date : post . data . date . toISOString ( ) ,
51
+ description : post . data . description ,
40
52
content,
41
53
} ;
42
54
} catch ( error ) {
@@ -45,15 +57,17 @@ async function processPost(post: CollectionEntry<"news">, siteUrl: string) {
45
57
title : post . data . title ,
46
58
url : `${ siteUrl } news/${ slug } /` ,
47
59
date : post . data . date . toISOString ( ) ,
60
+ description : post . data . description || "No description available" ,
48
61
content : `<p>${ escapeXml ( post . data . description || "No description available" ) } </p>` ,
49
62
} ;
50
63
}
51
64
}
52
65
53
66
function generateFeed (
54
- entries : Array < { title : string ; url : string ; date : string ; content : string } > ,
67
+ entries : Post [ ] ,
55
68
siteUrl : string ,
56
69
updated : string ,
70
+ short : boolean ,
57
71
) : string {
58
72
return `<?xml version="1.0" encoding="utf-8"?>
59
73
<feed xmlns="http://www.w3.org/2005/Atom">
@@ -68,20 +82,28 @@ function generateFeed(
68
82
<email>${ FEED_CONFIG . email } </email>
69
83
</author>
70
84
${ entries
71
- . map (
72
- ( entry ) => ` <entry>
85
+ . map ( ( entry ) => {
86
+ let content = short
87
+ ? `<content type="string">${ escapeXml ( entry . description ) } </content>`
88
+ : `<content type="html">${ escapeXml ( entry . content ) } </content>` ;
89
+ return ` <entry>
73
90
<title>${ escapeXml ( entry . title ) } </title>
74
91
<link href="${ entry . url } "/>
75
92
<updated>${ entry . date } </updated>
76
93
<id>${ entry . url } </id>
77
- <content type="html"> ${ escapeXml ( entry . content ) } </content>
78
- </entry>` ,
79
- )
94
+ ${ content }
95
+ </entry>` ;
96
+ } )
80
97
. join ( "\n" ) }
81
98
</feed>` ;
82
99
}
83
100
84
- export const GET : APIRoute = async ( { site } ) => {
101
+ export const GET : APIRoute = async ( { site, params : { feedName } } ) => {
102
+ if ( feedName !== "feed" && feedName !== "short" ) {
103
+ return new Response ( null , {
104
+ status : 404 ,
105
+ } ) ;
106
+ }
85
107
try {
86
108
const posts = ( await getCollection ( "news" , ( { data } ) => ! data . draft ) ) . sort (
87
109
( a , b ) => b . data . date . getTime ( ) - a . data . date . getTime ( ) ,
@@ -95,6 +117,7 @@ export const GET: APIRoute = async ({ site }) => {
95
117
await Promise . all ( posts . map ( ( post ) => processPost ( post , siteUrl ) ) ) ,
96
118
siteUrl ,
97
119
( posts [ 0 ] ?. data . date || new Date ( ) ) . toISOString ( ) ,
120
+ feedName === "short" ,
98
121
) ,
99
122
{
100
123
headers : {
@@ -116,3 +139,7 @@ export const GET: APIRoute = async ({ site }) => {
116
139
) ;
117
140
}
118
141
} ;
142
+
143
+ export const getStaticPaths = ( ) => {
144
+ return [ { params : { feedName : "feed" } } , { params : { feedName : "short" } } ] ;
145
+ } ;
0 commit comments