diff --git a/config/collections/index.js b/config/collections/index.js index c886e13..d1269db 100644 --- a/config/collections/index.js +++ b/config/collections/index.js @@ -26,7 +26,24 @@ const getAllPostCategories = (collection) => { return categories; }; +const getPostsByCategory = (collection) => { + const posts = getAllPosts(collection); + const categories = Object.keys(getAllPostCategories(collection)); + + const postsByCategory = {}; + categories.forEach((category) => { + const categoryPosts = posts.filter((post) => + post.data.categories.includes(category), + ); + + postsByCategory[category] = categoryPosts; + }); + + return postsByCategory; +}; + module.exports = { getAllPosts, getAllPostCategories, + getPostsByCategory, }; diff --git a/eleventy.config.js b/eleventy.config.js index bed958d..ef8c6de 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -1,6 +1,7 @@ const { getAllPosts, getAllPostCategories, + getPostsByCategory, } = require("./config/collections/index.js"); const { dir } = require("./config/constants.js"); const { @@ -22,6 +23,7 @@ module.exports = (eleventyConfig) => { eleventyConfig.addCollection("posts", getAllPosts); eleventyConfig.addCollection("categories", getAllPostCategories); + eleventyConfig.addCollection("postsByCategory", getPostsByCategory); // --------------------- Custom Filters ----------------------- eleventyConfig.addFilter("entries", entries); diff --git a/src/_layouts/post.html b/src/_layouts/post.html index 8d88c2e..442694d 100644 --- a/src/_layouts/post.html +++ b/src/_layouts/post.html @@ -7,7 +7,11 @@ layout: base
+ View all tags +
+