You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Eleventy标签转换为小写并按字母顺序排序?

解决Eleventy标签过滤器的小写统一与排序需求

以下是修改后的代码,可同时满足你的两个需求:

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

eleventyConfig.addFilter("filterTagList", function filterTagList(tags) {
  return (tags || []).filter(tag => ["all", "no-show"].indexOf(tag) === -1);
});

关键改动说明:

  • 统一标签为小写:在将标签存入Set前,调用tag.toLowerCase()把所有标签转为小写,彻底消除JavaScript、Javascript这类大小写差异导致的重复标签问题。
  • 字母顺序排序:将Set转换为数组后调用sort()方法,默认会按字母顺序对字符串数组进行升序排序,直接提升标签列表的易用性。
  • 过滤逻辑适配:由于所有标签已转为小写,原过滤逻辑里的["all", "no-show"]小写关键词可以准确匹配需要排除的标签,无需额外调整。

内容的提问来源于stack exchange,提问作者wcDogg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 20:35:01