From 0a138a608fa0383388852fbab936d0b00c5eb7cb Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Fri, 26 Jul 2024 22:41:59 -0400 Subject: [PATCH] Sort the post list --- src/handlers/home.tsx | 5 ++++- src/services/post-file.ts | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/handlers/home.tsx b/src/handlers/home.tsx index 730fbef..5200c52 100644 --- a/src/handlers/home.tsx +++ b/src/handlers/home.tsx @@ -13,7 +13,10 @@ const app = new Hono<{ Variables: Posts }>(); app.get("/", async (c) => { const postService: PostFileService = c.get('postService'); const posts = postService.getPosts(); - const postList: PostMeta[] = Array.from(posts.values()).map((p: Post) => p.meta); + const postList: PostMeta[] = Array.from(posts.values()) + .map((p: Post) => p.meta) + .sort((p1: Post, p2: Post) => p1.meta.date - p2.meta.date); + return c.render(, { meta: { diff --git a/src/services/post-file.ts b/src/services/post-file.ts index 7356f94..844b8e7 100644 --- a/src/services/post-file.ts +++ b/src/services/post-file.ts @@ -54,6 +54,16 @@ export class PostFileService { return this.posts; } + public getPostsSortedByDate(desc: boolean = true): Post[] { + return Array.from(this.posts.entries()).sort((p1: Post, p2: Post) => { + if (desc) { + return p1.meta.date - p2.meta.date; + } + + return p2.meta.date - p1.meta.date; + }) + } + public getPost(slug: string): Post { const post = this.posts.get(slug);