feat: add methods to fetch post categories
This commit is contained in:
parent
3b115e1cab
commit
5283ed6dc3
2 changed files with 36 additions and 0 deletions
32
config/collections/index.js
Normal file
32
config/collections/index.js
Normal 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,
|
||||||
|
};
|
|
@ -14,11 +14,15 @@ const imageShortcode = require("./config/shortcodes/image.js");
|
||||||
|
|
||||||
module.exports = (eleventyConfig) => {
|
module.exports = (eleventyConfig) => {
|
||||||
eleventyConfig.addWatchTarget("./src/assets");
|
eleventyConfig.addWatchTarget("./src/assets");
|
||||||
|
|
||||||
// --------------------- Custom Template Languages ---------------------
|
// --------------------- Custom Template Languages ---------------------
|
||||||
eleventyConfig.addPlugin(
|
eleventyConfig.addPlugin(
|
||||||
require("./config/template-languages/css-config.js"),
|
require("./config/template-languages/css-config.js"),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
eleventyConfig.addCollection("posts", getAllPosts);
|
||||||
|
eleventyConfig.addCollection("categories", getAllPostCategories);
|
||||||
|
|
||||||
// --------------------- Custom Filters -----------------------
|
// --------------------- Custom Filters -----------------------
|
||||||
eleventyConfig.addFilter("entries", entries);
|
eleventyConfig.addFilter("entries", entries);
|
||||||
eleventyConfig.addFilter("formatDate", formatDate);
|
eleventyConfig.addFilter("formatDate", formatDate);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue