Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## python-markdown2 2.5.6 (not yet released)

- [pull #745] Fix `footnotes` extra listing notes in definition order, so a note's position in the list didn't match its number in the text (#178)
- [pull #739] Fix `header-ids` extra putting sanitized HTML into header ids in safe mode, which allowed attribute injection (#649)
- [pull #732] Fix excessive CPU use in the inline HTML tokenizer on repeated unclosed tag fragments (#707)
- [pull #736] Clarify how to enable file variables in the Python API and command-line help (#269, #365).
Expand Down
6 changes: 2 additions & 4 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2272,10 +2272,8 @@ def _add_footnotes(self, text: str) -> str:
if not self.footnote_return_symbol:
self.footnote_return_symbol = "↩"

# self.footnotes is generated in _strip_footnote_definitions, which runs re.sub on the whole
# text. This means that the dict keys are inserted in order of appearance. Use the dict to
# sort footnote ids by that same order
self.footnote_ids.sort(key=lambda a: list(self.footnotes.keys()).index(a))
# self.footnote_ids is filled by _do_footnote_marker in the order the references are
# numbered, so listing the notes in that order keeps each note at its number
for i, id in enumerate(self.footnote_ids):
if i != 0:
footer.append('')
Expand Down
22 changes: 22 additions & 0 deletions test/tm-cases/footnotes_defined_out_of_order.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<p>The first reference<sup class="footnote-ref" id="fnref-b"><a href="#fn-b">1</a></sup> and the second<sup class="footnote-ref" id="fnref-a"><a href="#fn-a">2</a></sup>.</p>

<blockquote>
<p>A quoted reference<sup class="footnote-ref" id="fnref-c"><a href="#fn-c">3</a></sup>.</p>
</blockquote>

<div class="footnotes">
<hr />
<ol>
<li id="fn-b">
<p>Defined third, referenced first.&#160;<a href="#fnref-b" class="footnoteBackLink" title="Jump back to footnote 1 in the text.">&#8617;</a></p>
</li>

<li id="fn-a">
<p>Defined first, referenced second.&#160;<a href="#fnref-a" class="footnoteBackLink" title="Jump back to footnote 2 in the text.">&#8617;</a></p>
</li>

<li id="fn-c">
<p>Defined second, referenced third.&#160;<a href="#fnref-c" class="footnoteBackLink" title="Jump back to footnote 3 in the text.">&#8617;</a></p>
</li>
</ol>
</div>
1 change: 1 addition & 0 deletions test/tm-cases/footnotes_defined_out_of_order.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extras": ["footnotes"]}
7 changes: 7 additions & 0 deletions test/tm-cases/footnotes_defined_out_of_order.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
The first reference[^b] and the second[^a].

> A quoted reference[^c].

[^a]: Defined first, referenced second.
[^c]: Defined second, referenced third.
[^b]: Defined third, referenced first.
8 changes: 4 additions & 4 deletions test/tm-cases/repeated_footnote_labels.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
<div class="footnotes">
<hr />
<ol>
<li id="fn-second">
<p>Second note referencing first<sup class="footnote-ref" id="fnref-first"><a href="#fn-first">1</a></sup>.&#160;<a href="#fnref-second" class="footnoteBackLink" title="Jump back to footnote 1 in the text.">&#8617;</a></p>
<li id="fn-first">
<p>First note.&#160;<a href="#fnref-first" class="footnoteBackLink" title="Jump back to footnote 1 in the text.">&#8617;</a></p>
</li>

<li id="fn-first">
<p>First note.&#160;<a href="#fnref-first" class="footnoteBackLink" title="Jump back to footnote 2 in the text.">&#8617;</a></p>
<li id="fn-second">
<p>Second note referencing first<sup class="footnote-ref" id="fnref-first"><a href="#fn-first">1</a></sup>.&#160;<a href="#fnref-second" class="footnoteBackLink" title="Jump back to footnote 2 in the text.">&#8617;</a></p>
</li>

<li id="fn-third">
Expand Down
Loading