【问题标题】:Output link to "parent page"?输出链接到“父页面”?
【发布时间】:2023-03-20 20:15:01
【问题描述】:

我有一个集合_colletion。其中有一个文件_collection/path/topic.md 和一个文件夹_collection/path/topic/,其中包含许多带有内容的.md 文件。这些文件的永久链接是 /path/topic 和 /path/topic/file-x - 因此父页面具有同名文件夹,其中包含多个随机页面。

现在我想在所有这些.md 文件中输出指向/path/topic 的链接,标题为topic.md 作为链接文本:

---
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 并生成指向它的链接?

【问题讨论】:

  • “主题”不是 page.url 的一部分吗?
  • 是的,file-x.md 将得到/path/topic/file-x 作为它的page.url。我希望能够以某种方式从这里转到topic,然后转到title 和topic.md 的URL,以便我可以链接到那里。
  • 如果 topic.md 被称为 index.md 会更容易...
  • 并放入/topic/文件夹?如果它的 URL 保持为 /path/topic(没有斜杠应该可以工作或至少被重定向),我会很好。

标签: jekyll liquid


【解决方案1】:

我当前的手动“解决方案”(或解决方法):

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

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

在页面模板中:

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

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

【讨论】:

  • 不是很干,但构建时间可接受的解决方案。
【解决方案2】:

这个(选项 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。

【讨论】:

  • 哦,非常手动 ;) 看起来可以修改 href(/{{ path }}/{{ topic }} 在我的情况下就足够了)并替换我的部分“解决方法”。但我也想自动从topic.md 中获取实际的title 属性......(所以链接文本不应该是“主题”,而是“这是主题中定义的页面标题,md”)
  • 您可以遍历 site.pages 并匹配 url 以找到此标题...但这会(显着)增加构建时间。我不会那样做,因为这对我来说似乎没有必要。
  • 是的,我希望有一个我不知道的(高性能)“给我这个文件/页面的标题”功能。
【解决方案3】:

在 cmets 讨论我的问题和其他答案时,我注意到我想要构建的实际上是一个非常常见的东西:面包屑导航!只是一个非常“小”的,只有一层。

有了这些新知识,我可以用谷歌搜索 Jekyll 的“面包屑”插件:

此方案使用页面路径提取“碎屑”:

它使用文件夹名称作为链接文本。

另一个类似的实现:

另一个:

所以所有这些都没有title 链接文本。


这个解决方案实际上是读取页面title,但也可以从页面中读取breadcrumb frontmatter,并将其用作链接文本:

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


还有真正的插件(不幸的是,它们不适用于 Github Pages):

【讨论】:

    【解决方案4】:

    我的解决方案,基于 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 %}```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      相关资源
      最近更新 更多