Skip to content

Commit

Permalink
fix: fix render_toc_url method for rendering empty iterable, via #407
Browse files Browse the repository at this point in the history
  • Loading branch information
lepture committed Jan 28, 2025
1 parent 34f5a77 commit ac53009
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/mistune/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def render_toc_ul(toc: Iterable[Tuple[int, str, str]]) -> str:
if not toc:
return ''

s = '<ul>\n'
s = ''
levels: List[int] = []
for level, k, text in toc:
item = '<a href="#{}">{}</a>'.format(k, text)
Expand Down Expand Up @@ -121,4 +121,6 @@ def render_toc_ul(toc: Iterable[Tuple[int, str, str]]) -> str:
s += '</li>\n</ul>\n'
levels.pop()

return s + '</li>\n</ul>\n'
if not s:
return ''
return '<ul>\n' + s + '</li>\n</ul>\n'
3 changes: 3 additions & 0 deletions tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ def heading_id(token, i):
html = md('# h1')
self.assertEqual(html, '<h1 id="t-1">h1</h1>\n')

def test_render_empty_toc(self):
self.assertEqual(render_toc_ul([]), '')
self.assertEqual(render_toc_ul(filter(lambda _: False, [])), '')

TestTocHook.load_fixtures('hook_toc.txt')

0 comments on commit ac53009

Please sign in to comment.