Skip to content

Commit 19730bc

Browse files
committed
add post counts to tags page
1 parent 34931b0 commit 19730bc

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

eleventy.config.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,27 @@ export default function (eleventyConfig) {
1515
// Collections
1616
eleventyConfig.addCollection('tagsList', collectionApi => {
1717
const knownTags = new Set(tags);
18-
const foundTags = collectionApi.getAll()
18+
const tagMap = collectionApi.getAll()
1919
.flatMap(item => item.data.tags || [])
2020
.reduce((acc, tag) => {
21-
acc.add(tag);
21+
if (!acc.has(tag)) {
22+
acc.set(tag, { name: tag, count: 0 });
23+
}
24+
acc.get(tag).count += 1;
2225
return acc;
23-
}, new Set());
24-
foundTags.delete('articles');
26+
}, new Map());
27+
tagMap.delete('articles'); // all items have this tag
2528

2629
// check that we know all tags and all known tags are actually used
30+
const foundTags = new Set(tagMap.keys());
2731
if (!setEquals(knownTags, foundTags)) {
2832
const tagsToAdd = foundTags.difference(knownTags);
2933
const tagsToRemove = knownTags.difference(foundTags);
3034
throw new Error(`Update list of tags! Add: ${[...tagsToAdd]} Remove: ${[...tagsToRemove]}`);
3135
}
32-
return [...foundTags];
36+
37+
// return values only, sorted by name
38+
return [...tagMap.values()].sort((a, b) => a.name.localeCompare(b.name));
3339
});
3440

3541
// add templates for all tags

tags.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: Tags
77
<p>All tags used in articles</p>
88
<ul>
99
{%- for tag in collections.tagsList %}
10-
<li><a href="/tags/{{ tag | slugify }}">{{ tag }}</a></li>
10+
<li><a href="/tags/{{ tag.name | slugify }}">{{ tag.name }}</a>: {{ tag.count }} post{{ "" if tag.count == 1 else "s" }}</li>
1111
{%- endfor %}
1212
</ul>
1313
</section>

0 commit comments

Comments
 (0)