From a6944683e690ff5e1121b031beb6b3ace18da592 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 21 Oct 2025 16:35:44 -0700 Subject: [PATCH 1/2] Add a test that shows heading nav conflict with search mark The search marker is getting copied into the sidebar, but it cannot be dismissed. --- tests/gui/books/heading-nav/src/SUMMARY.md | 1 + tests/gui/books/heading-nav/src/filtered-headings.md | 5 +++++ tests/gui/heading-nav-filter.goml | 8 ++++++++ 3 files changed, 14 insertions(+) create mode 100644 tests/gui/books/heading-nav/src/filtered-headings.md create mode 100644 tests/gui/heading-nav-filter.goml diff --git a/tests/gui/books/heading-nav/src/SUMMARY.md b/tests/gui/books/heading-nav/src/SUMMARY.md index bf84824b70..41b3736fbb 100644 --- a/tests/gui/books/heading-nav/src/SUMMARY.md +++ b/tests/gui/books/heading-nav/src/SUMMARY.md @@ -7,3 +7,4 @@ - [Headings with markup](markup.md) - [Current scrolls to bottom](current-to-bottom.md) - [Unusual heading levels](unusual-heading-levels.md) +- [Filtered headings](filtered-headings.md) diff --git a/tests/gui/books/heading-nav/src/filtered-headings.md b/tests/gui/books/heading-nav/src/filtered-headings.md new file mode 100644 index 0000000000..0578c8a6be --- /dev/null +++ b/tests/gui/books/heading-nav/src/filtered-headings.md @@ -0,0 +1,5 @@ +# Filtered headings + +## Skateboard + +Checking for search marking. diff --git a/tests/gui/heading-nav-filter.goml b/tests/gui/heading-nav-filter.goml new file mode 100644 index 0000000000..7299833f43 --- /dev/null +++ b/tests/gui/heading-nav-filter.goml @@ -0,0 +1,8 @@ +// Tests for collapsed heading sidebar navigation. + +set-window-size: (1400, 800) +go-to: |DOC_PATH| + "heading-nav/filtered-headings.html?highlight=skateboard#skateboard" + +assert-property: ("//h2[@id='skateboard']", {"innerHTML": 'Skateboard'}) + +assert-property: ("//a[contains(@class, 'header-in-summary') and @href='#skateboard']", {"innerHTML": 'Skateboard'}) From 18813516e170abf19252dcaba6e00c0a77e54a73 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 21 Oct 2025 17:07:25 -0700 Subject: [PATCH 2/2] Fix avoiding the mark header in the sidebar nav This makes sure that the sidebar headings don't have the `` tag. When these are created, the Marker is unable to remove them from the sidebar (and we don't want them there in the first place). I suspect we'll want more filtering in the future, but I'm not sure exactly what to filter. Alternatively, it could have an allow list of tags, and filter all others out. --- crates/mdbook-html/front-end/templates/toc.js.hbs | 12 +++++++++++- tests/gui/heading-nav-filter.goml | 2 +- 2 files changed, 12 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 ec77e4c0ea..4d0306a65b 100644 --- a/crates/mdbook-html/front-end/templates/toc.js.hbs +++ b/crates/mdbook-html/front-end/templates/toc.js.hbs @@ -327,6 +327,16 @@ window.customElements.define('mdbook-sidebar-scrollbox', MDBookSidebarScrollbox) }); } + // Takes the nodes from the given head and copies them over to the + // destination, along with some filtering. + function filterHeader(source, dest) { + const clone = source.cloneNode(true); + clone.querySelectorAll('mark').forEach(mark => { + mark.replaceWith(...mark.childNodes); + }); + dest.append(...clone.childNodes); + } + // Scans page for headers and adds them to the sidebar. document.addEventListener('DOMContentLoaded', function() { const activeSection = document.querySelector('#mdbook-sidebar .active'); @@ -399,7 +409,7 @@ window.customElements.define('mdbook-sidebar-scrollbox', MDBookSidebarScrollbox) span.appendChild(a); a.href = '#' + header.id; a.classList.add('header-in-summary'); - a.innerHTML = header.children[0].innerHTML; + filterHeader(header.children[0], a); a.addEventListener('click', headerThresholdClick); const nextHeader = headers[i + 1]; if (nextHeader !== undefined) { diff --git a/tests/gui/heading-nav-filter.goml b/tests/gui/heading-nav-filter.goml index 7299833f43..a42892db54 100644 --- a/tests/gui/heading-nav-filter.goml +++ b/tests/gui/heading-nav-filter.goml @@ -5,4 +5,4 @@ go-to: |DOC_PATH| + "heading-nav/filtered-headings.html?highlight=skateboard#ska assert-property: ("//h2[@id='skateboard']", {"innerHTML": 'Skateboard'}) -assert-property: ("//a[contains(@class, 'header-in-summary') and @href='#skateboard']", {"innerHTML": 'Skateboard'}) +assert-property: ("//a[contains(@class, 'header-in-summary') and @href='#skateboard']", {"innerHTML": 'Skateboard'})