{%- comment -%}

ordered_child_list
  generates an html list of child pages, respecting the page.order value.
  only first-level children are listed, grandchildren and beyond are ignored.
  order: 0 is first, -1 is last, undefined orders go in the middle alphabetically

params:
  docs=<array-of-docs-to-order>
  page=<calling-page-object> (optional, will restrict list to children of given page)

{%- endcomment -%}
{% assign doc_list = include.docs %}
{% if include.page %}
  {% assign parent = include.page.path | split: '.' | first %}
  {% assign child_depth = include.page.path | split:'/' | size | minus:1 %}
{% endif %}

{% assign ordered = '' | split: '' %}
{% assign endered = '' | split: '' %}
{% assign unorder = '' | split: '' %}
{% for doc in doc_list %}
  {% if doc.order %}
    {% if doc.order < 0 %}{% assign endered = endered | push: doc %}
    {% else %}{% assign ordered = ordered | push: doc %}
    {% endif %}
  {% else %}{% assign unorder = unorder | push: doc %}
  {% endif %}
{% endfor %}
{% assign ordered = ordered | sort: 'order' %}
{% assign endered = endered | sort: 'order' %}
{% assign doc_list = ordered | concat: unorder | concat: endered %}

<div class="ui bulleted link list">
{% for doc in doc_list %}
  {% assign listable = true %}
  {% if include.page %}
    {% assign child = doc.path | split: '.' | first %}
    {% if parent == child %}{% continue %}{%endif%}
    {% assign prefix = child | truncate: parent.size, '' %}
    {% unless prefix == parent %}{% assign listable = false %}{% endunless %}
  {% endif %}
  {% if listable %}
    {% capture url %}{{ doc.url }}#/{{ doc.collection | downcase }}/{% endcapture %}
    <a class="item" href="{{ site.baseurl }}{{ url }}">{{ doc.title }}</a>
  {% endif %}
{% endfor %}
</div>
