From 1a64bce2fbf4f8c430672b63c62c6b99c0ac496a Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Mon, 13 Apr 2026 08:37:56 +0100 Subject: website can get pubDate from filename instead of frontmatter --- website/src/components/BlogFeed.astro | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'website/src/components/BlogFeed.astro') 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}

    - +

  • )) } -- cgit v1.2.3