【问题标题】:How may I manually sort Jekyll pages using yaml?如何使用 yaml 手动对 Jekyll 页面进行排序?
【发布时间】:2015-11-09 16:30:19
【问题描述】:

我正在将一个旧网站从 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

这可能吗?查看Jekyll site,他们似乎有一个手动订购的网站,但我不太清楚如何。

此外,为了使问题更加复杂,我希望将其分配给pages,以便page.nextbasics 类别的最后一页之后自动移动到advanced 类别。

【问题讨论】:

    标签: jekyll


    【解决方案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 %}
    

    【讨论】:

    • 这是一个非常好的、详细的答案。非常感谢。
    猜你喜欢
    • 2017-06-14
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 2018-12-25
    • 2011-11-08
    • 2019-12-19
    • 1970-01-01
    相关资源
    最近更新 更多