Skip to content

Commit

Permalink
Adds a /tags/ to list all valid tags
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Aug 29, 2018
1 parent e3f3bc1 commit ef77832
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module.exports = function(eleventyConfig) {
});
});

eleventyConfig.addCollection("tagList", require("./_11ty/getTagList"));

eleventyConfig.addPassthroughCopy("img");
eleventyConfig.addPassthroughCopy("css");

Expand Down
1 change: 1 addition & 0 deletions .eleventyignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
README.md
_11ty/
29 changes: 29 additions & 0 deletions _11ty/getTagList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = function(collection) {
let tagList = {};
collection.getAllSorted().forEach(function(item) {
if( "tags" in item.data ) {
let tags = item.data.tags;
if( typeof tags === "string" ) {
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;
}).forEach(function(tag) {
tagList[tag] = true;
});
}
});

// returning an array in addCollection works in Eleventy 0.5.3
return Object.keys(tagList);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"homepage": "https://github.com/11ty/eleventy-base-blog#readme",
"devDependencies": {
"@11ty/eleventy": "^0.4.0",
"@11ty/eleventy": "^0.5.3",
"@11ty/eleventy-plugin-rss": "^1.0.2",
"@11ty/eleventy-plugin-syntaxhighlight": "^1.0.5",
"luxon": "^1.0.0",
Expand Down
10 changes: 10 additions & 0 deletions tags-list.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
permalink: /tags/
layout: layouts/home.njk
---
<h1>Tags</h1>

{% for tag in collections.tagList %}
{% set tagUrl %}/tags/{{ tag }}/{% endset %}
<a href="{{ tagUrl | url }}" class="tag">{{ tag }}</a>
{% endfor %}
3 changes: 2 additions & 1 deletion tags.njk
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ pagination:
permalink: /tags/{{ tag }}/
layout: layouts/home.njk
---

<h1>Tagged “{{ tag }}”</h1>

{% set postslist = collections[ tag ] %}
{% include "postslist.njk" %}

<p>See <a href="/tags/">all tags</a>.</p>

0 comments on commit ef77832

Please sign in to comment.