fix post order
This commit is contained in:
parent
c0d530be27
commit
0f561b0353
23
app.js
23
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 };
|
||||
}
|
||||
|
||||
|
@ -42,16 +42,24 @@
|
||||
<div class="container">
|
||||
<h2>Recent Posts</h2>
|
||||
<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">
|
||||
<div>
|
||||
<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>
|
||||
<a href="/blog/<%= post.slug %>" class="btn btn-outline-primary">Read Article</a>
|
||||
</li>
|
||||
<% }) %>
|
||||
</ul>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- Pagination controls -->
|
||||
<nav aria-label="Page navigation">
|
||||
|
Loading…
Reference in New Issue
Block a user