import meta from "@/const/meta"
import { getAllPostsMeta } from "@/lib/posts"
const SITE_URL = process.env.SITE_URL || "http://localhost:3000"
function escapeXml(str: string) {
return str
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """)
.replace(/'/g, "'")
}
export async function GET() {
const posts = getAllPostsMeta()
const updated =
posts.length > 0
? new Date(posts[0].date).toISOString()
: new Date().toISOString()
const entries = posts
.map((p) => {
const url = `${SITE_URL}/posts/${p.slug}`
const date = new Date(p.date).toISOString()
return `
${escapeXml(p.title)}
${url}
${date}
${date}
${escapeXml(p.description)}
`
})
.join("")
const feed = `
${escapeXml(meta.title)}
${SITE_URL}/
${updated}
${escapeXml("d")}
${entries}
`
return new Response(feed, {
headers: { "Content-Type": "application/atom+xml; charset=utf-8" },
})
}