Chrysoblog/src/routes/rss.xml/+server.ts

45 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-10-11 01:25:09 -07:00
import { getPosts } from '$lib/posts';
import siteMetadata from '$content/metadata.json';
import type { Post } from '$lib/types'
2024-10-11 01:28:20 -07:00
export const prerender = true;
export async function GET() {
2024-10-11 01:25:09 -07:00
const posts: Post[] = await getPosts();
const headers = { 'Content-Type': 'application/xml' }
const description = siteMetadata?.description
? `<description>${siteMetadata?.description || ""}</description>`
: "";
const url = siteMetadata?.url || "";
const urlXml = siteMetadata?.url ? `
<link>${url}</link>
<atom:link href="${url}/rss.xml" rel="self" type="application/rss+xml"/>
` : "";
const xml = `
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>${siteMetadata.blogName}</title>
${description}
${urlXml}
${posts
.map(
(post) => `
<item>
<title>${post.title}</title>
<description>${post.description}</description>
<link>${url}/post/${post.slug}</link>
<guid isPermaLink="true">${url}/post/${post.slug}</guid>
<pubDate>${new Date(post.date).toUTCString()}</pubDate>
</item>
`
)
.join('')}
</channel>
</rss>
`.trim()
return new Response(xml, { headers })
}