如何使用yaml手动对Jekyll页面进行排序?

时间:2015-11-09 16:30:19

标签: jekyll

我正在将旧网站从PHP CMS转换为Jekyll,我想保留手动排序的页面。最初,我以为我可以重命名文件以包含索引,例如01-start-here.markdown02-learn-more.markdown等。如果我进一步添加了一个页面,这会让事情变得尴尬。

理想情况下,我想指定一个包含页面顺序的yaml文件。例如:

category: basics
pages:
  - start here
  - learn more
  - overview

category: advanced
pages:
  - diving further
  - wrapping up

这可能吗?看看description他们似乎有一个手动订购的网站,但我无法弄清楚如何。

此外,为了进一步加剧问题,我希望将其分配到pages,以便page.next在{{1}的最后一页之后自动移至advanced类别类别。

1 个答案:

答案 0 :(得分:1)

来自Jekyll文档代码

<强> _data / docs.yml

- title: chapter 1
  docs:
  - home
  - page
  - other

- title: Other chapter
  docs:
  - toto
  - etc

在Jekyll中,文档包含在 _docs 集合中,但这可以很容易地适用于页面。

_includes / docs_contents.html

中导航
<div class="unit one-fifth hide-on-mobiles">
  <aside>
    {% for section in site.data.docs %}
    <h4>{{ section.title }}</h4>
    {% include docs_ul.html items=section.docs %}
    {% endfor %}
  </aside>
</div>

<强> _includes / docs_ul.html

{% assign items = include.items %}
<ul>
{% for item in items %}
  {% assign item_url = item | prepend:"/docs/" | append:"/" %}
  {% if item_url == page.url %}
    {% assign c = "current" %}
  {% else %}
    {% assign c = "" %}
  {% endif %}
  {% for p in site.docs %}
    {% if p.url == item_url %}
      <li class="{{ c }}"><a href="{{ site.url }}{{ p.url }}">{{ p.title }}</a></li>
      {% break %}
    {% endif %}
  {% endfor %}
{% endfor %}
</ul>

页脚上一页/下一页导航位于 _includes / section_nav.html

{% comment %}
Map grabs the doc sections, giving us an array of arrays. Join, flattens all
the items to a comma delimited string. Split turns it into an array again.
{% endcomment %}
{% assign docs = site.data.docs | map: 'docs' | join: ',' | split: ',' %}

{% comment %}
Because this is built for every page, lets find where we are in the ordered
document list by comparing url strings. Then if there's something previous or
next, lets build a link to it.
{% endcomment %}

{% for document in docs %}
  {% assign document_url = document | prepend:"/docs/" | append:"/" %}
  {% if document_url == page.url %}
    <div class="section-nav">
      <div class="left align-right">
          {% if forloop.first %}
            <span class="prev disabled">Back</span>
          {% else %}
            {% assign previous = forloop.index0 | minus: 1 %}
            {% assign previous_page = docs[previous] | prepend:"/docs/" | append:"/" %}
            <a href="{{ previous_page }}" class="prev">Back</a>
          {% endif %}
      </div>
      <div class="right align-left">
          {% if forloop.last %}
            <span class="next disabled">Next</span>
          {% else %}
            {% assign next = forloop.index0 | plus: 1 %}
            {% assign next_page = docs[next] | prepend:"/docs/" | append:"/" %}
            <a href="{{ next_page }}" class="next">Next</a>
          {% endif %}
      </div>
    </div>
    <div class="clear"></div>
    {% break %}
  {% endif %}
{% endfor %}