Fix #2995: Skip duplicate first header in sidebar header navigation#3039
Fix #2995: Skip duplicate first header in sidebar header navigation#3039cobyfrombrooklyn-bot wants to merge 1 commit intorust-lang:masterfrom
Conversation
When a chapter's markdown file starts with an h2 or lower heading (e.g. `## Section Title`), the 'on this page' sidebar feature would add it as a sub-entry, duplicating the chapter's existing SUMMARY.md sidebar link. This skips the first heading in the on-this-page list when its text matches the active sidebar link, preventing the duplication. Fixes rust-lang#2995
54e40f4 to
257ea84
Compare
|
I'm not quite convinced this is the right solution. Chapters are intended to start with a level-1 heading, and the TOC is designed around that. |
Note that the actual issue was described in full detail in #2995 more than two months ago, and there has been no suggested solution or workaround. |
|
I would suggest structuring your chapters so that each one starts with a level-1 heading if that heading is the name of the chapter. |
Thanks for you reply. If I remember the actual issue, reported more than two months ago, correctly, the problem is not with chapter headings, but with the sections. For typically (english) books, chapters are typically divided by sections, where each section can be divided again into subsections. A chapter would start with a level 1 heading introduced with a single "#", and sections would start with a "##" level 2 heading. This is as it is done in LaTex and other tools like AsciiDoctor for decades, and that was working for MDBook before version 0.5. But now for version 0.5 we get duplicate TOC entries for the section headings, see |
|
I had some hope that the authors of the official Rust book found some hack to suppress this issue, so I just tested it locally You can find the books source at https://github.com/rust-lang/book Build with Actually, this result was expected, as they use correct section headings, as in https://github.com/rust-lang/book/tree/main/src And as you can see in the appended screenshot, it is not an Firefox issue -- Google Chromium gives the same issue! |

Problem
When a chapter's markdown file starts with a heading that matches the chapter title from SUMMARY.md, the sidebar header navigation (introduced in v0.5.0) shows a duplicate entry. For example:
The sidebar would show:
This is a common pattern when authors use proper heading hierarchy (
#for chapter title,##for sections), as the reporter describes.Fix
Added deduplication logic in the sidebar header navigation JS (
toc.js.hbs). After collecting page headers, it compares the first header's text against the active sidebar entry. If they match (after normalizing whitespace and stripping the section number prefix like "1.1."), the first header is skipped.The comparison handles:
Test
Added
duplicate_toc_header_skippedintegration test with a minimal book that reproduces the issue (chapter title matching the first h2). Verifies the deduplication logic is present in the generated JS. Fails without the fix, passes with it.Full test suite passes on macOS ARM (Apple Silicon): 270 tests, 0 failures.