-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy path+server.ts
More file actions
57 lines (51 loc) · 1.47 KB
/
+server.ts
File metadata and controls
57 lines (51 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as config from '$lib/config'
import type { PostsApiResponse } from '$api/posts/+server.js'
import type { RequestHandler } from './$types'
export const prerender = true
export const GET: RequestHandler = async ({ fetch }) => {
const response = await fetch('api/posts')
const posts = (await response.json()) as PostsApiResponse
const headers = { 'Content-Type': 'application/xml' }
const xml = `
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>${config.title}</title>
<description>${config.description}</description>
<link>${config.url}</link>
<atom:link href="${config.url}/rss.xml" rel="self" type="application/rss+xml"/>
${posts
.map(
(post) => `
<item>
<title>${escapeXml(post.title ?? 'Untitled')}</title>
<description>${post.description ? escapeXml(post.description) : ''}</description>
<link>${config.url}/${post.slug}</link>
<guid isPermaLink="true">${config.url}/${post.slug}</guid>
${post.date ? `<pubDate>${new Date(post.date).toUTCString()}</pubDate>` : ''}
</item>
`
)
.join('')}
</channel>
</rss>
`.trim()
return new Response(xml, { headers })
}
function escapeXml(unsafe: string) {
return unsafe.replace(/[<>&'"]/g, (c) => {
switch (c) {
case '<':
return '<'
case '>':
return '>'
case '&':
return '&'
case "'":
return '''
case '"':
return '"'
default:
return c
}
})
}