Fall back to "section" id for punctuation-only headings#3092
Open
ChrisJr404 wants to merge 1 commit into
Open
Conversation
Headings like `## ::` produce an empty id and a useless `href="#"` because every character gets stripped by `id_from_content`. When the slug ends up empty, return `"section"` instead so that `unique_id` can hand out `section`, `section-1`, `section-2`, ... — matching what pandoc and kramdown do for the same case. Updates the existing fixture in tests/testsuite/rendering/header_links which previously documented the broken output. Closes rust-lang#3002.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3002.
A heading whose text is entirely stripped by
id_from_content(e.g.## ::,## !!!) currently renders as<h2 id=""><a class="header" href="#"></a>. The empty id can't be linked to and breaks the in-page anchor.When the generated slug is empty, fall back to
"section". The existingunique_idhelper then handles disambiguation, so two punctuation-only headings on the same page becomesectionandsection-1, matching what pandoc and kramdown do for the same case (thanks @stevecheckoway for pointing this out and providing the original sketch in the issue).Changes:
crates/mdbook-html/src/utils.rs: whenid_from_contentwould return an empty string, return"section"instead. Added a unit test that exercises the per-page counter end-to-end throughunique_id(two punctuation-only headings + a normal heading + a third punctuation-only heading producesection,section-1,real-heading,section-2).tests/testsuite/rendering/header_links/expected/header_links.html: the existing integration fixture already covers two empty-id headings (###and####) and previously documented the broken output (id="",id="-1"); updated to the newsection/section-1output.Tested locally:
cargo test --workspace— all green.cargo clippy --workspace --all-targets -- -D warnings— clean.cargo fmt --check— clean.