Skip to content
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 @@ -16,6 +16,10 @@ See the [Contributing Guide](contributing.md) for details.

### Fixed

* The `toc` extension no longer raises `TypeError` for a heading whose
`data-toc-label` attribute is present but valueless, as the `attr_list`
extension's bare-attribute syntax (`{: data-toc-label }`) writes it: like a
heading with no `data-toc-label`, its text is used as the TOC name (#1712).
* The `toc` extension no longer raises `TypeError` for a heading whose `id`
attribute is present but valueless, as the `attr_list` extension's
bare-attribute syntax (`{id}`) writes it: like a heading with no `id` at
Expand Down
11 changes: 7 additions & 4 deletions markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,14 @@ def run(self, doc: etree.Element) -> None:

data_toc_label = ''
if 'data-toc-label' in el.attrib:
data_toc_label = run_postprocessors(unescape(el.attrib['data-toc-label']), self.md)
# Overwrite name with sanitized value of `data-toc-label`.
name = escape_cdata(strip_tags(data_toc_label))
# Remove the data-toc-label attribute as it is no longer needed
del el.attrib['data-toc-label']
label = el.attrib.pop('data-toc-label')
# The `attr_list` extension writes bare attributes with a value of
# None. A valueless label is treated like a missing one.
if label is not None:
data_toc_label = run_postprocessors(unescape(label), self.md)
# Overwrite name with sanitized value of `data-toc-label`.
name = escape_cdata(strip_tags(data_toc_label))

if int(el.tag[-1]) >= self.toc_top and int(el.tag[-1]) <= self.toc_bottom:
toc_tokens.append({
Expand Down
37 changes: 36 additions & 1 deletion tests/test_syntax/extensions/test_toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@ def testDontStripHTMLEntities(self):


class TestTOCWithAttrList(TestCase):
"""The `toc` extension together with `attr_list` (#1705)."""
"""The `toc` extension together with `attr_list` (#1705, #1712)."""

maxDiff = None
default_kwargs = {
Expand Down Expand Up @@ -1611,3 +1611,38 @@ def testBareIdAttributeWithTOCMarker(self):
'</div>\n'
'<h1 id="heading-one">Heading One</h1>'
)

def testBareDataTocLabelAttribute(self):
# A valueless `data-toc-label` is treated like a missing one: the
# heading text is used as the TOC name and the attribute is removed.
self.assertMarkdownRenders(
'# Hello {: data-toc-label }',
'<h1 id="hello">Hello</h1>',
expected_attrs={
'toc_tokens': [{
'level': 1,
'id': 'hello',
'name': 'Hello',
'html': 'Hello',
'data-toc-label': '',
'children': []
}]
}
)

def testBareDataTocLabelAttributeWithTOCMarker(self):
self.assertMarkdownRenders(
self.dedent(
'''
[TOC]

# Heading One {: data-toc-label }
'''
),
'<div class="toc">\n'
'<ul>\n' # noqa
'<li><a href="#heading-one">Heading One</a></li>\n' # noqa
'</ul>\n' # noqa
'</div>\n'
'<h1 id="heading-one">Heading One</h1>'
)
Loading