Eleventy 11ty - 2 个独立目录的标签列表排序

Posted

技术标签:

【中文标题】Eleventy 11ty - 2 个独立目录的标签列表排序【英文标题】:Eleventy 11ty - tag list sorting for 2 independent directories 【发布时间】:2021-10-19 04:54:05 【问题描述】:

我有两个单独的目录,一个是 /sights/,另一个是 /sounds/。我想独立运行它们,每个都有自己的标签。

为了实现这一点,我在 11.js 中创建了两个不同的集合:

  // SIGHTS - custom collections
  const nowsights = new Date();
  const sightslivePosts = (post) => post.date <= nowsights;
  eleventyConfig.addCollection("sights", (collection) => 
    return [
      ...collection.getFilteredByGlob("./sights/*.njk").filter(sightslivePosts),
    ].reverse();
  );

  // SOUNDS - custom collections
  const nowsounds = new Date();
  const soundslivePosts = (post) => post.date <= nowsounds;
  eleventyConfig.addCollection("sounds", (collection) => 
    return [
      ...collection.getFilteredByGlob("./sounds/*.njk").filter(soundslivePosts),
    ].reverse();
  );

然后我补充说:

  // generate a list of all tags collections
  eleventyConfig.addCollection('tagsList', (collectionApi) => 
    const tagsSet = new Set()
    collectionApi.getAll().forEach((item) => 
      if (!item.data.tags) return
      item.data.tags
        .filter((tag) => !['foo', 'bar'].includes(tag))
        .forEach((tag) => tagsSet.add(tag))
    )
    return [...tagsSet].sort((a, b) => b.localeCompare(a))
  )

我想建造十一:

/sights/tags/(景点的所有标签);

/sounds/tags/(声音的所有标签)。

但是我得到了:

/sights/tags/(所有景点和声音的标签);

/sounds/tags/(声音和景象的所有标签)。

另外请注意,我在每个目录里面都有一个index.njk和一个tags.njk,如下:

/sights/index.njk

---
title: Archive
pagination:
  data: collections.sights
  size: 4
  alias: sights
---
  % for tag in collections.tagsList %
      <a href="/sights/tags/ tag | slug " class="tags">tag</a>
  % endfor %

/sights/tags.njk

---
pagination:
  data: collections
  size: 1
  alias: tag
  addAllPagesToCollections: true
permalink: /sights/tags/ tag | slug /
---
% for tag in collections.tagsList %
    <a href="/sights/tags/ tag | slug " class="tags">tag</a>
% endfor %

/sounds/index.njk

---
title: Archive
pagination:
  data: collections.sounds
  size: 4
  alias: sounds
---
  % for tag in collections.tagsList %
      <a href="/sounds/tags/ tag | slug " class="tags">tag</a>
  % endfor %

/sounds/tags.njk

---
pagination:
  data: collections
  size: 1
  alias: tag
  addAllPagesToCollections: true
permalink: /sights/tags/ tag | slug /
---
% for tag in collections.tagsList %
    <a href="/sounds/tags/ tag | slug " class="tags">tag</a>
% endfor %

我实在想不通这个问题。我是一个新手,尽管到目前为止我用 110 构建我的网站没有任何问题,但现在我正在尝试更复杂的东西,我发现自己陷入了困境。我已经尝试过无数次尝试,从主要的开发人员文章中汲取灵感,但我还没有找到我力所能及的解决方案。我还在学习。你可能已经注意到我的英语也很有限,我是意大利人。如果您能帮助我,我将不胜感激。

雷蒙德的后续报道:

我也尝试过您的建议,在 /sights/tags.njk 中写入“data: colletions.sights”,在 /sounds/tags.njk 中写入“data: collections.sounds”,但我明白了:

Problem writing Eleventy templates: (more in DEBUG output)
> Having trouble rendering njk template ./sights/tags.njk

`TemplateContentRenderError` was thrown
> (./sights/tags.njk)
  Error: slugify: string argument expected

`Template render error` was thrown:
    Template render error: (./sights/tags.njk)
      Error: slugify: string argument expected
        at Object._prettifyError (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\lib.js:36:11)
        at C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:563:19
        at Template.root [as rootRenderFunc] (eval at _compile (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:633:18), <anonymous>:19:3)
        at Template.render (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:552:10)
        at C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\@11ty\eleventy\src\Engines\Nunjucks.js:236:14
        at new Promise (<anonymous>)
        at C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\@11ty\eleventy\src\Engines\Nunjucks.js:235:14
        at Template.render (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\@11ty\eleventy\src\TemplateContent.js:199:28)
        at async Template._getLink (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\@11ty\eleventy\src\Template.js:130:26)
        at async Template.getOutputLink (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\@11ty\eleventy\src\Template.js:161:16)
Copied 90 files / Wrote 0 files in 0.99 seconds (v0.12.1)
Watching…

================================================ ===============

雷蒙德的第二次跟进:

我正在尝试您的建议,以使用十一计算来定义永久链接。我做了几次尝试,都失败了。这对我来说似乎是最有意义的——我在理解这个功能方面存在局限性。

    在 /sights/index.njk 中我进行了如下更改:

---
pagination:
  data: collections.sights
  size: 1
  alias: tag
  addAllPagesToCollections: true

title: /sights/tags/
parent: " tag | slug /"
eleventyComputed:
  eleventyNavigation:
    key: " title "
    parent: " parent "
---
% for tag in collections.sights %
    <a href="/sights/tags/ tag | slug " class="tags">tag</a>
% endfor %

    在 /sounds/index.njk 中我进行了如下更改:

---
pagination:
  data: collections.sounds
  size: 1
  alias: tag
  addAllPagesToCollections: true

title: /sounds/tags/
parent: " tag | slug /"
eleventyComputed:
  eleventyNavigation:
    key: " title "
    parent: " parent "
---
% for tag in collections.sounds %
    <a href="/sounds/tags/ tag | slug " class="tags">tag</a>
% endfor %

    在 111.js 中我删除了: "// 生成所有标签集合的列表", 我刚离开: "// SIGHTS - 自定义集合" 和 "// SOUNDS - 自定义集合"; 这是最后的 11.js:

  // Articles sights - custom collections
  const nowsights = new Date();
  const sightslivePosts = (post) => post.date <= nowsights;
  eleventyConfig.addCollection("sights", (collection) => 
    return [
      ...collection.getFilteredByGlob("./sights/*.njk").filter(sightslivePosts),
    ].reverse();
  );

  // Articles sounds - custom collections
  const nowsounds = new Date();
  const soundslivePosts = (post) => post.date <= nowsounds;
  eleventyConfig.addCollection("sounds", (collection) => 
    return [
      ...collection.getFilteredByGlob("./sounds/*.njk").filter(soundslivePosts),
    ].reverse();
  );

这就是我得到的。据我所知,我仍然遇到同样的错误,因为我无法正确定义永久链接,但我无法获得任何线索。

Problem writing Eleventy templates: (more in DEBUG output)
> Having trouble rendering njk template ./sights/tags.njk

`TemplateContentRenderError` was thrown
> (./sights/tags.njk)
  Template render error: (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\_includes\partials\components\post-list.njk)
  Error: slugify: string argument expected

`Template render error` was thrown:
    Template render error: (./sights/tags.njk)
      Template render error: (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\_includes\partials\components\post-list.njk)
      Error: slugify: string argument expected
        at Object._prettifyError (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\lib.js:36:11)
        at C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:563:19
        at eval (eval at _compile (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:633:18), <anonymous>:53:12)
        at eval (eval at _compile (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:633:18), <anonymous>:60:12)
        at C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:571:11
        at Template.root [as rootRenderFunc] (eval at _compile (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:633:18), <anonymous>:75:3)
        at Template.render (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:552:10)
        at eval (eval at _compile (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:633:18), <anonymous>:59:10)
        at fn (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\a-sync-waterfall\index.js:26:24)
        at C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\a-sync-waterfall\index.js:66:22
Copied 90 files / Wrote 0 files in 0.69 seconds (v0.12.1)
Watching…
[Browsersync] Reloading Browsers...

================================================ ===============

雷蒙德的第三次跟进:

我用不同的变体进行了更多尝试,但都失败了。它们都返回相同的模板错误“_includes\partials\components\post-list.njk”,如下所示 - 除了下面列出的第二次尝试


`TemplateContentRenderError` was thrown
> (./sights/tags.njk)
  Template render error: (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\_includes\partials\components\post-list.njk)
  Error: slugify: string argument expected

这里有几个不同的错误:

    尝试了 /sounds/tags.njk 和 /sights/tags.njk(这只是 /sights/ 示例)

---
pagination:
  data: collections.sights
  size: 1
  alias: tag
  addAllPagesToCollections: true

url: /sights/tags/ tag | slug /
eleventyComputed:
    key: " url "
---

我明白了:

Problem writing Eleventy templates: (more in DEBUG output)
> Having trouble rendering njk template ./sights/tags.njk

`TemplateContentRenderError` was thrown
> (./sights/tags.njk)
  Template render error: (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\_includes\partials\components\post-list.njk)
  Error: slugify: string argument expected

`Template render error` was thrown:
    Template render error: (./sights/tags.njk)
      Template render error: (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\_includes\partials\components\post-list.njk)
      Error: slugify: string argument expected
        at Object._prettifyError (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\lib.js:36:11)
        at C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:563:19
        at eval (eval at _compile (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:633:18), <anonymous>:53:12)
        at eval (eval at _compile (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:633:18), <anonymous>:60:12)
        at C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:571:11
        at Template.root [as rootRenderFunc] (eval at _compile (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:633:18), <anonymous>:75:3)
        at Template.render (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:552:10)
        at eval (eval at _compile (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:633:18), <anonymous>:59:10)
        at fn (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\a-sync-waterfall\index.js:26:24)
        at C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\a-sync-waterfall\index.js:66:22
Copied 90 files / Wrote 0 files in 2.71 seconds (v0.12.1)
Watching…
[Browsersync] Reloading Browsers...

    尝试了 /sounds/tags.njk 和 /sights/tags.njk(这只是 /sights/ 示例)

---
pagination:
  data: collections.sights
  size: 1
  alias: tag
  addAllPagesToCollections: true
permalink: /sights/tags/ url /

eleventyComputed:
  url: " tag | slug "
---

我明白了:

Problem writing Eleventy templates: (more in DEBUG output)
> Having trouble rendering njk template ./sights/tags.njk

`TemplateContentRenderError` was thrown
> (./sights/tags.njk)
  Error: slugify: string argument expected

`Template render error` was thrown:
    Template render error: (./sights/tags.njk)
      Error: slugify: string argument expected
        at Object._prettifyError (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\lib.js:36:11)
        at C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:563:19
        at Template.root [as rootRenderFunc] (eval at _compile (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:633:18), <anonymous>:17:3)
        at Template.render (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\nunjucks\src\environment.js:552:10)
        at C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\@11ty\eleventy\src\Engines\Nunjucks.js:236:14
        at new Promise (<anonymous>)
        at C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\@11ty\eleventy\src\Engines\Nunjucks.js:235:14
        at Template.render (C:\Users\ricca\OneDrive\Documenti\Websites\rawriddims\node_modules\@11ty\eleventy\src\TemplateContent.js:199:28)
        at runMicrotasks (<anonymous>)
        at processTicksAndRejections (internal/process/task_queues.js:93:5)
Copied 90 files / Wrote 0 files in 0.57 seconds (v0.12.1)
Watching…
[Browsersync] Reloading Browsers...

【问题讨论】:

忘了说;我正在运行 110 v0.12.1 看看你前面的父值。它也在尝试使用动态值。暂时删除它。 谢谢雷蒙德。我不知道我是否正确理解了如何从前面的内容中删除动态值。我已经用我所做的一些尝试和不同的错误更新了我的问题。 所以基本上,如果你在前面有任何东西,而不是在十一计算中,那是基于分页数据的,它是行不通的。现在这变得有点混乱且难以调试。我可以建议以下内容。您能否制作一个非常小的站点副本,删除所有可能的内容。只是尝试“景点”,而不是声音。把它写成一个可复制的案例,然后放到 GitHub 上我可以试试? 另外,在上面的 #2 中,您在永久链接中使用 url。不,将永久链接放在十一计算下。我相信文档表明了这一点。 【参考方案1】:

“但是通过以下内容,我在每个目录中都获得了两个集合中所有标签的唯一列表。”

这里有点难以理解你的意思,但问题可能是这样的:

pagination:
  data: collections

应该是collections.something,其中something 是景象或声音。

关于您的第二个问题,分页支持 reverse 值,记录在此处 (https://www.11ty.dev/docs/pagination/#reverse-the-data)。看起来是这样的:

---
pagination:
  data: testdata
  size: 2
  reverse: true
testdata:
 - item1
 - item2
 - item3
 - item4
---

【讨论】:

我有两个单独的目录,一个用于 /sights,一个用于 /sounds。 Eleventy 应该在每个标签中构建各自的标签:/sights/tags/(所有景点标签)和 /sounds/tags/(所有声音标签)。但问题是我得到了/sights/tags/(所有景点和声音的标签)和/sounds/tags/(所有的声音和景点标签)。我已经尝试过您的建议,在 /sights/tags.njk 中写入“data: colletions.sights”,在 /sounds/tags.njk 中写入“data: collections.sounds”,但我得到: > 渲染 njk 模板时遇到问题。 /sights/tags.njk ...错误:slugify:需要字符串参数 现在有点难以看到你在哪里。首先,我建议编辑您的问题以显示您当前的代码。如果我能看到您当前的版本,我可以提供帮助。 问题已按照您的建议进行了编辑。谢谢。 你确定吗?因为对于您的两个标签页面,您仍在对集合进行分页,而不是对 collections.sights(或声音)进行分页。 是的,正如我所描述的那样。我也尝试过你之前的建议,在 /sights/tags.njk 中写入“data: colletions.sights”,在 /sounds/tags.njk 中写入“data: collections.sounds”,但我得到: > 渲染 njk 时遇到问题模板 ./sights/tags.njk ... 错误:slugify:需要字符串参数。我现在再次用错误详细信息编辑我的问题

以上是关于Eleventy 11ty - 2 个独立目录的标签列表排序的主要内容,如果未能解决你的问题,请参考以下文章

如何通过前端变量(Eleventy / 11ty)过滤处理过的文件?

是否可以在 .eleventy 配置文件中使用“addGlobalData”?

分页不适用于 11ty(十一)中的可扩展布局

在 Eleventy 中维护外部链接

如何使用 Eleventy 的灰质节选?

您如何按包含标签的帖子数量对博客帖子标签列表进行排序(在 Eleventy 中使用 Nunjucks)?