Skip to content

Commit

Permalink
Merge pull request #612 from Crozzers/fix-footnote-labels
Browse files Browse the repository at this point in the history
Fix footnote labels appearing out-of-order (#536)
  • Loading branch information
nicholasserra authored Dec 7, 2024
2 parents c2b7427 + 63b7d2a commit 1032d35
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [pull #605] Add support for Python 3.13, drop EOL 3.8
- [pull #607] Fix `middle-word-em` extra preventing strongs from being recognized (#606)
- [pull #609] Add option to output to file in CLI (#608)
- [pull #612] Fix footnote labels appearing out-of-order (#536)


## python-markdown2 2.5.1
Expand Down
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 1032d35

Please sign in to comment.