The code I was using to fetch all blog posts from the directory. This code is working okay,but got an upgrade with the addition of a featured category to be displayed on the main page.
/**blogPosts is an array of posts which is filled conditionally.
if the query variable has a value,
the posts are filtered based on that value,
else all posts are fetched from the directory and then sorted with
descending dates.**/
const blogPosts: Post[] = query
? getFilteredPosts(query).filter((post) => post.publish)
: getAllPosts()
.filter((post) => post.publish)
.sort(
(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
);
The following code is extracted from the category/[category]/page.tsx file,before it had the functionality to accomodate both categories and search filter.
const blogPosts =
category === query
? getFilteredPosts(query).filter((post) => post.publish)
: params.category === "All"
? getAllPosts().filter((post) => post.publish)
: getPostsbyCategory(params.category);
const blogPosts = getAllPosts().filter((post, i) => post.publish);