Skip to content

Commit e1e0c34

Browse files
committed
refactor: standardize date handling in post components
1 parent 69b62a7 commit e1e0c34

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

src/components/posts-loop.astro

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@ const allPosts = await getCollection("post");
44
55
const { count } = Astro.props;
66
7-
function parseDate(dateStr) {
8-
const [month, day, year] = dateStr.split(" ");
9-
return new Date(`${month} ${parseInt(day)}, ${year}`);
10-
}
7+
const formatter = new Intl.DateTimeFormat("en-US", {
8+
month: "short",
9+
day: "numeric",
10+
year: "numeric",
11+
});
1112
1213
const sortedPosts = allPosts
13-
.map((post) => ({
14-
...post,
15-
date: parseDate(post.data.dateFormatted),
16-
}))
17-
.sort((a, b) => b.date.getTime() - a.date.getTime());
14+
.sort((a, b) => b.data.date.getTime() - a.data.date.getTime());
1815
19-
const postsLoop = sortedPosts.slice(0, count).map((post) => {
20-
return {
21-
...(post.data || {}),
22-
link: `/post/${post.slug}`,
23-
};
24-
});
16+
const postsLoop = sortedPosts.slice(0, count).map((post) => ({
17+
title: post.data.title,
18+
description: post.data.description,
19+
dateISO: post.data.date.toISOString(),
20+
dateFormatted: formatter.format(post.data.date),
21+
link: `/post/${post.slug}`,
22+
}));
2523
---
2624

2725
{
@@ -85,7 +83,7 @@ const postsLoop = sortedPosts.slice(0, count).map((post) => {
8583
<span>{post.description}</span>
8684
</p>
8785
<div class="mt-2.5 text-xs font-medium text-neutral-800 dark:text-neutral-300">
88-
Posted on {post.dateFormatted}
86+
<time datetime={post.dateISO}>Posted on {post.dateFormatted}</time>
8987
</div>
9088
</div>
9189
</div>

src/content/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const postCollection = defineCollection({
55
schema: z.object({
66
title: z.string(),
77
description: z.string(),
8-
dateFormatted: z.string(),
8+
// Store canonical date as Date object; format at render time
9+
date: z.date(),
910
}),
1011
});
1112

src/content/post/waterbase.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: The Easiest Database you could ever need!
33
description: As a developer, you might have heard of FireBase. But do you know about.....WaterBase ??
4-
dateFormatted: Aug 2, 2022
4+
date: 2022-08-02
55
---
66
### As a developer, you might have heard of FireBase. But do you know about.....WaterBase ??
77

src/content/post/yantra-launcher.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Why I built an entire Launcher for Android?
33
description: Let's change the definition of Android Launchers...for the greater good!
4-
dateFormatted: Sep 26, 2022
4+
date: 2022-09-26
55
---
66
### Let's change the definition of Android Launchers...for the greater good!
77

src/pages/post/[slug].astro

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ const { entry } = Astro.props;
1414
// Render markdown entry to get the compiled Content component
1515
const { Content } = await entry.render();
1616
const frontmatter = entry.data;
17+
const formattedDate = new Intl.DateTimeFormat("en-US", {
18+
month: "short",
19+
day: "numeric",
20+
year: "numeric",
21+
}).format(frontmatter.date);
1722
---
18-
<PostLayout frontmatter={frontmatter}>
23+
<PostLayout frontmatter={{ ...frontmatter, dateFormatted: formattedDate }}>
1924
<Content />
2025
</PostLayout>

0 commit comments

Comments
 (0)