trentm / python-markdown2

markdown2: A fast and complete implementation of Markdown in Python
Other
2.66k stars 433 forks source link

Footnotes in lists are numbered before other footnotes #533

Closed goofpunk closed 11 months ago

goofpunk commented 1 year ago

Describe the bug If you include footnotes in a list (whether ordered or unordered), the footnotes that are in the list get numbered first, and are at the top of the list at the bottom of the HTML output.

To Reproduce

---
title: testing the footnotes bug
---
Lorem ipsum dolor sit amet. Aliqua cillum, eu velit.[^note1] Velit deserunt adipiscing adipiscing ullamco exercitation.

- this is a list item
- this is a list item[^note2]
- this is a list item[^note3]

Lorem ipsum dolor sit amet. Adipiscing[^note4] adipiscing ullamco, exercitation sint. Exercitation sint, fugiat exercitation voluptate amet.

[^note1]: this should be the first note
[^note2]: this should be the second note
[^note3]: this should be the third note
[^note4]: this should be the fourth note

Expected behavior Notes should be in the order they were numbered. Instead, the results (in both the numbering and the list at the bottom) are:

Debug info Version of library being used: 2.4.10

Any extras being used: reproducible with no extras other than footnotes

goofpunk commented 1 year ago

I noticed another, much more minor issue recently, that I think is related: if I mix regular HTML headings in with the Markdown, they don't get picked up in the table of contents. Are all of the extras being run before the main markdown conversion process? That could lead to both of these bugs.

Crozzers commented 1 year ago

Are all of the extras being run before the main markdown conversion process?

The table of contents extra is run when converting MD headers to HTML. I think most extras are run that way as well. For this specific issue, I guess we could do basic parsing of the h[1-6] tags, add them to the TOC and then sort by the line number, since h[1-6] tags are block level.

Notes should be in the order they were numbered.

Could probably apply the same "sort in order of appearance" logic to this. Not sure if footnotes are considered "block level" so might have to secondary sort by column as well.

I'll look into both of these at some point

Crozzers commented 12 months ago

I've created a PR that fixes the main point of this issue, which is footnote ordering. Footnotes will now appear in the same order that they are defined. This behaviour is in line with what python-markdown does (couldn't find explicit docs but I tested it locally).

For the headers issue, I've spun it off into a separate issue (#537) and I'll try to look at it soon