@@ -1333,12 +1333,14 @@ impl Markdown<'_> {
13331333
13341334 let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
13351335
1336- let p = HeadingLinks :: new ( p, None , ids, heading_offset) ;
1337- let p = footnotes:: Footnotes :: new ( p) ;
1338- let p = LinkReplacer :: new ( p. map ( |( ev, _) | ev) , links) ;
1339- let p = TableWrapper :: new ( p) ;
1340- let p = CodeBlocks :: new ( p, codes, edition, playground) ;
1341- html:: push_html ( & mut s, p) ;
1336+ ids. handle_footnotes ( |ids, existing_footnotes| {
1337+ let p = HeadingLinks :: new ( p, None , ids, heading_offset) ;
1338+ let p = footnotes:: Footnotes :: new ( p, existing_footnotes) ;
1339+ let p = LinkReplacer :: new ( p. map ( |( ev, _) | ev) , links) ;
1340+ let p = TableWrapper :: new ( p) ;
1341+ let p = CodeBlocks :: new ( p, codes, edition, playground) ;
1342+ html:: push_html ( & mut s, p) ;
1343+ } ) ;
13421344
13431345 s
13441346 }
@@ -1367,13 +1369,13 @@ impl MarkdownWithToc<'_> {
13671369
13681370 let mut toc = TocBuilder :: new ( ) ;
13691371
1370- {
1372+ ids . handle_footnotes ( |ids , existing_footnotes| {
13711373 let p = HeadingLinks :: new ( p, Some ( & mut toc) , ids, HeadingOffset :: H1 ) ;
1372- let p = footnotes:: Footnotes :: new ( p) ;
1374+ let p = footnotes:: Footnotes :: new ( p, existing_footnotes ) ;
13731375 let p = TableWrapper :: new ( p. map ( |( ev, _) | ev) ) ;
13741376 let p = CodeBlocks :: new ( p, codes, edition, playground) ;
13751377 html:: push_html ( & mut s, p) ;
1376- }
1378+ } ) ;
13771379
13781380 ( toc. into_toc ( ) , s)
13791381 }
@@ -1401,13 +1403,15 @@ impl MarkdownItemInfo<'_> {
14011403
14021404 let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
14031405
1404- let p = HeadingLinks :: new ( p, None , ids, HeadingOffset :: H1 ) ;
1405- let p = footnotes:: Footnotes :: new ( p) ;
1406- let p = TableWrapper :: new ( p. map ( |( ev, _) | ev) ) ;
1407- let p = p. filter ( |event| {
1408- !matches ! ( event, Event :: Start ( Tag :: Paragraph ) | Event :: End ( TagEnd :: Paragraph ) )
1406+ ids. handle_footnotes ( |ids, existing_footnotes| {
1407+ let p = HeadingLinks :: new ( p, None , ids, HeadingOffset :: H1 ) ;
1408+ let p = footnotes:: Footnotes :: new ( p, existing_footnotes) ;
1409+ let p = TableWrapper :: new ( p. map ( |( ev, _) | ev) ) ;
1410+ let p = p. filter ( |event| {
1411+ !matches ! ( event, Event :: Start ( Tag :: Paragraph ) | Event :: End ( TagEnd :: Paragraph ) )
1412+ } ) ;
1413+ html:: push_html ( & mut s, p) ;
14091414 } ) ;
1410- html:: push_html ( & mut s, p) ;
14111415
14121416 s
14131417 }
@@ -1882,6 +1886,7 @@ pub(crate) fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<Rust
18821886#[ derive( Clone , Default , Debug ) ]
18831887pub struct IdMap {
18841888 map : FxHashMap < Cow < ' static , str > , usize > ,
1889+ existing_footnotes : usize ,
18851890}
18861891
18871892// The map is pre-initialized and cloned each time to avoid reinitializing it repeatedly.
@@ -1943,7 +1948,7 @@ fn init_id_map() -> FxHashMap<Cow<'static, str>, usize> {
19431948
19441949impl IdMap {
19451950 pub fn new ( ) -> Self {
1946- IdMap { map : DEFAULT_ID_MAP . get_or_init ( init_id_map) . clone ( ) }
1951+ IdMap { map : DEFAULT_ID_MAP . get_or_init ( init_id_map) . clone ( ) , existing_footnotes : 0 }
19471952 }
19481953
19491954 pub ( crate ) fn derive < S : AsRef < str > + ToString > ( & mut self , candidate : S ) -> String {
@@ -1959,4 +1964,13 @@ impl IdMap {
19591964 self . map . insert ( id. clone ( ) . into ( ) , 1 ) ;
19601965 id
19611966 }
1967+
1968+ /// Method to handle `existing_footnotes` increment automatically (to prevent forgetting
1969+ /// about it).
1970+ pub ( crate ) fn handle_footnotes < F : FnOnce ( & mut Self , & mut usize ) > ( & mut self , closure : F ) {
1971+ let mut existing_footnotes = self . existing_footnotes ;
1972+
1973+ closure ( self , & mut existing_footnotes) ;
1974+ self . existing_footnotes = existing_footnotes;
1975+ }
19621976}
0 commit comments