blog.hangerthem.com/app/feed.xml/route.ts

56 lines
No EOL
1.5 KiB
TypeScript

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, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&apos;")
}
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 `
<entry>
<title>${escapeXml(p.title)}</title>
<link href="${url}" rel="alternate" type="text/html" />
<id>${url}</id>
<published>${date}</published>
<updated>${date}</updated>
<content type="html" src="${url}" />
<summary>${escapeXml(p.description)}</summary>
</entry>`
})
.join("")
const feed = `<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>${escapeXml(meta.title)}</title>
<id>${SITE_URL}/</id>
<updated>${updated}</updated>
<author>
<name>${escapeXml("d")}</name>
</author>
<link href="${SITE_URL}/feed" rel="self" type="application/atom+xml" />
<link href="${SITE_URL}" rel="alternate" type="text/html" />
${entries}
</feed>`
return new Response(feed, {
headers: { "Content-Type": "application/atom+xml; charset=utf-8" },
})
}