feat: update tags to order by count

This commit is contained in:
Devin Haska 2024-02-11 14:31:32 -08:00
parent 58d679d3c9
commit 2eed920716
10 changed files with 72 additions and 40 deletions

View file

@ -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;
};

View file

@ -36,8 +36,13 @@ const organizeByDate = (collection) => {
return collectionByDate;
};
const filterByCategory = (collection, category) => {
return collection.filter((item) => item.data.categories.includes(category));
};
module.exports = {
entries,
filterByCategory,
formatDate,
keys,
minifyCss,

11
config/utils.js Normal file
View file

@ -0,0 +1,11 @@
const slugify = require("slugify");
const slugifyString = (string) => {
return slugify(string, {
lower: true,
});
};
module.exports = {
slugifyString,
};