Avoid Liquid failure with empty collections (#1092)

Avoid Liquid failure when no pages with titles

Fix issue #1085

The user's config specified collections (incorrectly). Trying to build the site resulted in Jekyll failing due to a Liquid error. The error report did not suggest the cause of the error.

Liquid fails with division by 0 when title_pages_size is 0. This fix guards that code by checking that title_pages is non-empty.

To test:

1.  Specify a Jekyll collection with no pages, and specify it as a JTD collection.
2. Build the site.
3. Check that the specified collection has no nav links to pages.
This commit is contained in:
Peter Mosses 2022-12-27 18:46:18 +01:00 committed by GitHub
parent 0279757ff4
commit 0bfa011b47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,21 +39,23 @@
pages. pages.
{%- endcomment -%} {%- endcomment -%}
{%- assign unsorted_pages = title_pages {%- unless title_pages == empty -%}
| where_exp: "item", "item.parent == nil" {%- assign unsorted_pages = title_pages
| where_exp: "item", "item.nav_exclude == true"-%} | where_exp: "item", "item.parent == nil"
{%- assign title_pages_size = title_pages.size -%} | where_exp: "item", "item.nav_exclude == true" -%}
{%- assign unsorted_pages_percent = unsorted_pages.size {%- assign title_pages_size = title_pages.size -%}
| times: 100 | divided_by: title_pages_size -%} {%- assign unsorted_pages_percent = unsorted_pages.size
{%- if unsorted_pages_percent > 50 -%} | times: 100 | divided_by: title_pages_size -%}
{%- assign sorted_pages = "" | split: "" -%} {%- if unsorted_pages_percent > 50 -%}
{%- for item in title_pages -%} {%- assign sorted_pages = "" | split: "" -%}
{%- if item.nav_exclude != true or item.parent -%} {%- for item in title_pages -%}
{%- assign sorted_pages = sorted_pages | push: item -%} {%- if item.nav_exclude != true or item.parent -%}
{%- endif -%} {%- assign sorted_pages = sorted_pages | push: item -%}
{%- endfor -%} {%- endif -%}
{%- assign title_pages = sorted_pages -%} {%- endfor -%}
{%- endif -%} {%- assign title_pages = sorted_pages -%}
{%- endif -%}
{%- endunless -%}
{%- assign nav_order_pages = title_pages {%- assign nav_order_pages = title_pages
| where_exp: "item", "item.nav_order != nil" -%} | where_exp: "item", "item.nav_order != nil" -%}