website can get pubDate from filename instead of frontmatter
This commit is contained in:
@@ -19,20 +19,31 @@ const posts = maxEntries === undefined
|
|||||||
: allPosts.sort(sortByPubDateDescending).slice(0, maxEntries);
|
: allPosts.sort(sortByPubDateDescending).slice(0, maxEntries);
|
||||||
|
|
||||||
const distinctYears: number[] = posts
|
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], [])
|
.reduce<number[]>((acc, curr) => acc.includes(curr) ? acc : [...acc, curr], [])
|
||||||
.sort((a, b) => b - a);
|
.sort((a, b) => b - a);
|
||||||
|
|
||||||
function matchesYear(year: number) {
|
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'>) {
|
function sortByPubDateDescending(post1: CollectionEntry<'blog'>, post2: CollectionEntry<'blog'>) {
|
||||||
const date1 = post1.data.pubDate.getTime();
|
const date1 = pubDate(post1).getTime();
|
||||||
const date2 = post2.data.pubDate.getTime();
|
const date2 = pubDate(post2).getTime();
|
||||||
return date2 - date1;
|
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 HeadingElem = `h${headingLevel} class="p-name"`;
|
||||||
const SubHeadingElem = `h${headingLevel + 1}`;
|
const SubHeadingElem = `h${headingLevel + 1}`;
|
||||||
const AuthorElem = `p${hideAuthor ? " hidden" : ""}`;
|
const AuthorElem = `p${hideAuthor ? " hidden" : ""}`;
|
||||||
@@ -61,7 +72,7 @@ const canonicalBlogUrl = new URL('blog', Astro.site)
|
|||||||
<li class="h-entry">
|
<li class="h-entry">
|
||||||
<a class="u-url p-name" href={`/blog/${post.id}`}>{post.data.title}</a>
|
<a class="u-url p-name" href={`/blog/${post.id}`}>{post.data.title}</a>
|
||||||
<p class="p-summary" set:html={post.data.description} />
|
<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>
|
</li>
|
||||||
)) }
|
)) }
|
||||||
</ul>
|
</ul>
|
||||||
@@ -72,7 +83,7 @@ const canonicalBlogUrl = new URL('blog', Astro.site)
|
|||||||
<li class="h-entry">
|
<li class="h-entry">
|
||||||
<a class="u-url p-name" href={`/blog/${post.id}`}>{post.data.title}</a>
|
<a class="u-url p-name" href={`/blog/${post.id}`}>{post.data.title}</a>
|
||||||
<p class="p-summary" set:html={post.data.description} />
|
<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>
|
</li>
|
||||||
)) }
|
)) }
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ const blog = defineCollection({
|
|||||||
title: z.string(),
|
title: z.string(),
|
||||||
hidden: z.optional(z.boolean()),
|
hidden: z.optional(z.boolean()),
|
||||||
description: z.optional(z.string()),
|
description: z.optional(z.string()),
|
||||||
pubDate: z.date(),
|
pubDate: z.optional(z.date()),
|
||||||
updatedDate: z.optional(z.date()),
|
updatedDate: z.optional(z.date()),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,7 +4,12 @@ import BaseHead from '../components/BaseHead.astro';
|
|||||||
import FormattedDate from '../components/FormattedDate.astro';
|
import FormattedDate from '../components/FormattedDate.astro';
|
||||||
import Navbar from '../components/Navbar.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;
|
const { title, description, pubDate, updatedDate } = Astro.props;
|
||||||
|
|
||||||
|
|||||||
@@ -53,6 +53,11 @@ const Content = post.filePath?.endsWith('.gmi')
|
|||||||
: (await render(post)).Content;
|
: (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 />}
|
{ post.filePath?.endsWith('.gmi') ? <Fragment set:html={Content}></Fragment> : <Content />}
|
||||||
</BlogPost>
|
</BlogPost>
|
||||||
|
|||||||
Reference in New Issue
Block a user