@@ -1333,12 +1333,14 @@ impl Markdown<'_> {
1333
1333
1334
1334
let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
1335
1335
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
+ } ) ;
1342
1344
1343
1345
s
1344
1346
}
@@ -1367,13 +1369,13 @@ impl MarkdownWithToc<'_> {
1367
1369
1368
1370
let mut toc = TocBuilder :: new ( ) ;
1369
1371
1370
- {
1372
+ ids . handle_footnotes ( |ids , existing_footnotes| {
1371
1373
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 ) ;
1373
1375
let p = TableWrapper :: new ( p. map ( |( ev, _) | ev) ) ;
1374
1376
let p = CodeBlocks :: new ( p, codes, edition, playground) ;
1375
1377
html:: push_html ( & mut s, p) ;
1376
- }
1378
+ } ) ;
1377
1379
1378
1380
( toc. into_toc ( ) , s)
1379
1381
}
@@ -1401,13 +1403,15 @@ impl MarkdownItemInfo<'_> {
1401
1403
1402
1404
let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
1403
1405
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) ;
1409
1414
} ) ;
1410
- html:: push_html ( & mut s, p) ;
1411
1415
1412
1416
s
1413
1417
}
@@ -1882,6 +1886,7 @@ pub(crate) fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<Rust
1882
1886
#[ derive( Clone , Default , Debug ) ]
1883
1887
pub struct IdMap {
1884
1888
map : FxHashMap < Cow < ' static , str > , usize > ,
1889
+ existing_footnotes : usize ,
1885
1890
}
1886
1891
1887
1892
// 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> {
1943
1948
1944
1949
impl IdMap {
1945
1950
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 }
1947
1952
}
1948
1953
1949
1954
pub ( crate ) fn derive < S : AsRef < str > + ToString > ( & mut self , candidate : S ) -> String {
@@ -1959,4 +1964,13 @@ impl IdMap {
1959
1964
self . map . insert ( id. clone ( ) . into ( ) , 1 ) ;
1960
1965
id
1961
1966
}
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
+ }
1962
1976
}
0 commit comments