From 1a64bce2fbf4f8c430672b63c62c6b99c0ac496a Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Mon, 13 Apr 2026 08:37:56 +0100 Subject: [PATCH] website can get pubDate from filename instead of frontmatter --- website/src/components/BlogFeed.astro | 23 +++++++++++++++++------ website/src/content.config.ts | 2 +- website/src/layouts/BlogPost.astro | 7 ++++++- website/src/pages/blog/[...slug].astro | 7 ++++++- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/website/src/components/BlogFeed.astro b/website/src/components/BlogFeed.astro index ce011ba..663a826 100644 --- a/website/src/components/BlogFeed.astro +++ b/website/src/components/BlogFeed.astro @@ -19,20 +19,31 @@ const posts = maxEntries === undefined : allPosts.sort(sortByPubDateDescending).slice(0, maxEntries); const distinctYears: number[] = posts - .map(post => post.data.pubDate.getFullYear()) + .map(post => pubDate(post).getFullYear()) .reduce((acc, curr) => acc.includes(curr) ? acc : [...acc, curr], []) .sort((a, b) => b - a); function matchesYear(year: number) { - return (post: CollectionEntry<'blog'>) => post.data.pubDate.getFullYear() === year; + return (post: CollectionEntry<'blog'>) => pubDate(post).getFullYear() === year; } function sortByPubDateDescending(post1: CollectionEntry<'blog'>, post2: CollectionEntry<'blog'>) { - const date1 = post1.data.pubDate.getTime(); - const date2 = post2.data.pubDate.getTime(); + const date1 = pubDate(post1).getTime(); + const date2 = pubDate(post2).getTime(); return date2 - date1; } +function pubDate(post: CollectionEntry<'blog'>): Date { + if (post.data.pubDate) { + return post.data.pubDate; + } + const date = new Date(post.id); + if (isNaN(date.valueOf())) { + throw new Error(`Post ${post.id} does not have a valid publication date.`); + } + return date; +} + const HeadingElem = `h${headingLevel} class="p-name"`; const SubHeadingElem = `h${headingLevel + 1}`; const AuthorElem = `p${hideAuthor ? " hidden" : ""}`; @@ -61,7 +72,7 @@ const canonicalBlogUrl = new URL('blog', Astro.site)
  • {post.data.title}

    - +

  • )) } @@ -72,7 +83,7 @@ const canonicalBlogUrl = new URL('blog', Astro.site)
  • {post.data.title}

    - +

  • )) } diff --git a/website/src/content.config.ts b/website/src/content.config.ts index bf331ba..9514c9c 100644 --- a/website/src/content.config.ts +++ b/website/src/content.config.ts @@ -44,7 +44,7 @@ const blog = defineCollection({ title: z.string(), hidden: z.optional(z.boolean()), description: z.optional(z.string()), - pubDate: z.date(), + pubDate: z.optional(z.date()), updatedDate: z.optional(z.date()), }), }); diff --git a/website/src/layouts/BlogPost.astro b/website/src/layouts/BlogPost.astro index 3f7a999..267ea6c 100644 --- a/website/src/layouts/BlogPost.astro +++ b/website/src/layouts/BlogPost.astro @@ -4,7 +4,12 @@ import BaseHead from '../components/BaseHead.astro'; import FormattedDate from '../components/FormattedDate.astro'; import Navbar from '../components/Navbar.astro'; -type Props = CollectionEntry<'blog'>['data']; +type Props = { + title: string; + description?: string; + updatedDate?: Date; + pubDate: Date; +} const { title, description, pubDate, updatedDate } = Astro.props; diff --git a/website/src/pages/blog/[...slug].astro b/website/src/pages/blog/[...slug].astro index a4168b6..28d5ec0 100644 --- a/website/src/pages/blog/[...slug].astro +++ b/website/src/pages/blog/[...slug].astro @@ -53,6 +53,11 @@ const Content = post.filePath?.endsWith('.gmi') : (await render(post)).Content; --- - + { post.filePath?.endsWith('.gmi') ? : }