mirror of
https://github.com/snachodog/just-the-docs.git
synced 2025-04-10 05:51:23 -06:00
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.)
97 lines
3.2 KiB
HTML
97 lines
3.2 KiB
HTML
{%- comment -%}
|
|
Include as: {%- include components/breadcrumbs.html -%}
|
|
Depends on: page, site.
|
|
Includes: components/site_nav.html.
|
|
Results in: HTML for the breadcrumbs component.
|
|
Overwrites:
|
|
nav_list_link, site_nav, nav_list_simple, nav_list_link_class, nav_category,
|
|
nav_anchor_splits, nav_breadcrumbs, nav_split, nav_split_next, nav_split_test,
|
|
nav_breadcrumb_link, nav_list_end_less, nav_list_end_count, nav_end_index, nav_breadcrumb.
|
|
{%- endcomment -%}
|
|
|
|
{%- if page.url != "/" and page.parent and page.title -%}
|
|
|
|
{%- capture site_nav -%}
|
|
{%- include_cached components/site_nav.html all=true -%}
|
|
{%- endcapture -%}
|
|
|
|
{%- capture nav_list_link -%}
|
|
<a href="{{ page.url | relative_url }}" class="nav-list-link">
|
|
{%- endcapture -%}
|
|
|
|
{%- capture nav_list_simple -%}
|
|
<ul class="nav-list">
|
|
{%- endcapture -%}
|
|
|
|
{%- capture nav_list_link_class %} class="nav-list-link">
|
|
{%- endcapture -%}
|
|
|
|
{%- capture nav_category -%}
|
|
<div class="nav-category">
|
|
{%- endcapture -%}
|
|
|
|
{%- assign nav_anchor_splits =
|
|
site_nav | split: nav_list_link |
|
|
first | split: nav_category |
|
|
last | split: "</a>" -%}
|
|
|
|
{%- comment -%}
|
|
The ordinary pages (if any) and the collections pages (if any) are separated by
|
|
occurrences of nav_category.
|
|
|
|
Any ancestor nav-links of the page are contained in the last group of pages,
|
|
immediately preceding nav-lists. After splitting at "</a>", the anchor that
|
|
was split is a potential ancestor link when the following split starts with
|
|
a nav-list.
|
|
|
|
The array nav_breadcrumbs is the stack of current potential ancestors of the
|
|
current page. A split that contains one or more "</ul>"s requires that number
|
|
of potential ancestors to be popped from the stack.
|
|
|
|
The number of occurrences of a string in nav_split_next is computed by removing
|
|
them all, then dividing the resulting size difference by the length of the string.
|
|
{%- endcomment %}
|
|
|
|
{%- assign nav_breadcrumbs = "" | split: "" -%}
|
|
|
|
{%- for nav_split in nav_anchor_splits -%}
|
|
{%- unless forloop.last -%}
|
|
|
|
{%- assign nav_split_next = nav_anchor_splits[forloop.index] | strip -%}
|
|
|
|
{%- assign nav_split_test =
|
|
nav_split_next | remove_first: nav_list_simple | prepend: nav_list_simple -%}
|
|
{%- if nav_split_test == nav_split_next -%}
|
|
{%- assign nav_breadcrumb_link =
|
|
nav_split | split: "<a " | last | prepend: "<a " |
|
|
replace: nav_list_link_class, ">" | append: "</a>" -%}
|
|
{%- assign nav_breadcrumbs = nav_breadcrumbs | push: nav_breadcrumb_link -%}
|
|
{%- endif -%}
|
|
|
|
{%- if nav_split_next contains "</ul>" -%}
|
|
{%- assign nav_list_end_less = nav_split_next | remove: "</ul>" -%}
|
|
{%- assign nav_list_end_count =
|
|
nav_split_next.size | minus: nav_list_end_less.size | divided_by: 5 -%}
|
|
{% for nav_end_index in (1..nav_list_end_count) %}
|
|
{%- assign nav_breadcrumbs = nav_breadcrumbs | pop -%}
|
|
{%- endfor -%}
|
|
{%- endif -%}
|
|
|
|
{%- endunless -%}
|
|
{%- endfor -%}
|
|
|
|
<nav aria-label="Breadcrumb" class="breadcrumb-nav">
|
|
<ol class="breadcrumb-nav-list">
|
|
{%- for nav_breadcrumb in nav_breadcrumbs %}
|
|
<li class="breadcrumb-nav-list-item">{{ nav_breadcrumb }}</li>
|
|
{%- endfor %}
|
|
<li class="breadcrumb-nav-list-item"><span>{{ page.title }}</span></li>
|
|
</ol>
|
|
</nav>
|
|
|
|
{% if site.nav_error_report %}
|
|
{{ nav_error_report }}
|
|
{% endif %}
|
|
|
|
{%- endif -%}
|