This commit is contained in:
Ivy Turner 2025-04-30 11:32:28 +01:00
parent 7e1796b82d
commit 2ff196ff76
No known key found for this signature in database
3 changed files with 70 additions and 12 deletions

View file

@ -1,8 +1,10 @@
---
import { formattedDate, isoDate } from '~/lib/date';
export interface Props {
date: string | Date;
date: Date;
showTime?: boolean;
}
const { date, showTime = false } = Astro.props;
if (!date) {
@ -10,16 +12,6 @@ if (!date) {
return null;
}
const dateObj = typeof date === "string" ? new globalThis.Date(date) : date;
const options: Intl.DateTimeFormatOptions = { year: "numeric", month: "long", day: "numeric" };
if (showTime) {
options.hour = "2-digit";
options.minute = "2-digit";
}
const formattedDate = dateObj.toLocaleString("en-GB", options);
const isoDate = dateObj.toISOString();
---
<time datetime={isoDate}>{formattedDate}</time>
<time datetime={isoDate(date)}>{formattedDate(date, showTime)}</time>

26
src/lib/date.ts Normal file
View file

@ -0,0 +1,26 @@
function checkDate(date: Date | string): Date {
if (typeof date === Date)
}
export function formattedDate(date: Date, showTime: boolean): string {
const options: Intl.DateTimeFormatOptions = {
year: "numeric",
month: "long",
day: "numeric",
};
if (showTime) {
options.hour = "2-digit";
options.minute = "2-digit";
}
return date.toLocaleString("en-GB", options);
}
export function isoDate(date: Date): string {
if (isNaN(date.getTime())) {
throw new Error("Invalid Date");
}
return date.toISOString();
}

View file

@ -0,0 +1,40 @@
import rss from '@astrojs/rss';
import conf from "~/site.config";
import { getCollection } from 'astro:content';
import sanitizeHtml from 'sanitize-html';
import MarkdownIt from 'markdown-it';
import { formattedDate } from '~/lib/date';
const parser = new MarkdownIt();
export async function GET(context) {
const blog = await getCollection('notes');
return rss({
// `<title>` field in output xml
title: conf.site.title,
// `<description>` field in output xml
description: conf.site.description,
// Pull in your project "site" from the endpoint context
// https://docs.astro.build/en/reference/api-reference/#site
site: context.site,
stylesheet: "/feed.xsl",
// Array of `<item>`s in output xml
// See "Generating items" section for examples using content collections and glob imports
items: blog.map((post) => ({
title: post.data.title || `A note from ${formattedDate(post.data.date)}`,
pubDate: post.data.date,
description: post.data.description,
// Compute RSS link from post `id`
// This example assumes all posts are rendered as `/blog/[id]` routes
link: `/notes/${post.id}/`,
trailingSlash: false,
content: sanitizeHtml(parser.render(post.body), {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img'])
}),
...post.data,
})),
// (optional) inject custom xml
customData: `<language>en-gb</language>`,
});
}