danurbanowicz / eleventy-netlify-boilerplate

A template for building a blog with the Eleventy static site generator and Decap CMS
https://eleventy-netlify-boilerplate.netlify.app/
MIT License
528 stars 183 forks source link

Is the tag post really necessary? #28

Closed botelho closed 4 years ago

botelho commented 4 years ago

I'm trying to wrap my head around this: The tag "post" is specified in all posts/*.md files and also in the posts/posts.json file. As far as I understand this would allow to access collections.post, thought it would be unnecessary to specify the tag for all post md files. Anyway, I notice that a collection "posts" is added in the .eleventy.js config file which is then used to display the list of posts under blog. I'm trying to understand all this because I'm having issues using collections.[tagname] when specifying that tagname only in the directory configuration file. Any help and clarification on this would be much appreciated. Thanks!

danurbanowicz commented 4 years ago

Hi @botelho the declaration of the post tag in multiple places was unnecessary and confusing, so I've removed those instances. Thanks for spotting that!

Note that in the eleventy.js configuration, we're already creating the 'post' collection using eleventyConfig.addCollection, so it is unnecessary to declare the 'post' tag again elsewhere.

Can you describe in more detail the issues you're having using your collections?

It could be that you need to use https://www.11ty.dev/docs/data-deep-merge/ in order to stop your tags declared in a directory data file from being overridden by those in your content front matter, which is currently the default Eleventy behaviour.

In eleventy.js

module.exports = function(eleventyConfig) {
  eleventyConfig.setDataDeepMerge(true);
};
botelho commented 4 years ago

Hi @danurbanowicz, thank you for the update! The issue I'm having, and having this boilerplate as example, is that removing

eleventyConfig.addCollection("posts", function(collection) {
  return collection.getAllSorted().filter(function(item) {
    return item.inputPath.match(/^\.\/posts\//) !== null;
  });
});

from .eleventy.js and adding

tags: posts

to posts/posts.json would be sufficient to access collections.posts from anywhere, like in blog.njk:

{% set postslist = collections.posts %}

That is not working for me and I think it's supposed to work, just like accessing collections.[tagname] works for tags that are set per post (in the front matter of each post.md file).. I hope that makes sense. Maybe I'm missing something?

danurbanowicz commented 4 years ago

You're correct that you should be able to declare your collection solely in a directory data file.

However, in case you haven't spotted this, tags: posts won't work in your directory data file as it's not valid JSON.

You'll need to declare your tags (and any other data) in your directory .json file like this:-

{
  "tags": [
    "posts"
  ]
}

I'm hoping this is why it wasn't working for you.. but if not let me know!

botelho commented 4 years ago

OK, got it! I see I did miss

eleventyConfig.setDataDeepMerge(true);

in the .eleventy.js file. Without that, only tags inside each post will be considered for collections.[tagname] and the "tags":"posts" in the directory data file will be ignored. I completely missed this in the documentation. Thanks so much for clarifying this. I appreciate it.

danurbanowicz commented 4 years ago

Excellent, glad you got it working!