Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/mdbook-html/front-end/templates/toc.js.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,16 @@ window.customElements.define('mdbook-sidebar-scrollbox', MDBookSidebarScrollbox)
headers = Array.from(main.querySelectorAll('h2, h3, h4, h5, h6'))
.filter(h => h.id !== '' && h.children.length && h.children[0].tagName === 'A');
// Skip the first heading if its text matches the active sidebar link.
// This avoids duplicating the chapter title in the "on this page" list
// when a chapter's markdown starts with a heading (e.g. `## Section Title`)
// that matches its SUMMARY.md entry.
// See https://github.com/rust-lang/mdBook/issues/2995
if (headers.length > 0
&& activeSection.textContent.trim() === headers[0].textContent.trim()) {
headers.shift();
}
if (headers.length === 0) {
return;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/testsuite/toc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ fn check_link_target_fallback() {
);
}

/// The "on this page" feature should skip the first heading if it matches
/// the active sidebar link text, to avoid duplicating the chapter title
/// when a chapter starts with an h2+ heading.
/// See <https://github.com/rust-lang/mdBook/issues/2995>.
#[test]
fn toc_js_deduplicates_first_heading() {
BookTest::from_dir("toc/basic_toc")
.check_file_contains("book/toc*.js", "activeSection.textContent.trim() === headers[0].textContent.trim()")
.check_file_contains("book/toc*.js", "headers.shift()");
}

// Checks formatting of summary names with inline elements.
#[test]
fn summary_with_markdown_formatting() {
Expand Down
Loading