Skip to content

search: fix anchor ids for duplicate headers#1749

Merged
ehuss merged 1 commit into
rust-lang:masterfrom
tommilligan:unique-search-anchors
Mar 28, 2022
Merged

search: fix anchor ids for duplicate headers#1749
ehuss merged 1 commit into
rust-lang:masterfrom
tommilligan:unique-search-anchors

Conversation

@tommilligan
Copy link
Copy Markdown
Contributor

Closes: #720, #1579

Currently, if one page contains two sections with the same header, the generated anchor IDs will be uniquely enumerated:

<h3 id="my-title">
  <a class="header" href="#my-title">My Title</a>
</h3>
...
<h3 id="my-title-1">
  <a class="header" href="#my-title-1">My Title</a>
</h3>

However, search doesn't match the way it generates ids from header content, and doesn't enumerate them. This leads to all search results for a given section header, linking to the first section header with that content on the page.


This PR factors out unique id generation for headers to a new function, unique_id_from_content. The existing function id_from_content is publicly exported and so cannot be deleted - it has been marked as deprecated from the next patch version 0.4.16.

Matching tests have been added, and I have verified that this works for my specific use case locally.

Let me know if there's anything that needs changing - thanks very much for maintaining this project!

@tommilligan tommilligan changed the title search: match all generated anchor ids search: fix anchor ids for duplicate headers Feb 18, 2022
Copy link
Copy Markdown
Contributor

@ehuss ehuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thanks!

@ehuss ehuss merged commit a5fddfa into rust-lang:master Mar 28, 2022
ehuss added a commit to ehuss/mdBook that referenced this pull request Aug 13, 2025
This follows through with the deprecation of `id_from_content` which was
deprecated in rust-lang#1749.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Search does not handle headers with special formatting.

2 participants