Skip to content

Commit 64948d2

Browse files
committed
fix: decode HTML entities before slugifying header IDs
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 #649
1 parent ee81a6b commit 64948d2

4 files changed

Lines changed: 13 additions & 1 deletion

File tree

lib/markdown2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
__author__ = "Trent Mick"
117117

118118
import argparse
119+
import html
119120
import logging
120121
import re
121122
import sys
@@ -1578,7 +1579,7 @@ def header_id_from_text(self,
15781579
None to not have an id attribute and to exclude this header from
15791580
the TOC (if the "toc" extra is specified).
15801581
"""
1581-
header_id = _slugify(text)
1582+
header_id = _slugify(html.unescape(text))
15821583
if prefix and isinstance(prefix, str):
15831584
header_id = prefix + '-' + header_id
15841585

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1 id="escaped-lt">&lt;escaped-lt</h1>
2+
3+
<h1 id="rd-notes">R&amp;D Notes</h1>
4+
5+
<h1 id="normal-header">Normal Header</h1>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras": ["header-ids"]}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# &lt;escaped-lt
2+
3+
# R&amp;D Notes
4+
5+
# Normal Header

0 commit comments

Comments
 (0)