Better RSS: post content, better positioning, TypeScript

This commit is contained in:
Joe Carstairs
2024-05-19 10:00:08 +01:00
parent 55261df6fc
commit cbef290b5b
2 changed files with 34 additions and 17 deletions

View File

@@ -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: `
<image>/images/headshot.jpg</image>
<language>en-GB</language>
`,
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`, */
})),
})
}

View File

@@ -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),
})),
});
}