Skip to content

Commit

Permalink
Fix footnote labels appearing out of order
Browse files Browse the repository at this point in the history
  • Loading branch information
Crozzers committed Dec 7, 2024
1 parent c2b7427 commit dfc90c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 9 additions & 2 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ def _setup_extras(self):
# https://docs.python.org/3/whatsnew/3.7.html#summary-release-highlights
self.footnotes = OrderedDict()
self.footnote_ids = []
self._footnote_marker = _hash_text('<<footnote>>')
if "header-ids" in self.extras:
if not hasattr(self, '_count_from_header_id') or self.extras['header-ids'].get('reset-count', False):
self._count_from_header_id = defaultdict(int)
Expand Down Expand Up @@ -510,6 +511,12 @@ def convert(self, text: str) -> 'UnicodeWithAttrs':
text = self._run_block_gamut(text)

if "footnotes" in self.extras:
def footnote_sub(match):
normed_id = match.group(1)
self.footnote_ids.append(normed_id)
return str(len(self.footnote_ids))

text = re.sub(r'%s-(.*?)(?=</a></sup>)' % self._footnote_marker, footnote_sub, text)
text = self._add_footnotes(text)

text = self.postprocess(text)
Expand Down Expand Up @@ -1581,10 +1588,10 @@ def _do_links(self, text: str) -> str:
if "footnotes" in self.extras and link_text.startswith("^"):
normed_id = re.sub(r'\W', '-', link_text[1:])
if normed_id in self.footnotes:
self.footnote_ids.append(normed_id)
result = (
f'<sup class="footnote-ref" id="fnref-{normed_id}">'
f'<a href="#fn-{normed_id}">{len(self.footnote_ids)}</a></sup>'
# insert special footnote marker that's easy to find and match against later
f'<a href="#fn-{normed_id}">{self._footnote_marker}-{normed_id}</a></sup>'
)
text = text[:start_idx] + result + text[p+1:]
else:
Expand Down
6 changes: 3 additions & 3 deletions test/tm-cases/footnotes_order_of_appearance.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

<h2>title: testing the footnotes bug</h2>

<p>Lorem ipsum dolor sit amet. Aliqua cillum, eu velit.<sup class="footnote-ref" id="fnref-note1"><a href="#fn-note1">3</a></sup> Velit deserunt adipiscing adipiscing ullamco exercitation.</p>
<p>Lorem ipsum dolor sit amet. Aliqua cillum, eu velit.<sup class="footnote-ref" id="fnref-note1"><a href="#fn-note1">1</a></sup> Velit deserunt adipiscing adipiscing ullamco exercitation.</p>

<ul>
<li>this is a list item</li>
<li>this is a list item<sup class="footnote-ref" id="fnref-note2"><a href="#fn-note2">1</a></sup></li>
<li>this is a list item<sup class="footnote-ref" id="fnref-note3"><a href="#fn-note3">2</a></sup></li>
<li>this is a list item<sup class="footnote-ref" id="fnref-note2"><a href="#fn-note2">2</a></sup></li>
<li>this is a list item<sup class="footnote-ref" id="fnref-note3"><a href="#fn-note3">3</a></sup></li>
</ul>

<p>Lorem ipsum dolor sit amet. Adipiscing<sup class="footnote-ref" id="fnref-note4"><a href="#fn-note4">4</a></sup> adipiscing ullamco, exercitation sint. Exercitation sint, fugiat exercitation voluptate amet.</p>
Expand Down

0 comments on commit dfc90c6

Please sign in to comment.