feat: update tags to order by count
This commit is contained in:
parent
58d679d3c9
commit
2eed920716
10 changed files with 72 additions and 40 deletions
|
@ -1,5 +1,6 @@
|
|||
const path = require("path").posix;
|
||||
const { dir } = require("../constants.js");
|
||||
const { slugifyString } = require("../utils.js");
|
||||
|
||||
const getAllPosts = (collection) => {
|
||||
const posts = collection.getFilteredByGlob(
|
||||
|
@ -13,7 +14,7 @@ const getAllPostCategories = (collection) => {
|
|||
|
||||
const allCategories = posts.flatMap((post) => post.data.categories);
|
||||
|
||||
const categories = allCategories.reduce((acc, category) => {
|
||||
const categoryCounts = allCategories.reduce((acc, category) => {
|
||||
if (acc[category]) {
|
||||
acc[category]++;
|
||||
} else {
|
||||
|
@ -23,6 +24,15 @@ const getAllPostCategories = (collection) => {
|
|||
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;
|
||||
};
|
||||
|
||||
|
@ -30,14 +40,12 @@ const getPostsByCategory = (collection) => {
|
|||
const posts = getAllPosts(collection);
|
||||
const categories = Object.keys(getAllPostCategories(collection));
|
||||
|
||||
const postsByCategory = {};
|
||||
categories.forEach((category) => {
|
||||
const categoryPosts = posts.filter((post) => {
|
||||
return post.data.categories.includes(category);
|
||||
});
|
||||
|
||||
postsByCategory[category] = categoryPosts;
|
||||
});
|
||||
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;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue