sort by most recent first and only display drafts in development mode

This commit is contained in:
Julien Calixte
2022-11-19 15:26:26 +01:00
parent 1f22097a8e
commit eb3a6d142f

View File

@@ -1,35 +1,41 @@
import { computed } from "vue"; import { computed } from "vue"
interface Post { interface Post {
filename: string; filename: string
lastUpdated: Date; lastUpdated: Date
href: string; href: string
title: string; title: string
date: Date
meta: { meta: {
filename: string; filename: string
lastUpdated: Date; lastUpdated: Date
href: string; href: string
}; }
frontmatter: { frontmatter: {
title: string; title: string
publishedAt?: Date; publishedAt?: Date
}; }
draft?: boolean
} }
const byDate = (a: Post, b: Post) => { const byMostRecentFirst = (a: Post, b: Post) => {
if (!b.frontmatter.publishedAt) { if (!b.date) {
return -1; return 1
} }
if (!a.frontmatter.publishedAt) { if (!a.date) {
return 1; return 1
} }
return a.frontmatter.publishedAt <= b.frontmatter.publishedAt ? -1 : 1; return a.date <= b.date ? 1 : -1
}; }
export const usePosts = () => { export const usePosts = () => {
const posts = $(useDocuments<Post>("@/pages/posts")); const posts = $(useDocuments<Post>("@/pages/posts"))
return computed(() => posts.sort(byDate)); return computed(() =>
}; posts
.filter((post) => import.meta.env.DEV || post.draft !== true)
.sort(byMostRecentFirst)
)
}