Skip to content

Don't add an empty footnotes block when no footnote is referenced - #758

Open
RavSinghChandan wants to merge 4 commits into
trentm:masterfrom
RavSinghChandan:footnotes-without-references
Open

RavSinghChandan wants to merge 4 commits into
trentm:masterfrom
RavSinghChandan:footnotes-without-references

Conversation

@RavSinghChandan

Copy link
Copy Markdown

With the footnotes extra, a footnote that is defined but never referenced still makes markdown2 append the footnotes block, with an empty list:

>>> markdown2.markdown("Text.\n\n[^1]: A note.\n", extras=["footnotes"])
'<p>Text.</p>\n\n<div class="footnotes">\n<hr />\n<ol>\n</ol>\n</div>\n'

_add_footnotes checks self.footnotes (the definitions) but only lists self.footnote_ids (the referenced ones), so the block is empty when nothing is referenced. It now checks the referenced ids, so the output is just <p>Text.</p>. Documents with referenced footnotes are unchanged, and unreferenced definitions were already left out of the list.

Added the footnotes_unreferenced test case

@nicholasserra

Copy link
Copy Markdown
Collaborator

New test missing the html file

@RavSinghChandan

Copy link
Copy Markdown
Author

Thanks, added footnotes_unreferenced.html (and merged master).

@nicholasserra

Copy link
Copy Markdown
Collaborator

@Crozzers Can you think of any reason we'd want to keep a footnote that isn't used anywhere in the text body?

On one hand it's not needed, on another hand if the user inputs a footnote then they might have some reason

@RavSinghChandan

Copy link
Copy Markdown
Author

One detail that may help: this PR doesn't change whether an unused footnote is kept, master already leaves it out. For example, with Text[^a]. and the definitions [^a]: Used. and [^b]: Unused., master lists only a; the text of b is not in the output at all.

The only difference is when no footnote is referenced: master still writes <div class="footnotes"><hr /><ol></ol></div> with nothing in it, which renders as a stray horizontal rule at the end of the page. This PR just skips that empty block.

If you'd rather keep unused footnotes, that would be a separate change to how the list is built, and I'm happy to do it.

@Crozzers

Crozzers commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Explanation makes sense. Can't think of a reason to keep other than if it does draw a stray <hr> then that tells the user they've got dead footnotes they should clean up.
But I think this approach makes more sense

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.

3 participants