Skip to content

Fix footnotes listed out of order when definitions aren't in reference order - #745

Draft
adarshsm wants to merge 2 commits into
trentm:masterfrom
adarshsm:footnote-list-order
Draft

adarshsm wants to merge 2 commits into
trentm:masterfrom
adarshsm:footnote-list-order

Conversation

@adarshsm

@adarshsm adarshsm commented Oct 19, 2026 •

Copy link
Copy Markdown

Fixes #178.

Bug

With the footnotes extra, if footnote definitions aren't written in the same order as the references, a note's position in the footnote list doesn't match its number in the text:

>>> print(markdown2.markdown("- a[^2]\n\nb[^1]\n\n[^1]: one\n[^2]: two\n", extras=["footnotes"]))
<ul>
<li>a<sup class="footnote-ref" id="fnref-2"><a href="#fn-2">1</a></sup></li>
</ul>

<p>b<sup class="footnote-ref" id="fnref-1"><a href="#fn-1">2</a></sup></p>

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

The reference to [^1] reads 2 in the text, but its note is item 1 of the <ol>, and its back-link title says "footnote 1". The original 2015 report was about footnotes in lists and blockquotes. Those references are numbered correctly in order of appearance now, but the list is still out of step whenever the definitions are ordered differently.

Cause

Since #612 (now _do_footnote_marker, after #672), references are numbered on the final text, in order of appearance, and footnote_ids is filled in that order. _add_footnotes then still sorts footnote_ids by definition order. That sort was added in #536, back when numbering happened per span and references inside lists came out of order. #612 fixed the numbering but kept the sort, so now the sort only scrambles a list that is already correct.

Fix

Drop the sort, so the notes are listed in the order they're numbered.

  • New footnotes_defined_out_of_order test case: references in a paragraph and a blockquote, with definitions in a different order.
  • repeated_footnote_labels.html (from Preserve numbers for repeated footnote references #727) had the mismatch baked in: fn-second was listed first with the title "footnote 1", while the text numbered it 2. I updated it so fn-first (1) comes before fn-second (2).
  • footnotes_order_of_appearance and the other footnote cases pass unchanged.

Both test cases fail without the change and pass with it. With the fix, the full make testone run passes, except the 7 pygments-version-gated cases (pygments>=2.21 / <2.21), which fail identically on master with my local pygments.

🤖 Generated with Claude Code

adarshsm and others added 2 commits October 19, 2026 11:01
Footnote references are numbered in order of appearance by
_do_footnote_marker, but _add_footnotes then sorted the notes by the
order of their definitions. When the definitions weren't in reference
order, the note numbered 2 in the text could be the first item of the
list, and its back-link title said "footnote 1".

The sort dates from before _do_footnote_marker numbered references on
the final text, when it was needed to undo out-of-order numbering for
references inside lists and blockquotes. footnote_ids is now already in
numbering order, so drop the sort.

Fixes trentm#178

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nicholasserra

Copy link
Copy Markdown
Collaborator

Looks reasonable, thanks! Mark it as ready whenever and ill take a better look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong footnote order when footnote in list or quote

2 participants