Jekyll 链接集合中的文档?

2023-11-30

在 Jekyll 的 Front Matter 中,有没有办法引用另一个文档?

我有一个自定义集合,并且想在每个文档中添加元数据,例如“父主题”(指向父主题的链接)和“子主题”(文档数组)或“相关主题”。

通过这样的引用,我可以访问链接文档的元数据,例如标题、URL 或其他任意数据。

这个想法是文档的层次结构,包含主题、子主题、子子主题等。主题页面可以显示子主题列表或父主题的面包屑等。


真正的问题值得真正的答案。我也遇到了这个文档问题。下列的本·巴尔特的建议,我开始使用collections。我们的想法是让

  • 反映主题/子主题排列的目录,
  • 每页都有一个面包屑

我放弃了,因为它的编码最简单pages。所以,这就是我如何使用页面制作文档。

先决条件:

  • 文档位于文件夹中,例如:文档

  • permalink被设定为pretty在_config.yml中

  • 文件夹层次结构描述文档组织

example

documentation
|--index.html
|--chapter-1
|  |--index.html
|
|--chapter-2
|  |--index.html
|  |
|  |--part-1
|  |  |--index.html
|  |  |--subpart-1
|  |     |--index.html
|  |--part-2
|  |  |--index.html
|  |
|  |--part-3.html

Note : documentation/chapter-2/part-2/index.html也可以是documentation/chapter-2/part-2.html, 因为permalink被设定为pretty,生成的页面将位于documentation/chapter-2/part-2/index.html.

  • 同一级别的页面按照以下顺序排序weight前面的变量。这可以是你想要的任何东西。 按第十个编号可以轻松插入新文档。

前面的例子

---
title: My title
weight: 10
---
  • 文档从中获取默认变量值_config.yml

example

defaults:
  -
    scope:
      path: "documentation"
      type: pages

    values:
      isDoc: true # allows quick extraction from site.pages
      layout: page

一旦满足了这些先决条件,就可以轻松打印目录和面包屑。

表中的内容

_includes/show-children.html

{% assign parentDir = include.dir %}
{% if parentDir == nil %}<h1>You must specify a root directory</h1>{% endif %}

{% assign allDocs = include.docs %}
{% if allDocs == nil %}{% assign allDocs = site.pages | sort: "weight" %}{% endif %}

{% assign level = include.level %}
{% if level == nil %}{% assign level = parentDir | remove_first: "/" | split:"/" | size %}{% endif %}

{% assign maxLevel = include.maxLevel %}
{% if maxLevel == nil %}{% assign maxLevel = 100 %}{% endif %}

{% assign nextLevel = level | plus : 1 %}

{% comment %}+++++++++++++++++++++++++++++++++++++++++++++++++
Looking for all page in this path with the same level (siblings)
This avoid to deep recursion and error like :
__ Liquid Exception: Nesting too deep __
+++++++++++++++++++++++++++++++++++++++++++++++++{% endcomment %}

{% assign siblings = "" | split: "/" %}
{% for s in allDocs %}
    {% assign sPageLevel = s.url | remove_first: "/" | split:"/" | size %}
    {% if sPageLevel == level and s.url contains parentDir %}
        {% if s.title %}{% assign siblings = siblings | push: s %}{% endif %}
    {% endif %}
{% endfor %}

<ul>
{% for p in siblings %}
    <li><a href="{{site.baseurl}}{{p.url}}"{%if p.url == page.url%} class="active"{%endif%}>{{ p.title }}</a>
    {% if nextLevel <= maxLevel %}
      {% include show-children.html dir=p.dir docs=allDocs level=nextLevel maxLevel=maxLevel %}
    {% endif %}
    </li>
{% endfor %}
</ul>

{% comment %}+++++++++++++++++++++++++++++++++++++++++++++++++
Because all variables are globales (all includes have the same scope)
we restore level and nextLevel variables to parent values
+++++++++++++++++++++++++++++++++++++++++++++++++{% endcomment %}
{% assign level = level | minus : 1 %}
{% assign nextLevel = nextLevel | minus : 1 %}

Use

可以使用多个参数调用此包含:

dir:要探索的根目录(即:/documentation)

docs:页面数组 - 默认为 site.pages

level:我们开始打印的级别(/documentation 位于级别 1, /documentation/chapter-1 位于级别 2,依此类推) 默认为“dir”级别

maxLevel:在哪里停止打印 - 默认为 100

Extracting documentation pages
{% assign documents = site.pages | where: "isDoc", true | sort: "weight" %}
{% assign dir = "documentation" %}

This will print all documentation hierachy
{% include show-children.html dir=dir docs=documents %}

This will start printing at level 2
{% include show-children.html dir=dir docs=documents level=2 %}

This stop printing at level 2
{% include show-children.html dir=dir docs=documents maxLevel=2 %}

在页面布局上,如果您只想打印子页面,您可以执行以下操作:

{% assign documents = site.pages | where: "isDoc", true | sort: "weight" %}
{% assign level = page.dir | remove_first: "/" | split:"/" | size %}
{% assign childrenLevel = level | plus : 1 %}
{% include show-children.html docs=documents dir=page.dir level=childrenLevel %}

面包屑

_includes/breadcrumb.html

{% assign minLevel = include.minLevel %}
{% if minLevel == nil %}{% assign minLevel = 1 %}{% endif %}

<div class="breadcrumb">
<p>You are here : </p>
{% assign documents = site.pages | where: "isDoc", true | sort: "weight" %}
{% include get-parents.html page=page minLevel=minLevel docs=documents %}
<p>{{ page.title }}</p>
</div>

<style type="text/css">
.breadcrumb p { display: inline; }
.breadcrumb p+p+p:before { content:"» "; }
</style>

_includes/get-parents.html

{% assign currentPage = include.page %}
{% assign minLevel    = include.minLevel %}
{% assign allDocs     = include.docs %}
{% assign pageLevel   = currentPage.dir | remove_first: "/" | split:"/" | size %}
{% assign parentLevel = pageLevel | minus: 1 %}
{% if parentLevel >= minLevel %}
    {% for p in allDocs %}
        {% assign pPageLevel = p.dir | remove_first: "/" | split:"/" | size %}
        {% if pPageLevel == parentLevel and currentPage.dir contains p.dir %}
            {% include get-parents.html page=p minLevel=minLevel docs=allDocs %}
            <p><a href="{{site.baseurl}}{{p.url}}">{{ p.title }}</a></p>
        {% endif %}
    {% endfor %}
{% endif %}

Use

Print Documentation > chapter 1 > part 1

{% include breadcrumb.html %}

Print Chapter 1 > part 1

{% include breadcrumb.html minLevel=2 %}

能更简单一点吗?

工作代码可以在这里找到.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Jekyll 链接集合中的文档? 的相关文章

随机推荐