mistune icon indicating copy to clipboard operation
mistune copied to clipboard

Footnotes don't allow footnote references in the text

Open KajKandler opened this issue 1 year ago • 1 comments

This is a foo [^foo].

This is a bar [^bar].

[^foo]: A foo is beautiful. This is in contrast to a bar [^bar].

[^bar]: A bar is very common.

Is rendered as

  1. A foo is beautiful. This is in contrast to a bar [^bar].
  2. A bar is very common.

KajKandler avatar Feb 21 '24 10:02 KajKandler

It looks to me as if URLs in a footnote are not rendered either:

This is body text, and a footnote.[^f]

[^f]: The footnote includes [a link][1].

[1]: https://example.com

Renders as:

This is body text, and a footnote.¹

1. The footnote includes [a link][1]. ↩

Inline URLs work fine (e.g., [a link](https://example.com) renders correctly in the footnote).

dajare avatar Dec 10 '24 12:12 dajare

A few datapoints on the link issue (reported by @dajare above):

  1. only references [link][ref] are broken, normal links [link](url) seem to work fine; this may indicate that relevant document state doesn't get passed down when the footnote text is parsed
  2. this worked in v2 (so I guess it's a regression)

At a first glance the place to fix this would be in parse_footnote_item, will report back if I figure it out.

Update: ~~yup, in v2 the existing state is used when parsing the footnote text, whereas in v3 a fresh state is used (also, the footnote text is block.parse_text()ed only if it's longer than one line)~~ NVM, that doesn't seem to be it, I think references are resolved later, perhaps in md_footnotes_hook.

Update no. 2: passing the parent state in md_footnotes_hook here seems to fix it: state = BlockState(parent=state).

lemon24 avatar Dec 20 '25 23:12 lemon24

@lemon24 Please review https://github.com/lepture/mistune/pull/425

lepture avatar Dec 21 '25 04:12 lepture

@lepture LGTM (and it seems to have fixed the original footnote ref issue too 😁). Thank you!

lemon24 avatar Dec 21 '25 09:12 lemon24