Jekyll 3.0.x 可以_include_ index.html 中的 page.url

Can Jekyll 3.0.x _include_ the index.html in page.url

看到很多问题询问如何在以前版本的 Jekyll 中省略 index.html,但我想要 包含它。

是否有任何配置变量或其他变量可以使 Jekyll 3.0.x 包含它?

按页面,我指的是您在执行类似操作时获得的对象

<ul>
{% for page in site.pages %}
  <li>{{ page.url }}</li>
{% endfor %}
</ul>

不是当前页面。

找到了这个解决方案,但不是很实用:

    {% assign last_url_char = page.url | split: '' | last %}
    {% if last_url_char == '/' %}
    {%    assign page_url = page.url | append: "index.html" %}
    {% else %}
    {%    assign page_url = page.url %}
    {% endif %}

您需要在永久链接设置中指定 .html 扩展名:

 permalink: /blog/:slug.html

然后所有页面都会有该扩展名。

更新

创建全新的 3.0 安装:

$ jekyll _3.0_ new mysite
$ cd mysite

编辑 about.html 并删除自定义永久链接以查看我们生成具有 .html 扩展名的页面后的变化:

---
layout: page
title: About
permalink: /about/    <--- delete
---

页面帖子指定不同的固定链接_config.yml:

defaults:
 -   
  scope:
    type: pages
  values:
    permalink: /:path/:basename:output_ext
 -   
  scope:
    type: posts
  values:
    permalink: /:year/:month/:day/:title.html

将以下代码添加到 index.html 以查看可用页面及其 url:

<p>Current page: {{page.url}}</p>

{% for my_page in site.pages %}
{% if my_page.title %}
url: {{my_page.url }}
{% endif %}
{% endfor %}

现在提供网站 $ jekyll _3.0_ serve 并访问 http://localhost:4000/index.html