Skip to content

Commit

Permalink
Don't manually sort TOC when mixed headers disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Crozzers committed Nov 26, 2023
1 parent 55afcb4 commit 53b3ccb
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,15 +431,17 @@ def convert(self, text):
text = self._a_nofollow_or_blank_links.sub(r'<\1 rel="nofollow"\2', text)

if "toc" in self.extras and self._toc:
def toc_sort(entry):
'''Sort the TOC by order of appearance in text'''
return re.search(
# header tag, any attrs, the ID, any attrs, the text, close tag
r'^<(h%d).*?id=(["\'])%s\2.*>%s</\1>$' % (entry[0], entry[1], re.escape(entry[2])),
text, re.M
).start()

self._toc.sort(key=toc_sort)
if self.extras['header-ids'].get('mixed'):
# TOC will only be out of order if mixed headers is enabled
def toc_sort(entry):
'''Sort the TOC by order of appearance in text'''
return re.search(
# header tag, any attrs, the ID, any attrs, the text, close tag
r'^<(h%d).*?id=(["\'])%s\2.*>%s</\1>$' % (entry[0], entry[1], re.escape(entry[2])),
text, re.M
).start()

self._toc.sort(key=toc_sort)
self._toc_html = calculate_toc_html(self._toc)

# Prepend toc html to output
Expand Down

0 comments on commit 53b3ccb

Please sign in to comment.