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
3 changes: 2 additions & 1 deletion markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ def add_anchor(self, c, elem_id): # @ReservedAssignment
c.text = ""
for elem in c:
anchor.append(elem)
c.remove(elem)
while c:
c.remove(c[0])
c.append(anchor)

def add_permalink(self, c, elem_id):
Expand Down
72 changes: 72 additions & 0 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,78 @@ def testAnchorLink(self):
'<h2 id="header-2"><a class="toclink" href="#header-2">Header <em>2</em></a></h2>'
)

def testAnchorLinkWithSingleInlineCode(self):
""" Test TOC Anchorlink with single inline code. """
md = markdown.Markdown(
extensions=[markdown.extensions.toc.TocExtension(anchorlink=True)]
)
text = '# This is `code`.'
self.assertEqual(
md.convert(text),
'<h1 id="this-is-code">' # noqa
'<a class="toclink" href="#this-is-code">' # noqa
'This is <code>code</code>.' # noqa
'</a>' # noqa
'</h1>' # noqa
)

def testAnchorLinkWithDoubleInlineCode(self):
""" Test TOC Anchorlink with double inline code. """
md = markdown.Markdown(
extensions=[markdown.extensions.toc.TocExtension(anchorlink=True)]
)
text = '# This is `code` and `this` too.'
self.assertEqual(
md.convert(text),
'<h1 id="this-is-code-and-this-too">' # noqa
'<a class="toclink" href="#this-is-code-and-this-too">' # noqa
'This is <code>code</code> and <code>this</code> too.' # noqa
'</a>' # noqa
'</h1>' # noqa
)

def testPermalink(self):
""" Test TOC Permalink. """
md = markdown.Markdown(
extensions=[markdown.extensions.toc.TocExtension(permalink=True)]
)
text = '# Header'
self.assertEqual(
md.convert(text),
'<h1 id="header">' # noqa
'Header' # noqa
'<a class="headerlink" href="#header" title="Permanent link">&para;</a>' # noqa
'</h1>' # noqa
)

def testPermalinkWithSingleInlineCode(self):
""" Test TOC Permalink with single inline code. """
md = markdown.Markdown(
extensions=[markdown.extensions.toc.TocExtension(permalink=True)]
)
text = '# This is `code`.'
self.assertEqual(
md.convert(text),
'<h1 id="this-is-code">' # noqa
'This is <code>code</code>.' # noqa
'<a class="headerlink" href="#this-is-code" title="Permanent link">&para;</a>' # noqa
'</h1>' # noqa
)

def testPermalinkWithDoubleInlineCode(self):
""" Test TOC Permalink with double inline code. """
md = markdown.Markdown(
extensions=[markdown.extensions.toc.TocExtension(permalink=True)]
)
text = '# This is `code` and `this` too.'
self.assertEqual(
md.convert(text),
'<h1 id="this-is-code-and-this-too">' # noqa
'This is <code>code</code> and <code>this</code> too.' # noqa
'<a class="headerlink" href="#this-is-code-and-this-too" title="Permanent link">&para;</a>' # noqa
'</h1>' # noqa
)

def testTitle(self):
""" Test TOC Title. """
md = markdown.Markdown(
Expand Down