diff --git a/src/components/get-publishable-posts.ts b/src/components/get-posts.ts similarity index 72% rename from src/components/get-publishable-posts.ts rename to src/components/get-posts.ts index 8242e35..86da888 100644 --- a/src/components/get-publishable-posts.ts +++ b/src/components/get-posts.ts @@ -1,5 +1,17 @@ import { getCollection, type CollectionEntry } from "astro:content"; +const getTime = (post: CollectionEntry<"docs">): number => { + if (post.data.lastUpdated instanceof Date) { + return post.data.lastUpdated.getTime(); + } + + if (typeof post.data.lastUpdated === "number") { + return post.data.lastUpdated; + } + + return 0; +}; + export const getPublishablePosts = async () => { const posts = await getCollection( "docs", @@ -9,11 +21,8 @@ export const getPublishablePosts = async () => { !post.data.draft && post.data.pagefind ); - return posts.sort( - (postA, postB) => - (postB.data.lastUpdated as Date).getTime() - - (postA.data.lastUpdated as Date).getTime() - ); + + return posts.sort((postA, postB) => getTime(postB) - getTime(postA)); }; const sections = ["React", "Dark mode", "CSS", "TypeScript", "Yarn", "Others"]; diff --git a/src/content/docs/posts/full.mdx b/src/content/docs/posts/full.mdx index 496076e..0da283d 100644 --- a/src/content/docs/posts/full.mdx +++ b/src/content/docs/posts/full.mdx @@ -4,7 +4,7 @@ template: splash --- import LinkToPost from "@components/LinkToPost.astro"; -import { getPublishablePostsBySection } from "@components/get-publishable-posts"; +import { getPublishablePostsBySection } from "@components/get-posts"; { Object.entries(await getPublishablePostsBySection()).map(([section, posts]) => { diff --git a/src/content/docs/posts/index.mdx b/src/content/docs/posts/index.mdx index 7da4678..d4f0b27 100644 --- a/src/content/docs/posts/index.mdx +++ b/src/content/docs/posts/index.mdx @@ -4,7 +4,7 @@ template: splash --- import LinkToPost from "@components/LinkToPost.astro"; -import { getPublishablePosts } from "@components/get-publishable-posts"; +import { getPublishablePosts } from "@components/get-posts"; ### Latest posts