feat: move categories to tags
This commit is contained in:
parent
b6c9a2b75c
commit
327b38f35b
56 changed files with 125 additions and 136 deletions
|
@ -1,65 +1,18 @@
|
|||
const path = require("path").posix;
|
||||
const { dir } = require("../constants.js");
|
||||
const { slugifyString } = require("../utils.js");
|
||||
const postsByTag = (collection) => {
|
||||
const posts = collection.getFilteredByTag("post");
|
||||
|
||||
const getAllPosts = (collection) => {
|
||||
const posts = collection.getFilteredByGlob(
|
||||
path.join(dir.input, "content/posts/**/*.md"),
|
||||
);
|
||||
return posts.reverse();
|
||||
};
|
||||
const postsByTag = {};
|
||||
|
||||
const getAllPostCategories = (collection) => {
|
||||
const posts = getAllPosts(collection);
|
||||
|
||||
const allCategories = posts.flatMap((post) => post.data.categories);
|
||||
|
||||
const categoryCounts = allCategories.reduce((acc, category) => {
|
||||
if (acc[category]) {
|
||||
acc[category]++;
|
||||
} else {
|
||||
acc[category] = 1;
|
||||
for (const post of posts) {
|
||||
for (const tag of post.data.tags) {
|
||||
postsByTag[tag] ??= [];
|
||||
postsByTag[tag].push(post);
|
||||
}
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const categories = Object.entries(categoryCounts)
|
||||
.map(([category, count]) => ({
|
||||
key: slugifyString(category),
|
||||
title: category,
|
||||
href: `/tags/${slugifyString(category)}`,
|
||||
count: count,
|
||||
}))
|
||||
.sort((a, b) => b.count - a.count);
|
||||
|
||||
return categories;
|
||||
};
|
||||
|
||||
const getPostsByCategory = (collection) => {
|
||||
const posts = getAllPosts(collection);
|
||||
const categories = Object.keys(getAllPostCategories(collection));
|
||||
|
||||
const postsByCategory = categories
|
||||
.map((category) => ({
|
||||
category,
|
||||
posts: posts.filter((post) => post.data.categories.includes(category)),
|
||||
}))
|
||||
.sort((a, b) => b.posts.length - a.posts.length);
|
||||
|
||||
return postsByCategory;
|
||||
};
|
||||
|
||||
const getAllCatalogue = (collection) => {
|
||||
const items = collection.getFilteredByGlob(
|
||||
path.join(dir.input, "content/catalogue/**/*.md"),
|
||||
);
|
||||
return items.reverse();
|
||||
return postsByTag;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getAllCatalogue,
|
||||
getAllPostCategories,
|
||||
getAllPosts,
|
||||
getPostsByCategory,
|
||||
postsByTag,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue