Blog posts use Date type pubDate

This commit is contained in:
Joe Carstairs
2024-07-15 16:14:08 +01:00
parent dff4a997f8
commit ea96f40f7c
13 changed files with 20 additions and 69 deletions

View File

@@ -12,29 +12,18 @@ const { headingLevel = 2, hideAuthor = false } = Astro.props;
const posts = (await getCollection('blog')).filter((post) => !post.data.hidden); const posts = (await getCollection('blog')).filter((post) => !post.data.hidden);
const distinctYears: number[] = posts const distinctYears: number[] = posts
.map(post => post.data.pubDate.year) .map(post => post.data.pubDate.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.year === year; return (post: CollectionEntry<'blog'>) => post.data.pubDate.getFullYear() === year;
} }
function sortByPubDateDescending(post1: CollectionEntry<'blog'>, post2: CollectionEntry<'blog'>) { function sortByPubDateDescending(post1: CollectionEntry<'blog'>, post2: CollectionEntry<'blog'>) {
const year1 = post1.data.pubDate.year; const date1 = post1.data.pubDate.getMilliseconds();
const year2 = post2.data.pubDate.year; const date2 = post2.data.pubDate.getMilliseconds();
const month1 = post1.data.pubDate.month; return date2 - date1;
const month2 = post2.data.pubDate.month;
const day1 = post1.data.pubDate.day;
const day2 = post2.data.pubDate.day;
if (year1 !== year2) {
return year2 - year1;
} else if (month1 !== month2) {
return month2 - month1;
} else {
return day2 - day1;
}
} }
const headingElem = `h${headingLevel}`; const headingElem = `h${headingLevel}`;

View File

@@ -3,10 +3,7 @@ title: Hararis Sapiens on Religion
description: >- description: >-
In which I discuss why I think Hararis characterisation of religion In which I discuss why I think Hararis characterisation of religion
is inadequate because its too materialistic. is inadequate because its too materialistic.
pubDate: pubDate: 2024-01-14
year: 2024
month: 01
day: 14
--- ---
Ive been slowly re-reading Yuval Noah Hararis 2014 classic, Ive been slowly re-reading Yuval Noah Hararis 2014 classic,

View File

@@ -6,10 +6,7 @@ description: >-
Ullapool last weekend, I had the last privilege of giving the Toast to Ullapool last weekend, I had the last privilege of giving the Toast to
the Lassies. Particularly for the benefit of those who werent there, the Lassies. Particularly for the benefit of those who werent there,
here it is in full! here it is in full!
pubDate: pubDate: 2024-01-29
year: 2024
month: 01
day: 29
--- ---
Had Burns, instead of his sweet bonnie Jean,<br> Had Burns, instead of his sweet bonnie Jean,<br>

View File

@@ -4,10 +4,7 @@ description: >-
Based on a talk given to my colleagues at Based on a talk given to my colleagues at
<a href="https://www.scottlogic.co.uk">Scott Logic</a> for Maundy <a href="https://www.scottlogic.co.uk">Scott Logic</a> for Maundy
Thursday, 2024. Thursday, 2024.
pubDate: pubDate: 2024-03-30
year: 2024
month: 03
day: 30
--- ---
As you might have noticed, it is Easter this week! So I'd like to take five or As you might have noticed, it is Easter this week! So I'd like to take five or

View File

@@ -1,10 +1,7 @@
--- ---
title: Who consecrates the tabernacle? (Ex 29) title: Who consecrates the tabernacle? (Ex 29)
description: A quick, cursory and possibly completely rubbish observation I've made on Exodus 29. description: A quick, cursory and possibly completely rubbish observation I've made on Exodus 29.
pubDate: pubDate: 2024-04-11
year: 2024
month: 04
day: 11
--- ---
I've been reading Exodus recently, and the ending of Chapter 29 stuck out to me. I've been reading Exodus recently, and the ending of Chapter 29 stuck out to me.

View File

@@ -3,10 +3,7 @@ title: God Is Not Great, initial thoughts
description: >- description: >-
My spark notes on Hitchen's classic 2007 polemic against religion, plus some My spark notes on Hitchen's classic 2007 polemic against religion, plus some
initial thoughts on how I want to respond to it. initial thoughts on how I want to respond to it.
pubDate: pubDate: 2024-04-14
year: 2024
month: 04
day: 14
--- ---
These are my 'spark notes' on _God Is Not Great_, Christopher Hitchen's classic These are my 'spark notes' on _God Is Not Great_, Christopher Hitchen's classic

View File

@@ -2,10 +2,7 @@
title: How I made YouTube work for me title: How I made YouTube work for me
description: >- description: >-
I just learned YouTube channels have an RSS feed. This is terrific news. I just learned YouTube channels have an RSS feed. This is terrific news.
pubDate: pubDate: 2024-05-02
year: 2024
month: 05
day: 02
--- ---
One of my bad habits in life is wandering through YouTube. I've always had One of my bad habits in life is wandering through YouTube. I've always had

View File

@@ -2,10 +2,7 @@
title: LLMs do not understand anything title: LLMs do not understand anything
description: >- description: >-
Save this for the next time someone tells you that LLMs 'understand' things. Save this for the next time someone tells you that LLMs 'understand' things.
pubDate: pubDate: 2024-06-13
year: 2024
month: 06
day: 13
--- ---
LLMs do not understand what they are talking about. They just don't. It's not LLMs do not understand what they are talking about. They just don't. It's not

View File

@@ -3,10 +3,7 @@ title: Doctor Who, Gayness, and the Church
description: >- description: >-
Series 14 of Doctor Who has a schizophrenic relationship with Christianity. Series 14 of Doctor Who has a schizophrenic relationship with Christianity.
Its also gay. I think there might be a connection. Its also gay. I think there might be a connection.
pubDate: pubDate: 2024-07-08
year: 2024
month: 07
day: 8
--- ---
Ive recently finished the most recent series of Doctor Who, series fourteen (or Ive recently finished the most recent series of Doctor Who, series fourteen (or

View File

@@ -1,19 +1,13 @@
import { defineCollection, z } from 'astro:content'; import { defineCollection, z } from 'astro:content';
const dateSchema = z.object({
year: z.number(),
month: z.number(),
day: z.number(),
});
const blog = defineCollection({ const blog = defineCollection({
type: 'content', type: 'content',
schema: z.object({ schema: z.object({
title: z.string(), title: z.string(),
hidden: z.optional(z.boolean()), hidden: z.optional(z.boolean()),
description: z.string(), description: z.string(),
pubDate: dateSchema, pubDate: z.date(),
updatedDate: z.optional(dateSchema), updatedDate: z.optional(z.date()),
}), }),
}); });

View File

@@ -9,11 +9,6 @@ type Props = CollectionEntry<'blog'>['data'];
const { title, description, pubDate, updatedDate } = Astro.props; const { title, description, pubDate, updatedDate } = Astro.props;
const canonicalUrl = new URL(Astro.url.pathname, Astro.site); const canonicalUrl = new URL(Astro.url.pathname, Astro.site);
const pubDateStr = `${pubDate.year}-${pubDate.month}-${pubDate.day}`;
const updatedDateStr = updatedDate ?
`${updatedDate.year}-${updatedDate.month}-${updatedDate.day}`
: pubDateStr;
--- ---
<html lang="en"> <html lang="en">
@@ -32,9 +27,9 @@ const updatedDateStr = updatedDate ?
</span> </span>
{ updatedDate { updatedDate
? ( ? (
<span>Updated: <FormattedDate date={updatedDateStr} className="dt-updated"/>.</span> <span>Updated: <FormattedDate date={updatedDate} className="dt-updated"/>.</span>
<span>Originally published: <FormattedDate date={pubDateStr} className="dt-published"/>.</span> <span>Originally published: <FormattedDate date={pubDate} className="dt-published"/>.</span>
) : <span>Published: <FormattedDate date={pubDateStr} className="dt-published"/>.</span> ) : <span>Published: <FormattedDate date={pubDate} className="dt-published"/>.</span>
} }
<span hidden><a class="u-url uid" href={canonicalUrl}>Permalink</a></span> <span hidden><a class="u-url uid" href={canonicalUrl}>Permalink</a></span>
</aside> </aside>

View File

@@ -26,7 +26,7 @@ export async function GET(context: APIContext) {
link: `/blog/${post.slug}`, link: `/blog/${post.slug}`,
title: post.data.title, title: post.data.title,
content: mdParser.render(post.body), content: mdParser.render(post.body),
pubDate: new Date(`${post.data.pubDate.year}-${post.data.pubDate.month}-${post.data.pubDate.day}`), pubDate: post.data.pubDate,
description: post.data.description, description: post.data.description,
author: 'Joe Carstairs', author: 'Joe Carstairs',

View File

@@ -1,10 +1,7 @@
--- ---
title: Tracking pixels title: Tracking pixels
description: Concerning a notice in the privacy policy of an HR app description: Concerning a notice in the privacy policy of an HR app
pubDate: pubDate: 2024-04-10
year: 2024
month: 04
day: 10
--- ---
I recently made the mistake of reading the privacy policy of the HR app my I recently made the mistake of reading the privacy policy of the HR app my