From 09616e31afd103c3c53822c0ad80fe8f6ebbf5dd Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 19 Nov 2025 16:50:35 -0800 Subject: [PATCH 1/2] Add a test to validate the structure of the heading nav This checks the entire structure of the heading nav for this test. It currently is not handling nesting properly. Having a large inline string like this may not be the easiest to maintain or be able to see the structure. I don't see a way to format it in goml. --- tests/gui/heading-nav-collapsed.goml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/gui/heading-nav-collapsed.goml b/tests/gui/heading-nav-collapsed.goml index 003d7e68fc..8583b5fab8 100644 --- a/tests/gui/heading-nav-collapsed.goml +++ b/tests/gui/heading-nav-collapsed.goml @@ -10,6 +10,8 @@ assert-text: (".current-header", "Heading 1") assert-attribute: ("li:has(> span > a[href='#heading-12'])", {"class": "header-item"}) assert-attribute: ("li:has(> span > a[href='#heading-21'])", {"class": "header-item"}) +assert-property: ("div.on-this-page", {"innerHTML": '
  1. Heading 1
    1. Heading 1.1
    2. Heading 1.2
      1. Heading 1.2.1
      2. Heading 1.2.2
  2. Heading 1.3
  3. Heading 2
    1. Heading 2.1
      1. Heading 2.1.1
        1. Heading 2.1.1.1
          1. Heading 2.1.1.1.1
'}) + // Click 1.2, expands it. click: "a.header-in-summary[href='#heading-12']" assert-attribute: ("li:has(> span > a[href='#heading-12'])", {"class": "header-item expanded"}) From 718ceecfa2dc6ea97bcad25828f8b88e5cbda454 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 19 Nov 2025 17:06:49 -0800 Subject: [PATCH 2/2] Fix heading nav depth This fixes an issue where when a heading goes from a larger to a smaller level, it was going back too far so that subsequent headings would be at a lower level than they should have been. Fixes https://github.com/rust-lang/mdBook/issues/2944 --- crates/mdbook-html/front-end/templates/toc.js.hbs | 2 +- tests/gui/heading-nav-collapsed.goml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/mdbook-html/front-end/templates/toc.js.hbs b/crates/mdbook-html/front-end/templates/toc.js.hbs index 81ec2c9200..132da23cd3 100644 --- a/crates/mdbook-html/front-end/templates/toc.js.hbs +++ b/crates/mdbook-html/front-end/templates/toc.js.hbs @@ -392,7 +392,7 @@ window.customElements.define('mdbook-sidebar-scrollbox', MDBookSidebarScrollbox) stack.push({level: nextLevel, ol: ol}); } } else if (level < currentLevel) { - while (stack.length > 1 && stack[stack.length - 1].level >= level) { + while (stack.length > 1 && stack[stack.length - 1].level > level) { stack.pop(); } } diff --git a/tests/gui/heading-nav-collapsed.goml b/tests/gui/heading-nav-collapsed.goml index 8583b5fab8..b64d117461 100644 --- a/tests/gui/heading-nav-collapsed.goml +++ b/tests/gui/heading-nav-collapsed.goml @@ -10,7 +10,7 @@ assert-text: (".current-header", "Heading 1") assert-attribute: ("li:has(> span > a[href='#heading-12'])", {"class": "header-item"}) assert-attribute: ("li:has(> span > a[href='#heading-21'])", {"class": "header-item"}) -assert-property: ("div.on-this-page", {"innerHTML": '
  1. Heading 1
    1. Heading 1.1
    2. Heading 1.2
      1. Heading 1.2.1
      2. Heading 1.2.2
  2. Heading 1.3
  3. Heading 2
    1. Heading 2.1
      1. Heading 2.1.1
        1. Heading 2.1.1.1
          1. Heading 2.1.1.1.1
'}) +assert-property: ("div.on-this-page", {"innerHTML": '
  1. Heading 1
    1. Heading 1.1
    2. Heading 1.2
      1. Heading 1.2.1
      2. Heading 1.2.2
    3. Heading 1.3
  2. Heading 2
    1. Heading 2.1
      1. Heading 2.1.1
        1. Heading 2.1.1.1
          1. Heading 2.1.1.1.1
'}) // Click 1.2, expands it. click: "a.header-in-summary[href='#heading-12']"