import type { APIRoute } from "astro"; import { getCollection } from "astro:content"; import { siteConfig } from "../config"; import { getPostSlug } from "../utils/markdown"; // Helper function to normalize siteUrl - ensure it ends with a single slash function normalizeSiteUrl(url: string): string { return url.replace(/\/+$/, '') + '/'; } export const GET: APIRoute = async () => { const siteUrl = normalizeSiteUrl(import.meta.env.SITE || siteConfig.site); const posts = await getCollection("posts", ({ data }) => { return !data.draft; }); // Sort posts by date (newest first) const sortedPosts = posts.sort( (a, b) => new Date(b.data.date).getTime() - new Date(a.data.date).getTime() ); // Generate Atom feed XML const atomFeed = ` ${siteConfig.title} ${siteConfig.description} ${siteUrl} ${siteConfig.author} ${new Date().toISOString()} ${sortedPosts .map( (post) => { const slug = getPostSlug(post); return ` ${post.data.title} ${siteUrl}posts/${slug}/ ${new Date(post.data.date).toISOString()} ${new Date(post.data.date).toISOString()} ${post.data.description || ""} ${ post.data.tags ? post.data.tags.map((tag) => ``).join("") : "" } `; } ) .join("")} `; return new Response(atomFeed, { headers: { "Content-Type": "application/xml", "Cache-Control": "public, max-age=3600", // Cache for 1 hour }, }); };