From 0f561b0353f607ee8c42568c2df2f09019778f88 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Wed, 25 Sep 2024 23:05:49 -0400 Subject: [PATCH] fix post order --- app.js | 23 ++++++++++++----------- views/index.ejs | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/app.js b/app.js index 2e028f9..868ff4f 100644 --- a/app.js +++ b/app.js @@ -71,6 +71,13 @@ function slugToTitle(slug) { function getAllBlogPosts(page = 1, postsPerPage = 5) { const blogFiles = fs.readdirSync(path.join(__dirname, 'markdown')).filter(file => file.endsWith('.md')); + // Sort by birthtime (latest first) like in your RSS feed + blogFiles.sort((a, b) => { + const statA = fs.statSync(path.join(__dirname, 'markdown', a)).birthtime; + const statB = fs.statSync(path.join(__dirname, 'markdown', b)).birthtime; + return statB - statA; // Descending order, latest first + }); + // Paginate the results const totalPosts = blogFiles.length; const totalPages = Math.ceil(totalPosts / postsPerPage); @@ -83,24 +90,18 @@ function getAllBlogPosts(page = 1, postsPerPage = 5) { const title = file.replace('.md', '').replace(/-/g, ' '); // Keep original casing for title const slug = titleToSlug(title); // Convert title to slug (lowercase) - // Get the last modified date of the markdown file + // Get the creation time of the markdown file const stats = fs.statSync(path.join(__dirname, 'markdown', file)); - const dateCreated = new Date(stats.birthtime); // Use birthtime for last modification time - - // Format the date - const formattedDate = dateCreated.toLocaleDateString('en-US', { - year: 'numeric', - month: 'long', - day: 'numeric' - }); + const dateCreated = new Date(stats.birthtime); // Use birthtime for sorting and displaying return { - title, // Original casing title + title, slug, - date: formattedDate // Include the formatted date + dateCreated, // Keep as Date object for sorting }; }); + // Return the paginated and sorted blog posts and the total pages return { blogPosts, totalPages }; } diff --git a/views/index.ejs b/views/index.ejs index 112e0a9..a30ff0b 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -37,22 +37,30 @@

Where Technology Meets Creativity: Insights from a Linux Enthusiast

- +

Recent Posts

    - <% blogPosts.sort((a, b) => new Date(b.date) - new Date(a.date)).forEach(post => { %> + <% blogPosts.forEach(post => { %>
  • <%= post.title %>
    -

    Posted on <%= post.date %>

    +

    Posted on + <%= new Date(post.dateCreated).toLocaleDateString('en-US', { + year: 'numeric', + month: 'long', + day: 'numeric' + }) %> +

    Read Article
  • <% }) %>
- +
+
+