diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index b3cf898f4..56db33c50 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -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): diff --git a/tests/test_extensions.py b/tests/test_extensions.py index 6d47e0ebd..19a138974 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -775,6 +775,78 @@ def testAnchorLink(self): '

Header 2

' ) + 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), + '

' # noqa + '' # noqa + 'This is code.' # noqa + '' # noqa + '

' # 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), + '

' # noqa + '' # noqa + 'This is code and this too.' # noqa + '' # noqa + '

' # noqa + ) + + def testPermalink(self): + """ Test TOC Permalink. """ + md = markdown.Markdown( + extensions=[markdown.extensions.toc.TocExtension(permalink=True)] + ) + text = '# Header' + self.assertEqual( + md.convert(text), + '

' # noqa + 'Header' # noqa + '' # noqa + '

' # 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), + '

' # noqa + 'This is code.' # noqa + '' # noqa + '

' # 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), + '

' # noqa + 'This is code and this too.' # noqa + '' # noqa + '

' # noqa + ) + def testTitle(self): """ Test TOC Title. """ md = markdown.Markdown(