From 3f199ad319f3bc4c3fc9693441be3941d8b0feee Mon Sep 17 00:00:00 2001 From: Ayc0 Date: Thu, 19 Dec 2024 19:04:46 +0100 Subject: [PATCH] Refactor a bit get-posts --- ...{get-publishable-posts.ts => get-posts.ts} | 19 ++++++++++++++----- src/content/docs/posts/full.mdx | 2 +- src/content/docs/posts/index.mdx | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) rename src/components/{get-publishable-posts.ts => get-posts.ts} (72%) 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 -- 2.51.2