Skip to content

Instantly share code, notes, and snippets.

@danfascia
Created March 9, 2019 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danfascia/b39acf304f7a700f388219aee33da849 to your computer and use it in GitHub Desktop.
Save danfascia/b39acf304f7a700f388219aee33da849 to your computer and use it in GitHub Desktop.
Used in config to create a collection (tagList) of tag names which can be iterated.
/*
Usage:
eleventyConfig.addCollection("tagList", require("11ty_getTagList.js") );
This collection then produces a useful list...
for tag in collections.tagList...
which then gives access to
collections[tag]
*/
module.exports = function(collection) {
let tagSet = new Set();
collection.getAllSorted().forEach(function(item) {
if( "tags" in item.data ) {
let tags = item.data.tags;
if( typeof tags === "string" ) {
tags = [tags];
}
tags = tags.filter(function(item) {
switch(item) {
// this list should match the `filter` list in tags.njk
case "all":
case "nav":
case "post":
case "posts":
return false;
}
return true;
});
for (const tag of tags) {
tagSet.add(tag);
}
}
});
// returning an array in addCollection works in Eleventy 0.5.3
return [...tagSet].sort();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment