|
| 1 | +import type { APIContext } from "astro"; |
| 2 | +import rss, { type RSSFeedItem } from "@astrojs/rss"; |
| 3 | +import { |
| 4 | + getCollection, |
| 5 | + getEntry, |
| 6 | + render, |
| 7 | + type CollectionEntry, |
| 8 | +} from "astro:content"; |
| 9 | +import { experimental_AstroContainer as AstroContainer } from "astro/container"; |
| 10 | +import mdxRenderer from "@astrojs/mdx/server.js"; |
| 11 | +import solidRenderer from "@astrojs/solid-js/server.js"; |
| 12 | + |
| 13 | +import { transform, walk } from "ultrahtml"; |
| 14 | +import sanitize from "ultrahtml/transformers/sanitize"; |
| 15 | + |
| 16 | +import { SITE_TITLE, SITE_DESCRIPTION } from "../../constants"; |
| 17 | + |
| 18 | +// need to make this endpoint dynamic with only one path |
| 19 | +// otherwise the astro file will take precedence |
| 20 | +export function getStaticPaths() { |
| 21 | + return [{ params: { slug: "index" } }]; |
| 22 | +} |
| 23 | + |
| 24 | +const container = await AstroContainer.create({}); |
| 25 | +container.addServerRenderer({ renderer: mdxRenderer, name: "mdx" }); |
| 26 | +container.addServerRenderer({ renderer: solidRenderer, name: "solid" }); |
| 27 | + |
| 28 | +const getAuthors = async (authors: string[]): Promise<string> => { |
| 29 | + const authorEntries = await Promise.all( |
| 30 | + authors.map(async (author) => await getEntry("authors", author)), |
| 31 | + ); |
| 32 | + |
| 33 | + const authorTitles = authorEntries |
| 34 | + .map((author) => author?.data?.title) |
| 35 | + .filter(Boolean); |
| 36 | + |
| 37 | + if (authorTitles.length > 1) { |
| 38 | + const lastAuthor = authorTitles.pop(); |
| 39 | + return `${authorTitles.join(", ")} and ${lastAuthor}`; |
| 40 | + } |
| 41 | + |
| 42 | + return authorTitles.join(", "); |
| 43 | +}; |
| 44 | + |
| 45 | +const getPost = async ( |
| 46 | + context: APIContext, |
| 47 | + post: CollectionEntry<"news">, |
| 48 | +): Promise<RSSFeedItem> => { |
| 49 | + const content = await transform( |
| 50 | + await container.renderToString((await render(post)).Content), |
| 51 | + [ |
| 52 | + async (node) => { |
| 53 | + await walk(node, async (node) => { |
| 54 | + if (node.name === "img" && node.attributes.src?.startsWith("/")) { |
| 55 | + node.attributes.src = context.site + node.attributes.src.slice(1); |
| 56 | + } |
| 57 | + }); |
| 58 | + return node; |
| 59 | + }, |
| 60 | + sanitize({ |
| 61 | + dropElements: ["script", "style"], |
| 62 | + }), |
| 63 | + ], |
| 64 | + ); |
| 65 | + |
| 66 | + return { |
| 67 | + title: post.data.title, |
| 68 | + pubDate: post.data.date, |
| 69 | + description: post.data.description, |
| 70 | + author: await getAuthors(post.data.authors), |
| 71 | + content, |
| 72 | + link: `/news/${post.id}/`, |
| 73 | + }; |
| 74 | +}; |
| 75 | + |
| 76 | +export async function GET(context: APIContext) { |
| 77 | + const news = await getCollection("news"); |
| 78 | + const items = news.sort((a, b) => +b.data.date - +a.data.date).slice(0, 15); |
| 79 | + |
| 80 | + return rss({ |
| 81 | + title: SITE_TITLE, |
| 82 | + description: SITE_DESCRIPTION, |
| 83 | + site: context.site || "", |
| 84 | + items: await Promise.all(items.map(async (post) => getPost(context, post))), |
| 85 | + customData: `<language>en-US</language><lastBuildDate>${new Date().toUTCString()}</lastBuildDate>`, |
| 86 | + }); |
| 87 | +} |
0 commit comments