{%- comment -%} getting `folders` variable an array of folder names that are unescaped (i.e. with spaces and symbols) if `folders` settings set in config, then it follows order in config else it is sorted in alphabetical order {%- endcomment -%} {% assign folders = "" | split: ',' %} {% assign escaped_folders = "" | split: ',' %} {%- comment -%} checking if `folders` set in config.yml, if yes: assign folders variable to that {%- endcomment -%} {% if site.folders and site.folders.size > 0 %} {% for folder in site.folders %} {% assign folder_name = folder.name | strip %} {% assign folders = folders | push: folder_name %} {% endfor %} {%- comment -%} if no: get all pages and extract folder names then deduplicate and sort {%- endcomment -%} {% else %} {% for page in site.html_pages %} {% if page.name == 'index.html' or page.name == 'index.md' %} {% continue %} {% endif %} {% comment %} page.path gives - folder name/hello.md - hello.md - assets/hello.md {% endcomment %} {% assign path_split = page.path | split: '/' %} {% if path_split.size == 1 %} {% assign folder = '/' %} {% else %} {% assign folder = path_split | first %} {% endif %} {% unless folder == 'assets' %} {% assign folders = folders | push: folder %} {% endunless %} {% endfor %} {% assign folders = folders | uniq | sort %} {% endif %} {%- comment -%} Assingn folders html to `folders_html` variable Loops through folders, escape them and set as href and id id is used to assess the corresponding toc {%- endcomment -%} {% capture folders_html %} {% for folder in folders %} {% assign escaped_folder_path = folder | uri_escape %} {% assign escaped_folders = escaped_folders | push: escaped_folder_path %} {% assign folder_href = escaped_folder_path %} {% unless escaped_folder_path == '/' %} {% assign folder_href = escaped_folder_path | prepend: '/' %} {% endunless %}