/** * The RSS 2.0 feed: blog posts and case studies, newest first, with absolute * links from the configured site address or the request's origin. */ import type { APIRoute } from "astro"; import { xml } from "../lib/blog.ts"; import { site } from "../lib/page.ts"; export const GET: APIRoute = async ({ site: configured, url }) => { const s = await site(); const origin = (configured ?? new URL(url.origin)).href.replace(/\/$/, ""); const abs = (path: string) => new URL(path, `${origin}/`).href; const settings = s.settings(); const name = settings?.name ?? "Greg Kilmer"; const posts = s.posts({ includeCaseStudies: true }).slice(0, 50); const rfc822 = (iso: string) => new Date(`${iso}T12:00:00Z`).toUTCString(); const items = posts .map((p) => { const href = s.href(p); if (!href) return ""; const link = abs(href); const category = p.data.postType === "case study" ? "Case study" : p.data.topic; return [ " ", ` ${xml(p.title)}`, ` ${xml(link)}`, ` ${xml(link)}`, ` ${rfc822(p.data.date)}`, ` ${xml(category)}`, p.data.summary ? ` ${xml(p.data.summary)}` : "", " ", ] .filter(Boolean) .join("\n"); }) .filter(Boolean) .join("\n"); const body = ` ${xml(name)} ${xml(abs("/blog"))} ${xml(settings?.tagline ?? "What I am making, learning, playing and building.")} en-us ${posts[0] ? ` ${rfc822(posts[0].data.date)}\n` : ""}${items} `; return new Response(body, { headers: { "content-type": "application/rss+xml; charset=utf-8", "cache-control": "public, max-age=600" } }); };