From 2ff196ff76ceb1c3988023bb89e0b9ad25c7998e Mon Sep 17 00:00:00 2001 From: Ivy Turner Date: Wed, 30 Apr 2025 11:32:28 +0100 Subject: [PATCH] fixes --- src/components/util/Date.astro | 16 ++++---------- src/lib/date.ts | 26 ++++++++++++++++++++++ src/pages/notes/feed.xml.js | 40 ++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 src/lib/date.ts create mode 100644 src/pages/notes/feed.xml.js diff --git a/src/components/util/Date.astro b/src/components/util/Date.astro index d231793..e207be9 100644 --- a/src/components/util/Date.astro +++ b/src/components/util/Date.astro @@ -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(); --- - + diff --git a/src/lib/date.ts b/src/lib/date.ts new file mode 100644 index 0000000..e75f0f8 --- /dev/null +++ b/src/lib/date.ts @@ -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(); +} \ No newline at end of file diff --git a/src/pages/notes/feed.xml.js b/src/pages/notes/feed.xml.js new file mode 100644 index 0000000..26210d1 --- /dev/null +++ b/src/pages/notes/feed.xml.js @@ -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({ + // `` 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>`, + }); +} \ No newline at end of file