Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tags from various paths #21

Open
spekulatius opened this issue Nov 9, 2021 · 2 comments
Open

Tags from various paths #21

spekulatius opened this issue Nov 9, 2021 · 2 comments

Comments

@spekulatius
Copy link

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
Copy link

jevgenijs-jefimovs commented Jan 5, 2024

@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
Copy link

jevgenijs-jefimovs commented Jan 5, 2024

@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);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants