@@ -55,7 +55,7 @@ use crate::html::format::Buffer;
55
55
use crate :: html:: highlight;
56
56
use crate :: html:: length_limit:: HtmlWithLimit ;
57
57
use crate :: html:: render:: small_url_encode;
58
- use crate :: html:: toc:: TocBuilder ;
58
+ use crate :: html:: toc:: { Toc , TocBuilder } ;
59
59
60
60
#[ cfg( test) ]
61
61
mod tests;
@@ -101,6 +101,7 @@ pub struct Markdown<'a> {
101
101
/// A struct like `Markdown` that renders the markdown with a table of contents.
102
102
pub ( crate ) struct MarkdownWithToc < ' a > {
103
103
pub ( crate ) content : & ' a str ,
104
+ pub ( crate ) links : & ' a [ RenderedLink ] ,
104
105
pub ( crate ) ids : & ' a mut IdMap ,
105
106
pub ( crate ) error_codes : ErrorCodes ,
106
107
pub ( crate ) edition : Edition ,
@@ -532,9 +533,9 @@ impl<'a, 'b, 'ids, I: Iterator<Item = SpannedEvent<'a>>> Iterator
532
533
let id = self . id_map . derive ( id) ;
533
534
534
535
if let Some ( ref mut builder) = self . toc {
535
- let mut html_header = String :: new ( ) ;
536
- html :: push_html ( & mut html_header , self . buf . iter ( ) . map ( |( ev, _) | ev. clone ( ) ) ) ;
537
- let sec = builder. push ( level as u32 , html_header , id. clone ( ) ) ;
536
+ let mut text_header = String :: new ( ) ;
537
+ plain_text_from_events ( self . buf . iter ( ) . map ( |( ev, _) | ev. clone ( ) ) , & mut text_header ) ;
538
+ let sec = builder. push ( level as u32 , text_header , id. clone ( ) ) ;
538
539
self . buf . push_front ( ( Event :: Html ( format ! ( "{sec} " ) . into ( ) ) , 0 ..0 ) ) ;
539
540
}
540
541
@@ -1415,10 +1416,23 @@ impl Markdown<'_> {
1415
1416
}
1416
1417
1417
1418
impl MarkdownWithToc < ' _ > {
1418
- pub ( crate ) fn into_string ( self ) -> String {
1419
- let MarkdownWithToc { content : md, ids, error_codes : codes, edition, playground } = self ;
1419
+ pub ( crate ) fn into_parts ( self ) -> ( Toc , String ) {
1420
+ let MarkdownWithToc { content : md, links, ids, error_codes : codes, edition, playground } =
1421
+ self ;
1420
1422
1421
- let p = Parser :: new_ext ( md, main_body_opts ( ) ) . into_offset_iter ( ) ;
1423
+ // This is actually common enough to special-case
1424
+ if md. is_empty ( ) {
1425
+ return ( Toc { entries : Vec :: new ( ) } , String :: new ( ) ) ;
1426
+ }
1427
+ let mut replacer = |broken_link : BrokenLink < ' _ > | {
1428
+ links
1429
+ . iter ( )
1430
+ . find ( |link| & * link. original_text == & * broken_link. reference )
1431
+ . map ( |link| ( link. href . as_str ( ) . into ( ) , link. tooltip . as_str ( ) . into ( ) ) )
1432
+ } ;
1433
+
1434
+ let p = Parser :: new_with_broken_link_callback ( md, main_body_opts ( ) , Some ( & mut replacer) ) ;
1435
+ let p = p. into_offset_iter ( ) ;
1422
1436
1423
1437
let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
1424
1438
@@ -1432,7 +1446,11 @@ impl MarkdownWithToc<'_> {
1432
1446
html:: push_html ( & mut s, p) ;
1433
1447
}
1434
1448
1435
- format ! ( "<nav id=\" TOC\" >{toc}</nav>{s}" , toc = toc. into_toc( ) . print( ) )
1449
+ ( toc. into_toc ( ) , s)
1450
+ }
1451
+ pub ( crate ) fn into_string ( self ) -> String {
1452
+ let ( toc, s) = self . into_parts ( ) ;
1453
+ format ! ( "<nav id=\" TOC\" >{toc}</nav>{s}" , toc = toc. print( ) )
1436
1454
}
1437
1455
}
1438
1456
@@ -1611,7 +1629,16 @@ pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> Strin
1611
1629
1612
1630
let p = Parser :: new_with_broken_link_callback ( md, summary_opts ( ) , Some ( & mut replacer) ) ;
1613
1631
1614
- for event in p {
1632
+ plain_text_from_events ( p, & mut s) ;
1633
+
1634
+ s
1635
+ }
1636
+
1637
+ pub ( crate ) fn plain_text_from_events < ' a > (
1638
+ events : impl Iterator < Item = pulldown_cmark:: Event < ' a > > ,
1639
+ s : & mut String ,
1640
+ ) {
1641
+ for event in events {
1615
1642
match & event {
1616
1643
Event :: Text ( text) => s. push_str ( text) ,
1617
1644
Event :: Code ( code) => {
@@ -1626,8 +1653,6 @@ pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> Strin
1626
1653
_ => ( ) ,
1627
1654
}
1628
1655
}
1629
-
1630
- s
1631
1656
}
1632
1657
1633
1658
#[ derive( Debug ) ]
0 commit comments