Skip to content

Commit

Permalink
Sort blog posts by latest date
Browse files Browse the repository at this point in the history
  • Loading branch information
WanderedToLa committed Jul 18, 2024
1 parent 4028462 commit 263d99e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/plugins/blog-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@ async function createBlogDataPlugin(context, options) {
const frontMatter = matter(content).data;
if (frontMatter.slug && frontMatter.title) {
const dateStr = file.split('-').slice(0, 3).join('-');
const dateObj = new Date(dateStr);

posts.push({
slug: frontMatter.slug,
title: frontMatter.title,
date: dateStr,
date: dateObj,
});
}
}
}
});

posts.sort((a, b) => b.date - a.date);

return posts.slice(0, 5);
return posts.slice(0, 5).map((post) => ({
...post,
date: post.date.toISOString().split('T')[0],
}));
},

async contentLoaded({ content, actions }) {
Expand Down

0 comments on commit 263d99e

Please sign in to comment.