File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 >
You can’t perform that action at this time.
0 commit comments