fix: decode HTML entities before slugifying header IDs#711
Open
gaoflow wants to merge 1 commit into
Open
Conversation
When a markdown header contains HTML entities (e.g. `# <othertext`), `header_id_from_text` passed the raw entity string to `_slugify` before HTML-decoding it. The `&` and `;` were stripped as non-word characters but the entity name letters (e.g. `lt`) were kept, silently corrupting the generated ID (`ltothertext` instead of `othertext`). Fix: call `html.unescape()` on the header text before slugifying so that entity characters are resolved to their actual Unicode code points first, then stripped (or kept) by the slug logic as any other character would be. Closes trentm#649
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
Fixes #649.
When a markdown header contains HTML entities such as
<,&, or>, theheader_id_from_textmethod passes the raw entity string to_slugifybefore HTML-decoding it.The
∧characters are stripped by_slugifyas non-word characters, but the entity name letters (e.g.lt) survive and pollute the generated ID:Fix
Call
html.unescape()on the header text before slugifying, so entity characters are decoded to their actual Unicode code points first. The resulting characters are then stripped or kept by_slugifyas any literal character would be:This is a one-line fix plus a new test case (
test/tm-cases/header_ids_entity.*).Tested
# <othertext→id="othertext"✓ (wasid="ltothertext")# R&D→id="rd"✓ (wasid="rampd")# <sometext>→id="sometext"✓ (unchanged, still correct)header_ids_*test cases pass.This pull request was prepared with the assistance of AI, under my direction and review.