@@ -405,6 +405,7 @@ def _setup_extras(self):
405
405
# https://docs.python.org/3/whatsnew/3.7.html#summary-release-highlights
406
406
self .footnotes = OrderedDict ()
407
407
self .footnote_ids = []
408
+ self ._footnote_marker = _hash_text ('<<footnote>>' )
408
409
if "header-ids" in self .extras :
409
410
if not hasattr (self , '_count_from_header_id' ) or self .extras ['header-ids' ].get ('reset-count' , False ):
410
411
self ._count_from_header_id = defaultdict (int )
@@ -510,6 +511,12 @@ def convert(self, text: str) -> 'UnicodeWithAttrs':
510
511
text = self ._run_block_gamut (text )
511
512
512
513
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 )
513
520
text = self ._add_footnotes (text )
514
521
515
522
text = self .postprocess (text )
@@ -1581,10 +1588,10 @@ def _do_links(self, text: str) -> str:
1581
1588
if "footnotes" in self .extras and link_text .startswith ("^" ):
1582
1589
normed_id = re .sub (r'\W' , '-' , link_text [1 :])
1583
1590
if normed_id in self .footnotes :
1584
- self .footnote_ids .append (normed_id )
1585
1591
result = (
1586
1592
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>'
1588
1595
)
1589
1596
text = text [:start_idx ] + result + text [p + 1 :]
1590
1597
else :
0 commit comments