import type { APIRoute } from "astro"; import { getPostsMetadata } from "@/lib/posts"; import { SITE_NAME, SITE_URL } from "@/lib/seo"; /** * The blog had no feed, so the only way to follow it was the mailing list. * Hand-rolled rather than pulled from a package: the whole document is six * elements and readers are strict about exactly those six. */ const esc = (text: string) => text.replace( /[<>&'"]/g, (character) => ({ "<": "<", ">": ">", "&": "&", "'": "'", '"': """ })[character] as string, ); const MIME: Record = { png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", webp: "image/webp", avif: "image/avif", }; export const GET: APIRoute = async ({ site }) => { const base = (site?.toString() ?? `${SITE_URL}/`).replace(/\/$/, ""); const posts = await getPostsMetadata(); const items = posts .map((post) => { const link = `${base}/posts/${post.filename}/`; // media:content rather than enclosure: the spec wants a byte length on an // enclosure and there is none to hand at build time const type = MIME[post.preview.split(".").pop()?.toLowerCase() ?? ""]; const media = post.preview && type ? [` `] : []; return [ " ", ` ${esc(post.title)}`, ` ${esc(link)}`, ` ${esc(link)}`, ` ${esc(post.summary)}`, ` ${new Date(post.publishedTime).toUTCString()}`, ...post.tags.map((tag) => ` ${esc(tag)}`), ...media, " ", ].join("\n"); }) .join("\n"); const newest = posts[0]?.modifiedTime ?? new Date().toISOString(); const xml = ` ${esc(`${SITE_NAME} - posts`)} ${esc(`${base}/posts/`)} ${esc(`Essays by ${SITE_NAME}`)} en-us ${new Date(newest).toUTCString()} ${items} `; return new Response(xml, { headers: { "Content-Type": "application/rss+xml; charset=utf-8", "Cache-Control": "public, max-age=3600", }, }); };