Skip to content

fix(html): avoid nested anchors in headings with links#3100

Open
leno23 wants to merge 3 commits into
rust-lang:masterfrom
leno23:fix/header-links-no-nested-anchors-221
Open

fix(html): avoid nested anchors in headings with links#3100
leno23 wants to merge 3 commits into
rust-lang:masterfrom
leno23:fix/header-links-no-nested-anchors-221

Conversation

@leno23
Copy link
Copy Markdown

@leno23 leno23 commented May 17, 2026

Summary

Fixes invalid nested <a> elements when headings (or definition terms) contain markdown links.

  • When a heading already contains an <a> from markdown, prepend an empty <a class="header"> as the first child so permalink and inline links do not nest.
  • Otherwise, keep the existing behavior of wrapping heading text in the header link (preserves search highlighting, GUI tests, and sidebar behavior).
  • Update toc.js so sidebar “on this page” navigation still shows full heading text when the empty header link is used.

Example (heading with link):

<!-- before (invalid) -->
<h2><a class="header" href="#foo">Title with <a href="...">link</a></a></h2>

<!-- after (valid) -->
<h2 id="foo"><a class="header" href="#foo"></a>Title with <a href="...">link</a></h2>

Fixes #221.

Test plan

  • Extended rendering/header_links with a heading containing a markdown link
  • Updated definition_lists snapshot for <dt> terms with links
  • cargo test -p mdbook --test testsuite (110 passed)
  • CI GUI tests pass

Insert an empty header anchor as the first child of each heading
instead of wrapping all heading content. This fixes invalid HTML when
headings contain markdown links or other inline anchors.

Fixes rust-lang#221.

Co-authored-by: Cursor <cursoragent@cursor.com>
@rustbot rustbot added the S-waiting-on-review Status: waiting on a review label May 17, 2026
leno23 and others added 2 commits May 17, 2026 10:54
Update toc.js to copy heading text from the full heading element when the
`.header` anchor is empty, fixing sidebar navigation after the nested-anchor
fix for issue rust-lang#221.

Co-authored-by: Cursor <cursoragent@cursor.com>
Use empty prepended header links only when a heading or definition term
already contains an `<a>` from markdown. Other headings keep wrapping
text in the header link so search highlighting and GUI tests behave as
before. Update snapshots for the linked-heading case (issue rust-lang#221).

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: waiting on a review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Headers containing links result in invalid HTML; nested a tags

2 participants