Skip to content

Commit b40a342

Browse files
committed
fix: hash in table of contents
Closes: #675
1 parent e09fb90 commit b40a342

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/markdown2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ def _h_tag_sub(self, match: re.Match[str]) -> str:
16551655
text = match.string[match.start(): match.end()]
16561656
h_level = int(match.group(1))
16571657
# extract id= attr from tag, trying to account for regex "misses"
1658-
id_attr = (re.match(r'.*?id=(\S+)?.*', match.group(2) or '') or '')
1658+
id_attr = (re.match(r'.*?id="(\S+)?".*', match.group(2) or '') or '')
16591659
if id_attr:
16601660
# if id attr exists, extract that
16611661
id_attr = id_attr.group(1) or ''

test/test_markdown2.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,24 @@ def test_toc_with_persistent_object(self):
313313
self.assertEqual(expected_toc_html, md.convert(html).toc_html)
314314
test_toc_with_persistent_object.tags = ["toc", "issue208"]
315315

316+
def test_toc_with_inline_code(self):
317+
"""
318+
Tests that the toc does not contain a hash entry when the header is inline code.
319+
"""
320+
md = markdown2.Markdown(extras={
321+
"header-ids": {"mixed": True,},
322+
"toc": {"depth": 1},
323+
})
324+
html = """
325+
# `bla`
326+
"""
327+
expected_toc_html = """<ul>
328+
<li><a href="#bla"><code>bla</code></a></li>
329+
</ul>
330+
"""
331+
self.assertEqual(expected_toc_html, md.convert(html).toc_html)
332+
test_toc_with_inline_code.tags = ["toc", "issue675"]
333+
316334

317335
class DocTestsTestCase(unittest.TestCase):
318336
def test_api(self):

0 commit comments

Comments
 (0)