fix post order
This commit is contained in:
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 };
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user