feat: add methods to fetch post categories

This commit is contained in:
Devin Haska 2024-02-06 23:00:04 -08:00
parent 3b115e1cab
commit 5283ed6dc3
2 changed files with 36 additions and 0 deletions

View file

@ -0,0 +1,32 @@
const path = require("path");
const { dir } = require("../constants.js");
const getAllPosts = (collection) => {
const posts = collection.getFilteredByGlob(
path.join(dir.input, "content/posts/**/*.md"),
);
return posts.reverse();
};
const getAllPostCategories = (collection) => {
const posts = getAllPosts(collection);
const allCategories = posts.flatMap((post) => post.data.categories);
const categories = allCategories.reduce((acc, category) => {
if (acc[category]) {
acc[category]++;
} else {
acc[category] = 1;
}
return acc;
}, {});
return categories;
};
module.exports = {
getAllPosts,
getAllPostCategories,
};