daflh / vredeburg

A simple starter project to create a blog using Eleventy and Tailwind CSS
https://vredeburg.netlify.app
MIT License
185 stars 67 forks source link

Tags from various paths #21

Open spekulatius opened 2 years ago

spekulatius commented 2 years ago

Hello @dafiulh,

Cool template, thank you for building it!

I've got a number of folders with pages and each page with it's own tags. I was wondering if you know: how I can get tags from non-posts/ pages working easily?

Cheers!

jevgenijs-jefimovs commented 6 months ago

@spekulatius,

I'm not sure I understood your requirement, but you can combine content from multiple folders like this:

module.exports = (coll) => {
    const posts = [...coll.getFilteredByGlob('./posts/*.md', './myfolder/*.md', './anotherfolder/*.md')];

    return posts.reverse();
};
jevgenijs-jefimovs commented 6 months ago

@spekulatius,

Otherwise you can try something like this:

function fromEntries (iterable) {
  return [...iterable].reduce((obj, [key, val]) => {
    obj[key] = val;

    return obj;
  }, {});
}

module.exports = (coll) => {
  const posts = require('./posts')(coll);
  const events = require('./events')(coll);
  const postsAndEvents = posts.concat(events);

  const tagListArr = postsAndEvents
    .reduce((tags, post) => {
      if ('tags' in post.data) {
        tags = tags.concat(post.data.tags);
      }

      return [...new Set(tags)];
    }, [])
    .map((tag) => ([
      tag,
      coll.getFilteredByTag(tag).length
    ]))
    .sort((a, b) => b[1] - a[1]);

  return fromEntries(tagListArr);
};