11ty / eleventy-base-blog

A starter repository for a blog web site using the Eleventy static site generator.
https://eleventy-base-blog.netlify.app/
MIT License
1.2k stars 609 forks source link

How to transform tags to lowercase and sort alphabetically? #162

Closed wcDogg closed 8 months ago

wcDogg commented 8 months ago

Hello and thanks for a great project !

I'm using the getAllTags and filterTagList filters and they work as expected. However, I would like to:

  1. Transform all tags to lower case to reduce Output Conflict from tags like Javascript, JavaScript, and javascript.
  2. Sort tags alphabetically for usability.

I stink at JS and after several hours of trying, I must admit defeat. Any guidance would be much appreciated :)

wcDogg commented 8 months ago

Got a working answer from Stack Overflow. Here is the modified filter:

eleventyConfig.addFilter("getAllTags", collection => {
  let tagSet = new Set();
  for(let item of collection) {
    (item.data.tags || []).forEach(tag => tagSet.add(tag.toLowerCase()));
  }
  return Array.from(tagSet).sort();
});