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,
};

View file

@ -14,11 +14,15 @@ const imageShortcode = require("./config/shortcodes/image.js");
module.exports = (eleventyConfig) => {
eleventyConfig.addWatchTarget("./src/assets");
// --------------------- Custom Template Languages ---------------------
eleventyConfig.addPlugin(
require("./config/template-languages/css-config.js"),
);
eleventyConfig.addCollection("posts", getAllPosts);
eleventyConfig.addCollection("categories", getAllPostCategories);
// --------------------- Custom Filters -----------------------
eleventyConfig.addFilter("entries", entries);
eleventyConfig.addFilter("formatDate", formatDate);