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,
|
||||
};
|
||||
|
|
|
@ -4,6 +4,7 @@ const advancedFormat = require("dayjs/plugin/advancedFormat");
|
|||
|
||||
const postcss = require("postcss");
|
||||
const cssnano = require("cssnano");
|
||||
const { default: slugify } = require("slugify");
|
||||
|
||||
const keys = Object.keys;
|
||||
const values = Object.values;
|
||||
|
@ -40,6 +41,43 @@ const filterByCategory = (collection, category) => {
|
|||
return collection.filter((item) => item.data.categories.includes(category));
|
||||
};
|
||||
|
||||
const allTags = (collection, ignore = []) => {
|
||||
const tagSet = new Set(collection.flatMap((item) => item.data.tags));
|
||||
|
||||
ignore.forEach((tag) => tagSet.delete(tag));
|
||||
|
||||
return [...tagSet];
|
||||
};
|
||||
|
||||
const allTagCounts = (collection, ignore = ["post"]) => {
|
||||
if (!collection.length) {
|
||||
throw new Error("Invalid collection, no items");
|
||||
}
|
||||
|
||||
const tagCounts = new Map();
|
||||
|
||||
for (const item of collection) {
|
||||
const tags = item.data.tags;
|
||||
|
||||
tags?.forEach((tag) => tagCounts.set(tag, (tagCounts.get(tag) || 0) + 1));
|
||||
}
|
||||
|
||||
ignore.forEach((tag) => tagCounts.delete(tag));
|
||||
|
||||
const tagArray = Array.from(tagCounts.entries())
|
||||
.map(([tag, count]) => ({
|
||||
tag,
|
||||
count,
|
||||
}))
|
||||
.sort((a, b) => b.count - a.count);
|
||||
|
||||
return tagArray;
|
||||
};
|
||||
|
||||
const filter = (collection, filters = []) => {
|
||||
return collection.filter((item) => !filters.includes(item));
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
entries,
|
||||
filterByCategory,
|
||||
|
@ -48,4 +86,7 @@ module.exports = {
|
|||
minifyCss,
|
||||
organizeByDate,
|
||||
values,
|
||||
allTags,
|
||||
allTagCounts,
|
||||
filter,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue