@@ -405,6 +405,7 @@ def _setup_extras(self):
405405 # https://docs.python.org/3/whatsnew/3.7.html#summary-release-highlights
406406 self .footnotes = OrderedDict ()
407407 self .footnote_ids = []
408+ self ._footnote_marker = _hash_text ('<<footnote>>' )
408409 if "header-ids" in self .extras :
409410 if not hasattr (self , '_count_from_header_id' ) or self .extras ['header-ids' ].get ('reset-count' , False ):
410411 self ._count_from_header_id = defaultdict (int )
@@ -510,6 +511,12 @@ def convert(self, text: str) -> 'UnicodeWithAttrs':
510511 text = self ._run_block_gamut (text )
511512
512513 if "footnotes" in self .extras :
514+ def footnote_sub (match ):
515+ normed_id = match .group (1 )
516+ self .footnote_ids .append (normed_id )
517+ return str (len (self .footnote_ids ))
518+
519+ text = re .sub (r'%s-(.*?)(?=</a></sup>)' % self ._footnote_marker , footnote_sub , text )
513520 text = self ._add_footnotes (text )
514521
515522 text = self .postprocess (text )
@@ -1581,10 +1588,10 @@ def _do_links(self, text: str) -> str:
15811588 if "footnotes" in self .extras and link_text .startswith ("^" ):
15821589 normed_id = re .sub (r'\W' , '-' , link_text [1 :])
15831590 if normed_id in self .footnotes :
1584- self .footnote_ids .append (normed_id )
15851591 result = (
15861592 f'<sup class="footnote-ref" id="fnref-{ normed_id } ">'
1587- f'<a href="#fn-{ normed_id } ">{ len (self .footnote_ids )} </a></sup>'
1593+ # insert special footnote marker that's easy to find and match against later
1594+ f'<a href="#fn-{ normed_id } ">{ self ._footnote_marker } -{ normed_id } </a></sup>'
15881595 )
15891596 text = text [:start_idx ] + result + text [p + 1 :]
15901597 else :
0 commit comments