Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backslash Unescape IDs set via attr_list on toc #1494

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

### Fixed

* Backslash Unescape IDs set via `attr_list` on `toc` (#1493).

## [3.7] -- 2024-08-16

### Changed
Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def run(self, doc: etree.Element) -> None:
if int(el.tag[-1]) >= self.toc_top and int(el.tag[-1]) <= self.toc_bottom:
toc_tokens.append({
'level': int(el.tag[-1]),
'id': el.attrib["id"],
'id': unescape(el.attrib["id"]),
'name': name,
'html': innerhtml,
'data-toc-label': data_toc_label
Expand Down
6 changes: 6 additions & 0 deletions tests/test_syntax/extensions/test_attr_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ def test_unclosed_quote_ignored(self):
'<h1 foo="&quot;bar">Heading</h1>'
)

def test_backslash_escape_value(self):
self.assertMarkdownRenders(
'# `*Foo*` { id="\\*Foo\\*" }',
'<h1 id="*Foo*"><code>*Foo*</code></h1>'
)

def test_table_td(self):
self.assertMarkdownRenders(
self.dedent(
Expand Down
26 changes: 26 additions & 0 deletions tests/test_syntax/extensions/test_toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,32 @@ def test_escaped_char_in_id(self):
extensions=['toc']
)

def test_escaped_char_in_attr_list(self):
self.assertMarkdownRenders(
r'# `*Foo*` { id="\*Foo\*" }',
'<h1 id="*Foo*"><code>*Foo*</code></h1>',
expected_attrs={
'toc': (
'<div class="toc">\n'
'<ul>\n' # noqa
'<li><a href="#*Foo*">*Foo*</a></li>\n' # noqa
'</ul>\n' # noqa
'</div>\n' # noqa
),
'toc_tokens': [
{
'level': 1,
'id': '*Foo*',
'name': '*Foo*',
'html': '<code>*Foo*</code>',
'data-toc-label': '',
'children': []
}
]
},
extensions=['toc', 'attr_list']
)

def testAutoLinkEmail(self):
self.assertMarkdownRenders(
'## <foo@example.org>',
Expand Down
Loading