just-the-docs/_includes/components/children_nav.html
Peter Mosses bce3c32f46
Fix: auto-generated child navigation (TOC) (#1590)
Fix #1587

As reported in issue #1587, the auto-generated child navigation (TOC) has a bug: it can be incorrectly omitted. This happens when the first page built is a parent page. The omission is caused by a side-effect of including the cached site-nav HTML: the code that generates the site-nav is executed the first time the cached HTML is included, and assignments in the executed code may overwrite the values of variables. 

@kevinlin1 suggested a simple and safe way to fix this bug: move the inclusion of the site-nav in `components/children_nav.html` so that it is executed before all local assignments. This PR implements that suggestion, and applies the same fix to two other files.

### Testing

A test for this bug has been added to the [_Just the Docs Tests_ repo](https://github.com/just-the-docs/just-the-docs-tests). The first page rendered when building the website is `About this site` in the TESTS collection, and it is now the `parent` of the page [`Test TOC`](https://just-the-docs.github.io/just-the-docs-tests/tests/about/test-toc/), which should be listed in its auto-generated child navigation. (The test page uses `nav_exclude: true` to avoid the link to it appearing in the main navigation, but that doesn't affect the test of this PR.)

The following steps check that the bug appears when building _Just the Docs Tests_ with v0.10.0 of the theme:

1. Clone the [Just the Docs Tests repo](https://github.com/just-the-docs/just-the-docs-tests).
2. Build and serve the website locally using:
    ```sh
    JTD_ORG=just-the-docs JTD_REF=v0.10.0 bundle install
    JTD_ORG=just-the-docs JTD_REF=v0.10.0 bundle exec jekyll serve
    ```
3. Check that the `Test TOC` page at `.../just-the-docs-tests/tests/about/test-toc/` is a child of the `About this site` page at `.../just-the-docs-tests/tests/about/`.
4. Check that no auto-generated child navigation appears on the latter page.

The following steps check that the bug does not appear when building _Just the Docs Tests_ with this PR branch:

5. Build and serve the website locally using:
    ```sh
    JTD_ORG=pdmosses JTD_REF=fix-toc bundle install
    JTD_ORG=pdmosses JTD_REF=fix-toc bundle exec jekyll serve
    ```
6. Check that the `Test TOC` page at `.../just-the-docs-tests/tests/about/test-toc/` is a child of the `About this site` page at `.../just-the-docs-tests/tests/about/`.
7. Check that an auto-generated child navigation with a link to the `Test TOC` page appears on the latter page.

(It seems unnecessary to check that the reported bug does not appear on other pages, since subsequent includes of the cached site-nav cannot assign to any variables.)
2025-01-05 23:41:16 -08:00

91 lines
2.9 KiB
HTML

{%- comment -%}
Include as: {%- include components/children_nav.html -%}
Depends on: page, site, nav_breadcrumbs.
Results in: HTML for the children-navigation component.
Includes: components/nav/sorted.html, toc_heading_custom.html.
Overwrites:
nav_ancestor_links, nav_top_node_titles, nav_child_candidates, nav_children,
nav_child, nav_child_ok, nav_child_ancestor, nav_sorted.
{%- endcomment -%}
{%- comment -%}
Whether a page has any children is checked efficiently by inspecting the cached
site_nav. If the page has no children, nav_children is set to an empty array;
otherwise nav_children is left unset. (The site_nav is rendered the first time
it is included, and that may overwrite various variables.)
{%- endcomment -%}
{%- if page.has_children == false -%}
{%- assign nav_children = "" | split: "" -%}
{%- else -%}
{%- capture site_nav -%}
{%- include_cached components/site_nav.html all=true -%}
{%- endcapture -%}
{%- assign nav_children = nil -%}
{%- capture nav_list_link -%}
<a href="{{ page.url | relative_url }}" class="nav-list-link">
{%- endcapture -%}
{%- capture nav_list_simple -%}
<ul class="nav-list">
{%- endcapture -%}
{%- assign nav_child_start = site_nav
| split: nav_list_link | last
| split: "</a>" | slice: 1 | first -%}
{%- assign nav_child_test = nav_child_start
| remove_first: nav_list_simple | prepend: nav_list_simple -%}
{%- if nav_child_start != nav_child_test -%}
{%- assign nav_children = "" | split: "" -%}
{%- endif -%}
{%- endif -%}
{%- unless nav_children -%}
{%- comment -%}
The layout is assumed to include components/breadcrumbs.html before this file,
otherwise it needs to be included here.
{%- endcomment -%}
{%- assign nav_ancestors = "" | split: "" -%}
{%- for nav_link in nav_breadcrumbs -%}
{%- assign nav_title = nav_link | split: ">" | slice: 1 | first | append: ">" | remove: "</a>" -%}
{%- assign nav_ancestors = nav_ancestors | push: nav_title -%}
{%- endfor -%}
{%- assign nav_parenthood = site[page.collection] | default: site.html_pages
| where_exp: "item", "item.title != nil" | group_by: "parent" -%}
{%- assign nav_top_nodes = nav_parenthood
| where_exp: "item", "item.name == ''" | map: "items" | first -%}
{% assign nav_top_node_titles = nav_top_nodes | map: "title" -%}
{%- include components/nav/children.html node=page ancestors=nav_ancestors all=true -%}
{%- endunless -%}
{%- if nav_children.size >= 1 -%}
{%- if page.child_nav_order == 'desc' or page.child_nav_order == 'reversed' -%}
{%- assign nav_children = nav_children | reverse -%}
{%- endif -%}
<hr>
{% include toc_heading_custom.html %}
<ul>
{% for nav_child in nav_children %}
<li>
<a href="{{ nav_child.url | relative_url }}">{{ nav_child.title }}</a>{% if nav_child.summary %} - {{ nav_child.summary }}{% endif %}
</li>
{% endfor %}
</ul>
{%- endif -%}