Skip to content

Commit

Permalink
Avoid Liquid failure with empty collections (mmistakes#1092)
Browse files Browse the repository at this point in the history
Avoid Liquid failure when no pages with titles

Fix issue mmistakes#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.
  • Loading branch information
pdmosses authored Dec 27, 2022
1 parent 0279757 commit 0bfa011
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions _includes/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,23 @@
pages.
{%- endcomment -%}

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

{%- assign nav_order_pages = title_pages
| where_exp: "item", "item.nav_order != nil" -%}
Expand Down

0 comments on commit 0bfa011

Please sign in to comment.