website can get pubDate from filename instead of frontmatter

This commit is contained in:
2026-04-13 08:37:56 +01:00
parent cf6dbc2aa1
commit 1a64bce2fb
4 changed files with 30 additions and 9 deletions
+17 -6
View File
@@ -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<number[]>((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)
<li class="h-entry">
<a class="u-url p-name" href={`/blog/${post.id}`}>{post.data.title}</a>
<p class="p-summary" set:html={post.data.description} />
<FormattedDate className="dt-published" date={post.data.pubDate} />
<FormattedDate className="dt-published" date={pubDate(post)} />
</li>
)) }
</ul>
@@ -72,7 +83,7 @@ const canonicalBlogUrl = new URL('blog', Astro.site)
<li class="h-entry">
<a class="u-url p-name" href={`/blog/${post.id}`}>{post.data.title}</a>
<p class="p-summary" set:html={post.data.description} />
<FormattedDate className="dt-published" date={post.data.pubDate} />
<FormattedDate className="dt-published" date={pubDate(post)} />
</li>
)) }
</ul>
+1 -1
View File
@@ -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()),
}),
});
+6 -1
View File
@@ -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;
+6 -1
View File
@@ -53,6 +53,11 @@ const Content = post.filePath?.endsWith('.gmi')
: (await render(post)).Content;
---
<BlogPost {...post.data}>
<BlogPost
title={post.data.title}
description={post.data.description}
pubDate={post.data.pubDate ?? new Date(post.id)}
updatedDate={post.data.updatedDate}
>
{ post.filePath?.endsWith('.gmi') ? <Fragment set:html={Content}></Fragment> : <Content />}
</BlogPost>