Skip to content

Commit 7aace29

Browse files
authored
Backslash Unescape IDs set via attr_list on toc
Fixes #1493.
1 parent 0b5e80e commit 7aace29

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

docs/changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
* DRY fix in `abbr` extension by introducing method `create_element` (#1483).
1616

17+
### Fixed
18+
19+
* Backslash Unescape IDs set via `attr_list` on `toc` (#1493).
20+
1721
## [3.7] -- 2024-08-16
1822

1923
### Changed

markdown/extensions/toc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ def run(self, doc: etree.Element) -> None:
391391
if int(el.tag[-1]) >= self.toc_top and int(el.tag[-1]) <= self.toc_bottom:
392392
toc_tokens.append({
393393
'level': int(el.tag[-1]),
394-
'id': el.attrib["id"],
394+
'id': unescape(el.attrib["id"]),
395395
'name': name,
396396
'html': innerhtml,
397397
'data-toc-label': data_toc_label

tests/test_syntax/extensions/test_attr_list.py

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ def test_unclosed_quote_ignored(self):
7272
'<h1 foo="&quot;bar">Heading</h1>'
7373
)
7474

75+
def test_backslash_escape_value(self):
76+
self.assertMarkdownRenders(
77+
'# `*Foo*` { id="\\*Foo\\*" }',
78+
'<h1 id="*Foo*"><code>*Foo*</code></h1>'
79+
)
80+
7581
def test_table_td(self):
7682
self.assertMarkdownRenders(
7783
self.dedent(

tests/test_syntax/extensions/test_toc.py

+26
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,32 @@ def test_escaped_char_in_id(self):
974974
extensions=['toc']
975975
)
976976

977+
def test_escaped_char_in_attr_list(self):
978+
self.assertMarkdownRenders(
979+
r'# `*Foo*` { id="\*Foo\*" }',
980+
'<h1 id="*Foo*"><code>*Foo*</code></h1>',
981+
expected_attrs={
982+
'toc': (
983+
'<div class="toc">\n'
984+
'<ul>\n' # noqa
985+
'<li><a href="#*Foo*">*Foo*</a></li>\n' # noqa
986+
'</ul>\n' # noqa
987+
'</div>\n' # noqa
988+
),
989+
'toc_tokens': [
990+
{
991+
'level': 1,
992+
'id': '*Foo*',
993+
'name': '*Foo*',
994+
'html': '<code>*Foo*</code>',
995+
'data-toc-label': '',
996+
'children': []
997+
}
998+
]
999+
},
1000+
extensions=['toc', 'attr_list']
1001+
)
1002+
9771003
def testAutoLinkEmail(self):
9781004
self.assertMarkdownRenders(
9791005

0 commit comments

Comments
 (0)