Skip to content

Commit 8f04f49

Browse files
committed
refactor(blog): update date handling in blog posts to use last edit date
1 parent f501950 commit 8f04f49

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

source.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export const blogPosts = defineCollections({
2525
dir: "content/blog",
2626
schema: frontmatterSchema.extend({
2727
author: z.string(),
28-
date: z.iso.date().or(z.date()),
2928
category: z.enum(["devlog", "updates", "other"]),
3029
}),
3130
})

src/app/(home)/blog/page.tsx

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,40 @@
33
import { blog } from "@/lib/source"
44
import { PathUtils } from "fumadocs-core/source"
55
import { BlogSidebar } from "@/components/blog/blog-sidebar"
6+
import { lastEdit } from "@/lib/api"
67

78
function getName(path: string) {
89
return PathUtils.basename(path, PathUtils.extname(path))
910
}
1011

1112
export default async function Blog() {
12-
const posts = [...blog.getPages()].sort(
13+
const pages = blog.getPages()
14+
15+
const postsWithDates = await Promise.all(
16+
pages.map(async (page) => ({
17+
page,
18+
lastEditDate: await lastEdit(page),
19+
}))
20+
)
21+
22+
const posts = postsWithDates.sort(
1323
(a, b) =>
14-
new Date(b.data.date ?? getName(b.path)).getTime() -
15-
new Date(a.data.date ?? getName(a.path)).getTime(),
24+
new Date(b.lastEditDate ?? getName(b.page.path)).getTime() -
25+
new Date(a.lastEditDate ?? getName(a.page.path)).getTime(),
1626
)
1727

18-
const serializablePosts = posts.map((p) => ({
19-
path: p.path,
20-
data: {
21-
title: p.data.title,
22-
description: p.data.description,
23-
category: p.data.category,
24-
url: p.url,
25-
date:
26-
(typeof p.data.date === "string" ? p.data.date : p.data.date?.toISOString()) ?? undefined,
27-
},
28-
}))
28+
const serializablePosts = await Promise.all(
29+
posts.map(async (p) => ({
30+
path: p.page.path,
31+
data: {
32+
title: p.page.data.title,
33+
description: p.page.data.description,
34+
category: p.page.data.category,
35+
url: p.page.url,
36+
date: p.lastEditDate,
37+
},
38+
}))
39+
)
2940

3041
return (
3142
<>

0 commit comments

Comments
 (0)