我也无法让split 正常工作,但breadcrumbs are still possible。以下内容改编自我在该答案中的代码(请注意应在if 语句中修改的部分,这是可读版本,不能按预期精确工作)。
{% capture url_parts %} {{ page.url | remove: "/index.html" | replace:'/'," " }}{% endcapture %}
{% capture num_parts %}{{ url_parts | number_of_words }}{% endcapture %}
{% assign previous="" %}
{% if num_parts == "0" %}
<Handle being at the top of the site (i.e. "site.com/") here>
{% else %}
{% for unused in page.content limit:num_parts %}
{% capture first_word %}{{ url_parts | truncatewords:1 | remove:"…"}}{% endcapture %}
{{ first_word }} »
{% capture url_parts %}{{ url_parts | remove_first:first_word }}{% endcapture %}
{% endfor %}
{% endif %}
如果用户在/a/b/c.html,这将打印a » b » c »,如果他们在/a/b/(或等效的/a/b/index.html),它将只打印a » b »。
至少,它将接近于:对于 Markdown 文件,每次打印 first_word 之间有太多换行符,因此它们被视为单独的段落,输出将是(这在HTML 文件,但需要更多标签才能使其正常工作):
a »
b »
c »
这可以通过将整个for循环放在一行来解决(这是应该使用的代码):
{% capture url_parts %} {{ page.url | remove: "/index.html" | replace:'/'," " }}{% endcapture %}
{% capture num_parts %}{{ url_parts | number_of_words }}{% endcapture %}
{% assign previous="" %}
{% if num_parts == "0" %}
<Handle being at the top of the site (i.e. "site.com/") here>
{% else %}
{% for unused in page.content limit:num_parts %}{% capture first_word %}{{ url_parts | truncatewords:1 | remove:"..."}}{% endcapture %}{{ first_word }} »{% capture url_parts %}{{ url_parts | remove_first:first_word }}{% endcapture %}{% endfor %}
{% endif %}
(注意。for 循环中的page.content 只是为了提供一些东西来迭代,魔术是由limit:num_parts 完成的。但是,这意味着如果page.content 的段落比num_parts 少,则不会所有面包屑都会出现,如果这很可能在_config.yml 中定义一个站点变量,如breadcrumb_list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 并使用site.breadcrumb_list 作为占位符而不是page.content。(从我的其他答案中提取。))
Here is an example(它没有使用与上面完全相同的代码,但只是做了一些小修改,并且它在HTML文件中,因此不存在新行创建段落的问题)。