Created
March 9, 2019 19:20
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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