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

45 lines
1.2 KiB
TypeScript

import { getPosts } from '$lib/posts';
import siteMetadata from '$content/metadata.json';
import type { Post } from '$lib/types'
export const prerender = true;
export async function GET() {
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 })
}