Skip to content

Fix header-ids extra generating duplicate ids (#661)#713

Open
apoorvdarshan wants to merge 1 commit into
trentm:masterfrom
apoorvdarshan:fix/header-ids-duplicate-661
Open

Fix header-ids extra generating duplicate ids (#661)#713
apoorvdarshan wants to merge 1 commit into
trentm:masterfrom
apoorvdarshan:fix/header-ids-duplicate-661

Conversation

@apoorvdarshan

Copy link
Copy Markdown

Summary

The header-ids extra can emit duplicate id attributes (#661).

De-duplication relied only on a per-base-slug counter. When that counter is appended to a base slug, the resulting id can collide with a differently-named header whose own slug happens to equal the suffixed value:

# Chapter
# Chapter
# Chapter 2

produced:

<h1 id="chapter">Chapter</h1>
<h1 id="chapter-2">Chapter</h1>
<h1 id="chapter-2">Chapter 2</h1>   <!-- duplicate of the line above -->

The second # Chapter becomes chapter-2, and # Chapter 2 also slugifies to the base chapter-2, which was emitted verbatim.

Fix

header_id_from_text now tracks the set of ids it has already emitted. After building the candidate id it keeps bumping the base-slug counter until the id is genuinely unique, so the third header becomes chapter-2-2:

<h1 id="chapter">Chapter</h1>
<h1 id="chapter-2">Chapter</h1>
<h1 id="chapter-2-2">Chapter 2</h1>

The change preserves the existing -N suffix convention (e.g. duplicate Intro -> intro-2, as covered by header_ids_5) and the reset-count behavior for persistent Markdown objects; the new seen-set is reset in lockstep with the existing counter.

Tests / Verification

  • Added a tm-cases regression test (header_ids_duplicate) using the exact repro from the issue. It fails on master (produced a duplicate chapter-2) and passes with this fix.
  • Full test suite run: no new failures. The 14 remaining failures are all pre-existing and tagged knownfailure (unrelated to this change).
  • Added a CHANGES.md entry under the unreleased section.

Disclosure: prepared with AI assistance; reviewed and verified locally.

The `header-ids` extra de-duplicated ids using only a per-base-slug
counter. Appending that counter to the base slug could produce an id
that collides with a differently-named header. For example, two
`# Chapter` headers yield `chapter` and `chapter-2`, but a following
`# Chapter 2` also slugifies to the base `chapter-2` and was emitted
verbatim, duplicating the id.

Track the set of ids already emitted and, after building the candidate
id, keep bumping the counter until the id is genuinely unique. So the
third header now becomes `chapter-2-2` instead of a duplicate
`chapter-2`.

Fixes trentm#661.
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.

1 participant