You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jekyll中获取非_posts目录下的页面并生成列表?

在Jekyll的Github Pages中列出Documentation目录下的所有页面

要实现这一需求,你可以利用Jekyll内置的site.pages变量结合Liquid过滤器,筛选出/documentation/目录下的页面,具体方案如下:

基础实现:列出所有Documentation页面

在/documentation/index.md中添加以下Liquid代码,即可遍历并显示该目录(含子目录)下的所有页面,同时排除着陆页本身:

{% assign docs_pages = site.pages | where_exp: "page", "page.url contains '/documentation/' and page.url != '/documentation/'" %}
{%- for doc in docs_pages -%}
  <div class="documentation-item">
    <h3><a href="{{ doc.url | relative_url }}">{{ doc.title }}</a></h3>
    {% if doc.excerpt %}
      <p>{{ doc.excerpt | strip_html | truncatewords: 30 }}</p>
    {% endif %}
  </div>
{%- endfor -%}

代码说明:

  • site.pages:Jekyll自带变量,包含站点所有非文章(非/_posts/)的页面文件。
  • where_exp:自定义条件过滤,筛选出URL包含/documentation/且不是着陆页(/documentation/)的页面。
  • relative_url:自动适配Github Pages的部署路径,避免链接失效。
  • 每个文档页面需要添加YAML Front Matter(至少包含title字段),例如get-service.md:
    ---
    title: Get-Service 命令详解
    ---
    

进阶实现:按子目录分组显示

如果需要按子目录(如powershell)分组展示文档,可以使用group_by_exp实现自动分组:

{% assign docs_pages = site.pages | where_exp: "page", "page.url contains '/documentation/' and page.url != '/documentation/'" %}
{% assign docs_by_directory = docs_pages | group_by_exp: "page", "page.url | split: '/' | slice: 2 | first" %}

{%- for directory in docs_by_directory -%}
  <h2>{{ directory.name | capitalize }}</h2>
  <ul>
    {%- for doc in directory.items -%}
      <li><a href="{{ doc.url | relative_url }}">{{ doc.title }}</a></li>
    {%- endfor -%}
  </ul>
{%- endfor -%}

代码说明:

  • group_by_exp:按URL分割后的第3段内容(即子目录名称,如powershell)进行分组,自动整理同目录下的文档。
  • capitalize:将目录名首字母大写,优化显示效果。

内容的提问来源于stack exchange,提问作者jeremywat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 03:16:05