Sort the post list

This commit is contained in:
Dave Smith-Hayes 2024-07-26 22:41:59 -04:00
parent 586ab0287b
commit 0a138a608f
2 changed files with 14 additions and 1 deletions

View File

@ -13,7 +13,10 @@ const app = new Hono<{ Variables: Posts }>();
app.get("/", async (c) => { app.get("/", async (c) => {
const postService: PostFileService = c.get('postService'); const postService: PostFileService = c.get('postService');
const posts = postService.getPosts(); 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(<Home posts={postList} />, { return c.render(<Home posts={postList} />, {
meta: { meta: {

View File

@ -54,6 +54,16 @@ export class PostFileService {
return this.posts; 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 { public getPost(slug: string): Post {
const post = this.posts.get(slug); const post = this.posts.get(slug);