From 718ceecfa2dc6ea97bcad25828f8b88e5cbda454 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 19 Nov 2025 17:06:49 -0800 Subject: [PATCH] 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 81ec2c92..132da23c 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 8583b5fa..b64d1174 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']"