输出link到"parent page"?

Output link to "parent page"?

我有一个collection_colletion。其中有一个文件 _collection/path/topic.md 和一个文件夹 _collection/path/topic/,其中包含许多 .md 文件及其内容。这些文件的 permalink 是 /path/topic/path/topic/file-x - 所以一个 parent 页面有一个同名的文件夹,里面有多个随机页面。

现在我想在所有这些 .md 文件中输出一个 link 到 /path/topic,标题为 topic.md 作为 link 文本:

---
title: This is the page title defined in topic.md
---

应该成为 <a href="/path/topic">This is the page title defined in topic.md</a>

如何最轻松地做到这一点?

我能以某种方式访问​​ .md 文件的文件夹名称 topic 并使用它来读取 topic.md 并输出它的 title 并生成 link 到了吗?

我当前的手册 "solution"(或解决方法):

parent 条目添加到 /topic/ 中所有页面的首页,其中包含 topic.md 的标题和相对 URL:

parent: ['Topic Title', '../topic']

在页面模板中:

{% if page.parent %}
  <p>« <a href="{{ page.parent[1] }}">{{ page.parent[0] }}</a></p>
{% endif %}

有效,但当然会重复此信息 n 次,必须手动维护。

这个(选项 1)怎么样?

{% assign pageurl_array = page.url | split: "/" %}
{% assign path = pageurl_array[0] %}
{% assign topic = pageurl_array[1] %}
<p>« <a href="{{ path }}/{{ topic }}/{{ topic }}.html">
  {{ topic | capitalize | replace: "-", " " }}
</a></p>

如果您不介意疯狂的构建时间,请执行此操作(选项 2):

{% assign pageurl_array = page.url | split: "/" %}
{% assign path = pageurl_array[0] %}
{% assign topic = pageurl_array[1] %}
{% capture parent_url %}{{ path }}/{{ topic }}/{{ topic }}.html{% endcapture %}
<p>« <a href="{{ parent_url }}">
  {% for i in site.pages %}
    {% if i.url == parent_url %}
      {{ i.title }}
    {% endif %}
  {% endfor %}
</a></p>

我会选择第一个选项(快得多)并使用这个 javascript 来获得正确的大写字母和特殊字符:

$('a').each( function() {
  var str = $(this).html();
  str = str.replace('Topic from url', 'Topic from URL');
  $(this).html(str);
});

我承认 javascript 解决方案远非完美,但它很好地解决了构建时间问题。

请注意,Jekyll 非常慢。如果您需要更快的构建时间,我建议您深入研究 Hugo。

在对我的问题和其他答案的评论中的讨论中,我注意到我想要构建的实际上是一个非常常见的东西:面包屑导航!非常"small"一个,只有一级。

有了这些新知识,我可以 google "breadcrumb" Jekyll 插件:

此解决方案使用页面路径提取 "crumbs":

link 文本使用文件夹名称。

另一个类似的实现:

另一个:

所以所有这些都没有 title link 文字。


此解决方案实际读取页面 title,但也可以从页面读取 breadcrumb 前文,并将其用作 link 文本:

这个可能是一个有效的解决方案。


也有真正的插件(遗憾的是不能与 Github 页面一起使用):

我的解决方案,基于 JoostS 代码:

{% assign  url = page.url | remove:'.html' | split: "/"  %}
{% assign path = url | pop %}
{% if path.size == 1 %}
    <a class="back" href="/home/">home</a>
{% else %}
    <a class="back" href="/{% for dir in path offset: 1 %}{{ dir | append: "/" }}{% endfor %}">{{ path | last }}</a>
{% endif %}```