From eb3a6d142fb369c528c7b9e7e04cf98e4ce336e9 Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Sat, 19 Nov 2022 15:26:26 +0100 Subject: [PATCH] sort by most recent first and only display drafts in development mode --- src/hooks/usePosts.hook.ts | 50 +++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/hooks/usePosts.hook.ts b/src/hooks/usePosts.hook.ts index ab7e439..df40041 100644 --- a/src/hooks/usePosts.hook.ts +++ b/src/hooks/usePosts.hook.ts @@ -1,35 +1,41 @@ -import { computed } from "vue"; +import { computed } from "vue" interface Post { - filename: string; - lastUpdated: Date; - href: string; - title: string; + filename: string + lastUpdated: Date + href: string + title: string + date: Date meta: { - filename: string; - lastUpdated: Date; - href: string; - }; + filename: string + lastUpdated: Date + href: string + } frontmatter: { - title: string; - publishedAt?: Date; - }; + title: string + publishedAt?: Date + } + draft?: boolean } -const byDate = (a: Post, b: Post) => { - if (!b.frontmatter.publishedAt) { - return -1; +const byMostRecentFirst = (a: Post, b: Post) => { + if (!b.date) { + return 1 } - if (!a.frontmatter.publishedAt) { - return 1; + if (!a.date) { + return 1 } - return a.frontmatter.publishedAt <= b.frontmatter.publishedAt ? -1 : 1; -}; + return a.date <= b.date ? 1 : -1 +} export const usePosts = () => { - const posts = $(useDocuments("@/pages/posts")); + const posts = $(useDocuments("@/pages/posts")) - return computed(() => posts.sort(byDate)); -}; + return computed(() => + posts + .filter((post) => import.meta.env.DEV || post.draft !== true) + .sort(byMostRecentFirst) + ) +}