From cbef290b5bcbbb15db20f3943f56bf4293e42e49 Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Sun, 19 May 2024 10:00:08 +0100 Subject: [PATCH] Better RSS: post content, better positioning, TypeScript --- website/src/pages/blog/rss.xml.ts | 34 +++++++++++++++++++++++++++++++ website/src/pages/rss.xml.js | 17 ---------------- 2 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 website/src/pages/blog/rss.xml.ts delete mode 100644 website/src/pages/rss.xml.js diff --git a/website/src/pages/blog/rss.xml.ts b/website/src/pages/blog/rss.xml.ts new file mode 100644 index 0000000..c9ef251 --- /dev/null +++ b/website/src/pages/blog/rss.xml.ts @@ -0,0 +1,34 @@ +import rss from '@astrojs/rss'; +import type { APIContext } from 'astro'; +import { getCollection } from 'astro:content'; +import MarkdownIt from 'markdown-it'; + +const mdParser = new MarkdownIt(); + +export async function GET(context: APIContext) { + // `site` is guaranteed to exist because we define it in our Astro config + const site = context.site!; + const posts = await getCollection('blog'); + + return rss({ + title: 'Joe Carstairs’ blog', + description: 'Short posts on random topics I find interesting', + customData: ` + /images/headshot.jpg + en-GB + `, + site, + trailingSlash: false, + items: posts.map((post) => ({ + link: post.slug, + title: post.data.title, + content: mdParser.render(post.body), + pubDate: new Date(`${post.data.pubDate.year}-${post.data.pubDate.month}-${post.data.pubDate.day}`), + description: post.data.description, + author: 'Joe Carstairs', + + // A page for displaying comments related to the item + /* commentsUrl: `${post.slug}/comments`, */ + })), + }) +} diff --git a/website/src/pages/rss.xml.js b/website/src/pages/rss.xml.js deleted file mode 100644 index 79da596..0000000 --- a/website/src/pages/rss.xml.js +++ /dev/null @@ -1,17 +0,0 @@ -import rss from '@astrojs/rss'; -import { getCollection } from 'astro:content'; -import { SITE_TITLE, SITE_DESCRIPTION } from '../consts'; - -export async function GET(context) { - const posts = await getCollection('blog'); - return rss({ - title: SITE_TITLE, - description: SITE_DESCRIPTION, - site: context.site, - items: posts.map((post) => ({ - ...post.data, - link: `/blog/${post.slug}/`, - pubDate: new Date(post.data.pubDate.year, post.data.pubDate.month - 1, post.data.pubDate.day), - })), - }); -}