@@ -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 ,
@@ -530,9 +531,9 @@ impl<'a, 'b, 'ids, I: Iterator<Item = SpannedEvent<'a>>> Iterator
530
531
let id = self . id_map . derive ( id) ;
531
532
532
533
if let Some ( ref mut builder) = self . toc {
533
- let mut html_header = String :: new ( ) ;
534
- html :: push_html ( & mut html_header , self . buf . iter ( ) . map ( |( ev, _) | ev. clone ( ) ) ) ;
535
- let sec = builder. push ( level as u32 , html_header , id. clone ( ) ) ;
534
+ let mut text_header = String :: new ( ) ;
535
+ plain_text_from_events ( self . buf . iter ( ) . map ( |( ev, _) | ev. clone ( ) ) , & mut text_header ) ;
536
+ let sec = builder. push ( level as u32 , text_header , id. clone ( ) ) ;
536
537
self . buf . push_front ( ( Event :: Html ( format ! ( "{sec} " ) . into ( ) ) , 0 ..0 ) ) ;
537
538
}
538
539
@@ -1395,10 +1396,23 @@ impl Markdown<'_> {
1395
1396
}
1396
1397
1397
1398
impl MarkdownWithToc < ' _ > {
1398
- pub ( crate ) fn into_string ( self ) -> String {
1399
- let MarkdownWithToc { content : md, ids, error_codes : codes, edition, playground } = self ;
1399
+ pub ( crate ) fn into_parts ( self ) -> ( Toc , String ) {
1400
+ let MarkdownWithToc { content : md, links, ids, error_codes : codes, edition, playground } =
1401
+ self ;
1400
1402
1401
- let p = Parser :: new_ext ( md, main_body_opts ( ) ) . into_offset_iter ( ) ;
1403
+ // This is actually common enough to special-case
1404
+ if md. is_empty ( ) {
1405
+ return ( Toc { entries : Vec :: new ( ) } , String :: new ( ) ) ;
1406
+ }
1407
+ let mut replacer = |broken_link : BrokenLink < ' _ > | {
1408
+ links
1409
+ . iter ( )
1410
+ . find ( |link| & * link. original_text == & * broken_link. reference )
1411
+ . map ( |link| ( link. href . as_str ( ) . into ( ) , link. tooltip . as_str ( ) . into ( ) ) )
1412
+ } ;
1413
+
1414
+ let p = Parser :: new_with_broken_link_callback ( md, main_body_opts ( ) , Some ( & mut replacer) ) ;
1415
+ let p = p. into_offset_iter ( ) ;
1402
1416
1403
1417
let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
1404
1418
@@ -1412,7 +1426,11 @@ impl MarkdownWithToc<'_> {
1412
1426
html:: push_html ( & mut s, p) ;
1413
1427
}
1414
1428
1415
- format ! ( "<nav id=\" TOC\" >{toc}</nav>{s}" , toc = toc. into_toc( ) . print( ) )
1429
+ ( toc. into_toc ( ) , s)
1430
+ }
1431
+ pub ( crate ) fn into_string ( self ) -> String {
1432
+ let ( toc, s) = self . into_parts ( ) ;
1433
+ format ! ( "<nav id=\" TOC\" >{toc}</nav>{s}" , toc = toc. print( ) )
1416
1434
}
1417
1435
}
1418
1436
@@ -1591,7 +1609,16 @@ pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> Strin
1591
1609
1592
1610
let p = Parser :: new_with_broken_link_callback ( md, summary_opts ( ) , Some ( & mut replacer) ) ;
1593
1611
1594
- for event in p {
1612
+ plain_text_from_events ( p, & mut s) ;
1613
+
1614
+ s
1615
+ }
1616
+
1617
+ pub ( crate ) fn plain_text_from_events < ' a > (
1618
+ events : impl Iterator < Item = pulldown_cmark:: Event < ' a > > ,
1619
+ s : & mut String ,
1620
+ ) {
1621
+ for event in events {
1595
1622
match & event {
1596
1623
Event :: Text ( text) => s. push_str ( text) ,
1597
1624
Event :: Code ( code) => {
@@ -1606,8 +1633,6 @@ pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> Strin
1606
1633
_ => ( ) ,
1607
1634
}
1608
1635
}
1609
-
1610
- s
1611
1636
}
1612
1637
1613
1638
#[ derive( Debug ) ]
0 commit comments