Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintain order of appearance in footnotes #536

Merged
merged 2 commits into from
Nov 21, 2023

Conversation

Crozzers
Copy link
Contributor

This PR closes #533 by making sure footnotes maintain their order of appearance in the final HTML.

Previously, the order of the final footnotes list was based on the insertion order of footnote_ids. This array was populated as the parser processed the text and encountered footnotes. The problem is that markdown isn't processed entirely in order, leading to a disorganised footnotes list being generated.

This PR changes this by instead using the insertion order of footnotes, which is populated at the beginning of the conversion process by _strip_footnote_definitions.

The result is that the generated list of footnotes will appear in the same order as the footnote definitions.

@nicholasserra
Copy link
Collaborator

Nice, thank you!

@nicholasserra nicholasserra merged commit 182b7f5 into trentm:master Nov 21, 2023
18 checks passed
@radiofan
Copy link

radiofan commented Dec 7, 2024

Hello!
Maintain order of appearance in footnotes is really wrong...
I try execute this code

import markdown2

print(f'Markdown Version: {markdown2.__version__}')
print('-----')

MD = """
aboba [^1]
biba [^2]
boba [^3]
   
 - 1
   
    - 1.1 [^4]
   
 - 2
   
    - 2.1 [^5]
   
 - 3
   
    - 3.1 [^6]
   

[^1]: aboba
[^2]: biba
[^3]: boba
[^4]: 1.1
[^5]: 2.1
[^6]: 3.1
"""

extensions = ["footnotes", "tables", 'cuddled-lists']
extension_configs = {}

print(markdown2.markdown(MD, extras=extensions))

and result is

Markdown Version: 2.5.1
-----
<p>aboba <sup class="footnote-ref" id="fnref-1"><a href="#fn-1">4</a></sup>
biba <sup class="footnote-ref" id="fnref-2"><a href="#fn-2">5</a></sup>
boba <sup class="footnote-ref" id="fnref-3"><a href="#fn-3">6</a></sup></p>

<ul>
<li><p>1</p>

<ul>
<li>1.1 <sup class="footnote-ref" id="fnref-4"><a href="#fn-4">1</a></sup></li>
</ul></li>
<li><p>2</p>

<ul>
<li>2.1 <sup class="footnote-ref" id="fnref-5"><a href="#fn-5">2</a></sup></li>
</ul></li>
<li><p>3</p>

<ul>
<li>3.1 <sup class="footnote-ref" id="fnref-6"><a href="#fn-6">3</a></sup></li>
</ul></li>
</ul>

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

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

<li id="fn-3">
<p>boba&#160;<a href="#fnref-3" class="footnoteBackLink" title="Jump back to footnote 3 in the text.">&#8617;</a></p>
</li>

<li id="fn-4">
<p>1.1&#160;<a href="#fnref-4" class="footnoteBackLink" title="Jump back to footnote 4 in the text.">&#8617;</a></p>
</li>

<li id="fn-5">
<p>2.1&#160;<a href="#fnref-5" class="footnoteBackLink" title="Jump back to footnote 5 in the text.">&#8617;</a></p>
</li>

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

@Crozzers
Copy link
Contributor Author

Crozzers commented Dec 7, 2024

@radiofan FYI I've submitted #612 to fix this

nicholasserra added a commit that referenced this pull request Dec 7, 2024
Fix footnote labels appearing out-of-order (#536)
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.

Footnotes in lists are numbered before other footnotes
3 participants