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 path = require("path").posix;
const { dir } = require("../constants.js"); const { dir } = require("../constants.js");
const { slugifyString } = require("../utils.js");
const getAllPosts = (collection) => { const getAllPosts = (collection) => {
const posts = collection.getFilteredByGlob( const posts = collection.getFilteredByGlob(
@ -13,7 +14,7 @@ const getAllPostCategories = (collection) => {
const allCategories = posts.flatMap((post) => post.data.categories); const allCategories = posts.flatMap((post) => post.data.categories);
const categories = allCategories.reduce((acc, category) => { const categoryCounts = allCategories.reduce((acc, category) => {
if (acc[category]) { if (acc[category]) {
acc[category]++; acc[category]++;
} else { } else {
@ -23,6 +24,15 @@ const getAllPostCategories = (collection) => {
return acc; 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; return categories;
}; };
@ -30,14 +40,12 @@ const getPostsByCategory = (collection) => {
const posts = getAllPosts(collection); const posts = getAllPosts(collection);
const categories = Object.keys(getAllPostCategories(collection)); const categories = Object.keys(getAllPostCategories(collection));
const postsByCategory = {}; const postsByCategory = categories
categories.forEach((category) => { .map((category) => ({
const categoryPosts = posts.filter((post) => { category,
return post.data.categories.includes(category); posts: posts.filter((post) => post.data.categories.includes(category)),
}); }))
.sort((a, b) => b.posts.length - a.posts.length);
postsByCategory[category] = categoryPosts;
});
return postsByCategory; return postsByCategory;
}; };

View file

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

View file

@ -12,6 +12,7 @@ const {
values, values,
organizeByDate, organizeByDate,
keys, keys,
filterByCategory,
} = require("./config/filters/index.js"); } = require("./config/filters/index.js");
const markdown = require("./config/plugins/markdown.js"); const markdown = require("./config/plugins/markdown.js");
const imageShortcode = require("./config/shortcodes/image.js"); const imageShortcode = require("./config/shortcodes/image.js");
@ -31,6 +32,7 @@ module.exports = (eleventyConfig) => {
// --------------------- Custom Filters ----------------------- // --------------------- Custom Filters -----------------------
eleventyConfig.addFilter("entries", entries); eleventyConfig.addFilter("entries", entries);
eleventyConfig.addFilter("filterByCategory", filterByCategory);
eleventyConfig.addFilter("formatDate", formatDate); eleventyConfig.addFilter("formatDate", formatDate);
eleventyConfig.addFilter("keys", keys); eleventyConfig.addFilter("keys", keys);
eleventyConfig.addFilter("minifyCss", minifyCss); eleventyConfig.addFilter("minifyCss", minifyCss);

View file

@ -28,5 +28,8 @@
"postcss-import": "^16.0.0", "postcss-import": "^16.0.0",
"postcss-import-ext-glob": "^2.1.1", "postcss-import-ext-glob": "^2.1.1",
"prettier": "3.2.4" "prettier": "3.2.4"
},
"dependencies": {
"slugify": "^1.6.6"
} }
} }

View file

@ -1,17 +1,19 @@
{% set itemsByYear = items | organizeByDate %} {% set itemsByYear = items | organizeByDate %}
{% set years = itemsByYear | keys | sort("desc") %} {% set years = itemsByYear | keys | sort("desc") %}
<section> <section class="[ archive ]">
{% for year in years %} {% for year in years %}
{% set itemsInYear = itemsByYear[year] %} {% set itemsInYear = itemsByYear[year] %}
<div class="[ flex ]"> <div class="[ flex mt-1.5 mb-0.5 ]">
<h2>{{ year }}</h2> <h2>{{ year }}</h2>
</div> </div>
{% for item in itemsInYear %} <div class="[ items ] [ flex-col gap-0.25 ]">
<div class="[ flex items-center justify-between ]"> {% for item in itemsInYear %}
<a href="{{ item.url }}">{{ item.data.title }}</a> <div class="[ flex items-center justify-between ]">
<div class="[ archive-divider ] [ mx-0.5 ]"></div> <a href="{{ item.url }}">{{ item.data.title }}</a>
<div>{{ item.date | formatDate("MM/DD") }}</div> <hr class="[ archive-divider ] [ mx-0.5 ]" />
</div> <p class="[ archive-date ]">{{ item.date | formatDate("MM/DD") }}</p>
{% endfor %} </div>
{% endfor %}
</div>
{% endfor %} {% endfor %}
</section> </section>

View file

@ -5,6 +5,7 @@
flex: 1; flex: 1;
} }
.archive-month { .archive-date {
margin-left: auto; color: var(--color-text-soft);
letter-spacing: 0.05em;
} }

View file

@ -18,3 +18,11 @@
.justify-between { .justify-between {
justify-content: space-between; justify-content: space-between;
} }
.flex-wrap {
flex-wrap: wrap;
}
.flex-nowrap {
flex-wrap: nowrap;
}

View file

@ -1,20 +1,14 @@
--- ---
eleventyComputed: eleventyComputed:
title: "Tag: {{ tag }}" title: "Tag: {{ category.title }}"
permalink: "{{ category.href }}/index.html"
layout: base layout: base
pagination: pagination:
data: collections.postsByCategory data: collections.categories
size: 1 size: 1
alias: tag alias: category
permalink: /tag/{{ tag }}/index.html
--- ---
{% set posts = collections.postsByCategory[tag] %} <h1>Tag: {{ category.title }}</h1>
<h1>Tag: {{ tag }}</h1> {% set items = collections.posts | filterByCategory(category.title) %}
<ul> {% include "partials/archive.html" %}
{% for post in posts %}
<li>
<a href="{{ post.url }}">{{ post.data.title }}</a>
</li>
{% endfor %}
</ul>

View file

@ -5,14 +5,12 @@ title: All tags
--- ---
<section class="[ flow ]"> <section class="[ flow ]">
<h1>Tags</h1> <h1>{{ title }}</h1>
{% set categories = collections.categories | entries %} <ol class="[ flex flex-wrap gap-0.5 list-none p-0 mb-0 ]">
<ol class="[ flex gap-1 list-none p-0 mb-0 ]"> {% for category in collections.categories %}
{% for tag in categories %}
{% set key = tag | first %}
{% set count = tag | last %}
<li> <li>
<a href="/tag/{{ key }}" class="[ pill ] [ flex px-1 py-0.5 gap-0.5 ]">{{ key }} <span class="[ pill-count ]">{{ count }}</span></a> <a href="{{ category.href }}"
class="[ pill ] [ flex px-1 py-0.5 gap-0.5 ]">{{ category.title }} <span class="[ pill-count ]">{{ category.count }}</span></a>
</li> </li>
{% endfor %} {% endfor %}
</ol> </ol>