Fix header-ids extra generating duplicate ids (#661)#713
Open
apoorvdarshan wants to merge 1 commit into
Open
Conversation
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.
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.
Summary
The
header-idsextra can emit duplicateidattributes (#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:
produced:
The second
# Chapterbecomeschapter-2, and# Chapter 2also slugifies to the basechapter-2, which was emitted verbatim.Fix
header_id_from_textnow 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 becomeschapter-2-2:The change preserves the existing
-Nsuffix convention (e.g. duplicateIntro->intro-2, as covered byheader_ids_5) and thereset-countbehavior for persistentMarkdownobjects; the new seen-set is reset in lockstep with the existing counter.Tests / Verification
tm-casesregression test (header_ids_duplicate) using the exact repro from the issue. It fails onmaster(produced a duplicatechapter-2) and passes with this fix.knownfailure(unrelated to this change).CHANGES.mdentry under the unreleased section.Disclosure: prepared with AI assistance; reviewed and verified locally.