Skip to content

Commit 118c109

Browse files
authored
Merge pull request #2899 from ehuss/filtered-headings
Filter mark tags from sidebar heading nav
2 parents f27ae21 + 1881351 commit 118c109

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

crates/mdbook-html/front-end/templates/toc.js.hbs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,16 @@ window.customElements.define('mdbook-sidebar-scrollbox', MDBookSidebarScrollbox)
327327
});
328328
}
329329
330+
// Takes the nodes from the given head and copies them over to the
331+
// destination, along with some filtering.
332+
function filterHeader(source, dest) {
333+
const clone = source.cloneNode(true);
334+
clone.querySelectorAll('mark').forEach(mark => {
335+
mark.replaceWith(...mark.childNodes);
336+
});
337+
dest.append(...clone.childNodes);
338+
}
339+
330340
// Scans page for headers and adds them to the sidebar.
331341
document.addEventListener('DOMContentLoaded', function() {
332342
const activeSection = document.querySelector('#mdbook-sidebar .active');
@@ -399,7 +409,7 @@ window.customElements.define('mdbook-sidebar-scrollbox', MDBookSidebarScrollbox)
399409
span.appendChild(a);
400410
a.href = '#' + header.id;
401411
a.classList.add('header-in-summary');
402-
a.innerHTML = header.children[0].innerHTML;
412+
filterHeader(header.children[0], a);
403413
a.addEventListener('click', headerThresholdClick);
404414
const nextHeader = headers[i + 1];
405415
if (nextHeader !== undefined) {

tests/gui/books/heading-nav/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
- [Headings with markup](markup.md)
88
- [Current scrolls to bottom](current-to-bottom.md)
99
- [Unusual heading levels](unusual-heading-levels.md)
10+
- [Filtered headings](filtered-headings.md)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Filtered headings
2+
3+
## Skateboard
4+
5+
Checking for search marking.

tests/gui/heading-nav-filter.goml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Tests for collapsed heading sidebar navigation.
2+
3+
set-window-size: (1400, 800)
4+
go-to: |DOC_PATH| + "heading-nav/filtered-headings.html?highlight=skateboard#skateboard"
5+
6+
assert-property: ("//h2[@id='skateboard']", {"innerHTML": '<a class="header" href="#skateboard"><mark data-markjs="true">Skateboard</mark></a>'})
7+
8+
assert-property: ("//a[contains(@class, 'header-in-summary') and @href='#skateboard']", {"innerHTML": 'Skateboard'})

0 commit comments

Comments
 (0)