Skip to content

Commit 1032d35

Browse files
Merge pull request #612 from Crozzers/fix-footnote-labels
Fix footnote labels appearing out-of-order (#536)
2 parents c2b7427 + 63b7d2a commit 1032d35

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [pull #605] Add support for Python 3.13, drop EOL 3.8
66
- [pull #607] Fix `middle-word-em` extra preventing strongs from being recognized (#606)
77
- [pull #609] Add option to output to file in CLI (#608)
8+
- [pull #612] Fix footnote labels appearing out-of-order (#536)
89

910

1011
## python-markdown2 2.5.1

lib/markdown2.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ def _setup_extras(self):
405405
# https://docs.python.org/3/whatsnew/3.7.html#summary-release-highlights
406406
self.footnotes = OrderedDict()
407407
self.footnote_ids = []
408+
self._footnote_marker = _hash_text('<<footnote>>')
408409
if "header-ids" in self.extras:
409410
if not hasattr(self, '_count_from_header_id') or self.extras['header-ids'].get('reset-count', False):
410411
self._count_from_header_id = defaultdict(int)
@@ -510,6 +511,12 @@ def convert(self, text: str) -> 'UnicodeWithAttrs':
510511
text = self._run_block_gamut(text)
511512

512513
if "footnotes" in self.extras:
514+
def footnote_sub(match):
515+
normed_id = match.group(1)
516+
self.footnote_ids.append(normed_id)
517+
return str(len(self.footnote_ids))
518+
519+
text = re.sub(r'%s-(.*?)(?=</a></sup>)' % self._footnote_marker, footnote_sub, text)
513520
text = self._add_footnotes(text)
514521

515522
text = self.postprocess(text)
@@ -1581,10 +1588,10 @@ def _do_links(self, text: str) -> str:
15811588
if "footnotes" in self.extras and link_text.startswith("^"):
15821589
normed_id = re.sub(r'\W', '-', link_text[1:])
15831590
if normed_id in self.footnotes:
1584-
self.footnote_ids.append(normed_id)
15851591
result = (
15861592
f'<sup class="footnote-ref" id="fnref-{normed_id}">'
1587-
f'<a href="#fn-{normed_id}">{len(self.footnote_ids)}</a></sup>'
1593+
# insert special footnote marker that's easy to find and match against later
1594+
f'<a href="#fn-{normed_id}">{self._footnote_marker}-{normed_id}</a></sup>'
15881595
)
15891596
text = text[:start_idx] + result + text[p+1:]
15901597
else:

test/tm-cases/footnotes_order_of_appearance.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

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

5-
<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>
5+
<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>
66

77
<ul>
88
<li>this is a list item</li>
9-
<li>this is a list item<sup class="footnote-ref" id="fnref-note2"><a href="#fn-note2">1</a></sup></li>
10-
<li>this is a list item<sup class="footnote-ref" id="fnref-note3"><a href="#fn-note3">2</a></sup></li>
9+
<li>this is a list item<sup class="footnote-ref" id="fnref-note2"><a href="#fn-note2">2</a></sup></li>
10+
<li>this is a list item<sup class="footnote-ref" id="fnref-note3"><a href="#fn-note3">3</a></sup></li>
1111
</ul>
1212

1313
<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>

0 commit comments

Comments
 (0)