Create automatic file index

Personal GitHub site

Create automatic file index

How To

To automatically create a list of files, use the following snippet.

List all pages in the same directory

{% assign pages = site.pages | sort: 'title' %}

<ul>
   {% for item in pages %}
      {% if item.dir == page.dir and item.path != page.path %}
         {% assign html_item = item.name | replace: '.md', '.html' %}
         <li><a href="{{ html_item }}" > {{ item.title }} </a></li>
      {% endif %}
   {% endfor %}
</ul>

List all pages in a specific directory

{% assign pages = site.pages | sort: 'title' %}

{% assign target_dir = page.dir | append: 'path/to/target_dir/' %}
<ul>
    {% for item in pages %}
        {% if item.dir == target_dir %}
            {% assign html_item = item.name | replace: '.md', '.html' %}
           <li><a href="{{ target_dir | append: html_item }}" > {{ item.title }} </a></li>
        {% endif %}
   {% endfor %}
</ul>

References