diff --git a/config/collections/tags.js b/config/collections/tags.js index 42ee86a..026ed31 100644 --- a/config/collections/tags.js +++ b/config/collections/tags.js @@ -1,10 +1,30 @@ module.exports = (eleventyConfig) => { eleventyConfig.addCollection('tags', (collectionApi) => { const tags = new Set(); - collectionApi.getAll().forEach((item) => { - if (!item.data.tags) return; - item.data.tags.forEach((tag) => tags.add(tag)); - }); + collectionApi.getFilteredByTag("posts") + .filter(p => process.env.DEBUG || !p.data.tags.includes("draft")) + .filter(p => process.env.DEBUG || p.date < new Date()) + .forEach((item) => { + if (!item.data.tags) return; + item.data.tags.forEach((tag) => tags.add(tag)); + }); return [...tags]; }); + + eleventyConfig.addCollection('tagPosts', (collectionApi) => { + const tagPosts = {}; + collectionApi.getFilteredByTag("posts") + .filter(p => process.env.DEBUG || !p.data.tags.includes("draft")) + .filter(p => process.env.DEBUG || p.date < new Date()) + .forEach((item) => { + if (!item.data.tags) return; + item.data.tags.forEach((tag) => { + if (!(tag in tagPosts)) { + tagPosts[tag] = []; + } + tagPosts[tag] = [item, ...tagPosts[tag]]; + }); + }); + return tagPosts; + }); } \ No newline at end of file diff --git a/src/_includes/components/blog.njk b/src/_includes/components/blog.njk new file mode 100644 index 0000000..5e9d489 --- /dev/null +++ b/src/_includes/components/blog.njk @@ -0,0 +1,30 @@ +
+

{{ title }}

+ + + +
    + {% for item in posts %} +
  1. +

    {{ item.data.title | safe }}

    + +

    {{ item.content | excerpt }}

    + Read more +
  2. + {% endfor %} +
+ {% if not tag %} + + {% endif %} +
\ No newline at end of file diff --git a/src/blog/index.html b/src/blog/index.html index 9e8b13a..7515ffe 100644 --- a/src/blog/index.html +++ b/src/blog/index.html @@ -1,38 +1,18 @@ --- -title: Blog +title: Blog posts layout: base.njk pagination: data: collections.posts size: 5 reverse: true + alias: posts eleventyImport: collections: ["posts"] +feeds: + rss: "/rss.xml" + atom: "/atom.xml" + json: "/feed.json" --- -
-

Blog posts

+{% extends "components/blog.njk" %} - - -
    - {% for item in pagination.items %} -
  1. -

    {{ item.data.title | safe }}

    - -

    {{ item.content | excerpt }}

    - Read more -
  2. - {% endfor %} -
- -
+{{ content | safe }} \ No newline at end of file diff --git a/src/blog/tag.html b/src/blog/tag.html index 1aa538c..da08ebf 100644 --- a/src/blog/tag.html +++ b/src/blog/tag.html @@ -1,6 +1,6 @@ ---json { - "title": "Blog", + "title": "Tagged posts", "layout": "base.njk", "pagination": { "data": "collections.tags", @@ -20,25 +20,9 @@ } } --- +{% extends "components/blog.njk" %} +{% set posts = collections.tagPosts[tag] %} +{% set title = "Posts tagged with “" ~ tag ~ "”" %} +{% set feeds = feed %} -
-

Posts tagged “{{ tag }}”

- - - -
    - {% set posts = collections.posts | filterBy(tag) | reverse %} - {% for item in posts %} -
  1. -

    {{ item.data.title | safe }}

    - -
    {{ item.content | excerpt }}
    - Read more -
  2. - {% endfor %} -
-
+{{ content | safe }} diff --git a/src/feeds/tags/atom.njk b/src/feeds/tags/atom.njk index 08aa39f..c3ca2e6 100644 --- a/src/feeds/tags/atom.njk +++ b/src/feeds/tags/atom.njk @@ -23,7 +23,7 @@ eleventyImport: {{ metadata.site.url }} - {%- for post in collections.posts | filterBy(tag) | reverse %} + {%- for post in collections.tagPosts[tag] %} {%- set absolutePostUrl = post.url | absoluteUrl(metadata.site.url) %} {{ post.data.title }} diff --git a/src/feeds/tags/json.njk b/src/feeds/tags/json.njk index e04b1bb..c43a667 100644 --- a/src/feeds/tags/json.njk +++ b/src/feeds/tags/json.njk @@ -21,7 +21,7 @@ eleventyImport: "url": "{{ metadata.site.url }}" }, "items": [ - {%- for post in collections.posts | filterBy(tag) | reverse %} + {%- for post in collections.tagPosts[tag] %} {%- set absolutePostUrl = post.url | absoluteUrl(metadata.site.url) %} { "id": "{{ absolutePostUrl }}", diff --git a/src/feeds/tags/rss.njk b/src/feeds/tags/rss.njk index 2c24a3a..2e94ed0 100644 --- a/src/feeds/tags/rss.njk +++ b/src/feeds/tags/rss.njk @@ -18,8 +18,7 @@ eleventyImport: Posts tagged #{{ tag }} {{ metadata.site.language }} - {% set posts = collections.posts | filterBy(tag) | reverse %} - {%- for post in posts %} + {%- for post in collections.tagPosts[tag] %} {%- set absolutePostUrl = post.url | absoluteUrl(metadata.site.url) %} {{ post.data.title }}