Skip to content
Open
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 `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
all, it now gets a generated slug (#1705).
* Fix for complex cases in new emphasis handling where text could be unexpectedly
dropped (#1694).
* Fix rule of 3 handling in new emphasis handling where an opener skipped by the rule could still be paired
Expand Down
5 changes: 4 additions & 1 deletion markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,10 @@ def run(self, doc: etree.Element) -> None:
name = strip_tags(innerhtml)

# Do not override pre-existing ids
if "id" not in el.attrib:
if not el.attrib.get("id"):
# The `attr_list` extension writes bare attributes with a
# value of None. A valueless id is no id at all, so a slug
# is generated for it like for any other id-less heading.
el.attrib["id"] = unique(self.slugify(html.unescape(name), self.sep), used_ids, id_counters)

data_toc_label = ''
Expand Down
37 changes: 37 additions & 0 deletions tests/test_syntax/extensions/test_toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from markdown.test_tools import TestCase
from markdown import Markdown
from markdown.extensions.toc import TocExtension, strip_tags, unique
from markdown.extensions.attr_list import AttrListExtension
from markdown.extensions.nl2br import Nl2BrExtension


Expand Down Expand Up @@ -1574,3 +1575,39 @@ def testDontStripHTMLEntities(self):
strip_tags('foo < & < bar'),
'foo < & < bar'
)


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

maxDiff = None
default_kwargs = {
'extensions': [TocExtension(), AttrListExtension()]
}

def testBareIdAttribute(self):
# The `attr_list` extension writes bare attributes with a value of
# None. A valueless `id` is no id at all, so `toc` generates a slug
# for it like for any other id-less heading instead of raising a
# `TypeError`.
self.assertMarkdownRenders(
'#d {id}',
'<h1 id="d">d</h1>'
)

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

# Heading One {id}
'''
),
'<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