fix post order

This commit is contained in:
Raven Scott 2024-09-25 23:05:49 -04:00
parent c0d530be27
commit 0f561b0353
2 changed files with 24 additions and 15 deletions

23
app.js
View File

@ -71,6 +71,13 @@ function slugToTitle(slug) {
function getAllBlogPosts(page = 1, postsPerPage = 5) { function getAllBlogPosts(page = 1, postsPerPage = 5) {
const blogFiles = fs.readdirSync(path.join(__dirname, 'markdown')).filter(file => file.endsWith('.md')); 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 // Paginate the results
const totalPosts = blogFiles.length; const totalPosts = blogFiles.length;
const totalPages = Math.ceil(totalPosts / postsPerPage); 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 title = file.replace('.md', '').replace(/-/g, ' '); // Keep original casing for title
const slug = titleToSlug(title); // Convert title to slug (lowercase) 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 stats = fs.statSync(path.join(__dirname, 'markdown', file));
const dateCreated = new Date(stats.birthtime); // Use birthtime for last modification time const dateCreated = new Date(stats.birthtime); // Use birthtime for sorting and displaying
// Format the date
const formattedDate = dateCreated.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
return { return {
title, // Original casing title title,
slug, 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 }; return { blogPosts, totalPages };
} }

View File

@ -42,16 +42,24 @@
<div class="container"> <div class="container">
<h2>Recent Posts</h2> <h2>Recent Posts</h2>
<ul class="list-group list-group-flush"> <ul class="list-group list-group-flush">
<% blogPosts.sort((a, b) => new Date(b.date) - new Date(a.date)).forEach(post => { %> <% blogPosts.forEach(post => { %>
<li class="list-group-item d-flex justify-content-between align-items-center py-4"> <li class="list-group-item d-flex justify-content-between align-items-center py-4">
<div> <div>
<h5 class="mb-1"><a href="/blog/<%= post.slug %>"> <%= post.title %> </a></h5> <h5 class="mb-1"><a href="/blog/<%= post.slug %>"> <%= post.title %> </a></h5>
<p class="mb-1 text-muted">Posted on <%= post.date %></p> <p class="mb-1 text-muted">Posted on
<%= new Date(post.dateCreated).toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
}) %>
</p>
</div> </div>
<a href="/blog/<%= post.slug %>" class="btn btn-outline-primary">Read Article</a> <a href="/blog/<%= post.slug %>" class="btn btn-outline-primary">Read Article</a>
</li> </li>
<% }) %> <% }) %>
</ul> </ul>
</div>
</section>
<!-- Pagination controls --> <!-- Pagination controls -->
<nav aria-label="Page navigation"> <nav aria-label="Page navigation">