Skip to content

Commit fcf93a0

Browse files
committed
get post counts from collections
1 parent 18f46a5 commit fcf93a0

2 files changed

Lines changed: 9 additions & 12 deletions

File tree

eleventy.config.js

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

2926
// check that we know all tags and all known tags are actually used
30-
const foundTags = new Set(tagMap.keys());
3127
if (!setEquals(knownTags, foundTags)) {
3228
const tagsToAdd = foundTags.difference(knownTags);
3329
const tagsToRemove = knownTags.difference(foundTags);
3430
throw new Error(`Update list of tags! Add: ${[...tagsToAdd]} Remove: ${[...tagsToRemove]}`);
3531
}
3632

37-
// return values only, sorted by name
38-
return [...tagMap.values()].sort((a, b) => a.name.localeCompare(b.name));
33+
return [...foundTags].sort();
3934
});
4035

4136
// add templates for all tags
4237
tags.forEach(tag => {
43-
eleventyConfig.addTemplate(`tags/${slugify(tag)}.njk`, `<p>All articles with the tag <em>${tag}</em></p>`, {
38+
eleventyConfig.addTemplate(`tags/${slugify(tag)}.njk`, `<p>All articles with the tag <em>{{ tag }}</em> ({{ collections[tag].length }})</p>`, {
4439
layout: 'paged.njk',
40+
tag,
4541
title: tag,
4642
pagination: {
4743
data: `collections.${tag}`,

tags.njk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ title: Tags
77
<p>All tags used in articles</p>
88
<ul>
99
{%- for tag in collections.tagsList %}
10-
<li><a href="/tags/{{ tag.name | slugify }}">{{ tag.name }}</a>: {{ tag.count }} post{{ "" if tag.count == 1 else "s" }}</li>
10+
{%- set count = collections[tag].length %}
11+
<li><a href="/tags/{{ tag | slugify }}">{{ tag }}</a>: {{ count }} post{{ "" if count == 1 else "s" }}</li>
1112
{%- endfor %}
1213
</ul>
1314
</section>

0 commit comments

Comments
 (0)