Skip to content

Commit 182b7f5

Browse files
Merge pull request #536 from Crozzers/footnote-order-of-appearance
Maintain order of appearance in footnotes
2 parents b15d37d + d83c2ae commit 182b7f5

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [pull #529] Add `breaks` extra with ability to hard break on backslashes (issue #525)
88
- [pull #532] Fix #493 persisting when `code-friendly` extra enabled
99
- [pull #535] Update `_slugify` to use utf-8 encoding (issue #534)
10+
- [pull #536] Maintain order of appearance in footnotes
1011

1112
## python-markdown2 2.4.10
1213

lib/markdown2.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
import logging
116116
import re
117117
import sys
118-
from collections import defaultdict
118+
from collections import defaultdict, OrderedDict
119119
from hashlib import sha256
120120
from random import randint, random
121121

@@ -280,7 +280,9 @@ def reset(self):
280280

281281
def _setup_extras(self):
282282
if "footnotes" in self.extras:
283-
self.footnotes = {}
283+
# order of insertion matters for footnotes. Use ordered dict for Python < 3.7
284+
# https://docs.python.org/3/whatsnew/3.7.html#summary-release-highlights
285+
self.footnotes = OrderedDict()
284286
self.footnote_ids = []
285287
if "header-ids" in self.extras:
286288
self._count_from_header_id = defaultdict(int)
@@ -2508,6 +2510,10 @@ def _add_footnotes(self, text):
25082510
if not self.footnote_return_symbol:
25092511
self.footnote_return_symbol = "&#8617;"
25102512

2513+
# self.footnotes is generated in _strip_footnote_definitions, which runs re.sub on the whole
2514+
# text. This means that the dict keys are inserted in order of appearance. Use the dict to
2515+
# sort footnote ids by that same order
2516+
self.footnote_ids.sort(key=lambda a: list(self.footnotes.keys()).index(a))
25112517
for i, id in enumerate(self.footnote_ids):
25122518
if i != 0:
25132519
footer.append('')
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<hr />
2+
3+
<h2>title: testing the footnotes bug</h2>
4+
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>
6+
7+
<ul>
8+
<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>
11+
</ul>
12+
13+
<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>
14+
15+
<div class="footnotes">
16+
<hr />
17+
<ol>
18+
<li id="fn-note1">
19+
<p>this should be the first note&#160;<a href="#fnref-note1" class="footnoteBackLink" title="Jump back to footnote 1 in the text.">&#8617;</a></p>
20+
</li>
21+
22+
<li id="fn-note2">
23+
<p>this should be the second note&#160;<a href="#fnref-note2" class="footnoteBackLink" title="Jump back to footnote 2 in the text.">&#8617;</a></p>
24+
</li>
25+
26+
<li id="fn-note3">
27+
<p>this should be the third note&#160;<a href="#fnref-note3" class="footnoteBackLink" title="Jump back to footnote 3 in the text.">&#8617;</a></p>
28+
</li>
29+
30+
<li id="fn-note4">
31+
<p>this should be the fourth note&#160;<a href="#fnref-note4" class="footnoteBackLink" title="Jump back to footnote 4 in the text.">&#8617;</a></p>
32+
</li>
33+
</ol>
34+
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras": ["footnotes"]}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: testing the footnotes bug
3+
---
4+
Lorem ipsum dolor sit amet. Aliqua cillum, eu velit.[^note1] Velit deserunt adipiscing adipiscing ullamco exercitation.
5+
6+
- this is a list item
7+
- this is a list item[^note2]
8+
- this is a list item[^note3]
9+
10+
Lorem ipsum dolor sit amet. Adipiscing[^note4] adipiscing ullamco, exercitation sint. Exercitation sint, fugiat exercitation voluptate amet.
11+
12+
[^note1]: this should be the first note
13+
[^note2]: this should be the second note
14+
[^note3]: this should be the third note
15+
[^note4]: this should be the fourth note

0 commit comments

Comments
 (0)