32 lines
675 B
JavaScript
32 lines
675 B
JavaScript
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,
|
|
};
|