From 711eb24cb0b8c03bc43edffd082e91d970fc3dc9 Mon Sep 17 00:00:00 2001
From: Furkan Sahin <furkan-dev@proton.me>
Date: Wed, 21 Jul 2021 04:42:31 -0500
Subject: Added a timestamp and sorts posts by timestamp

---
 utils/Posts.tsx | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

(limited to 'utils/Posts.tsx')

diff --git a/utils/Posts.tsx b/utils/Posts.tsx
index d82467e..2a69629 100644
--- a/utils/Posts.tsx
+++ b/utils/Posts.tsx
@@ -7,6 +7,7 @@ marked.setOptions(markedOptions);
 
 interface PostMetadata {
   name: string;
+  lastUpdated: string;
 }
   
 interface Post {
@@ -45,9 +46,20 @@ async function getPostFromDirectory(directory : string) {
 async function getPosts() : Promise<Post[]> {
   const directories = await fs.readdir(POST_PATH);
 
-  const posts = directories.map(getPostFromDirectory);
+  const posts = await Promise.all(directories.map(getPostFromDirectory));
 
-  return await Promise.all(posts);
+  return posts.sort((post_a, post_b) => {
+    const a = new Date(post_a.meta.lastUpdated);
+    const b = new Date(post_b.meta.lastUpdated);
+
+    if(a === b)
+      return 0;
+    
+    if(a > b)
+      return -1;
+    
+    return 1;
+  });
 }
 
 async function getMarkdown(post : Post) : Promise<string> {
-- 
cgit v1.2.3

