@@ -51,7 +51,7 @@ use crate::html::format::Buffer;
51
51
use crate :: html:: highlight;
52
52
use crate :: html:: length_limit:: HtmlWithLimit ;
53
53
use crate :: html:: render:: small_url_encode;
54
- use crate :: html:: toc:: TocBuilder ;
54
+ use crate :: html:: toc:: { Toc , TocBuilder } ;
55
55
56
56
use pulldown_cmark:: {
57
57
html, BrokenLink , BrokenLinkCallback , CodeBlockKind , CowStr , Event , LinkType , OffsetIter ,
@@ -102,6 +102,7 @@ pub struct Markdown<'a> {
102
102
/// A struct like `Markdown` that renders the markdown with a table of contents.
103
103
pub ( crate ) struct MarkdownWithToc < ' a > {
104
104
pub ( crate ) content : & ' a str ,
105
+ pub ( crate ) links : & ' a [ RenderedLink ] ,
105
106
pub ( crate ) ids : & ' a mut IdMap ,
106
107
pub ( crate ) error_codes : ErrorCodes ,
107
108
pub ( crate ) edition : Edition ,
@@ -531,9 +532,9 @@ impl<'a, 'b, 'ids, I: Iterator<Item = SpannedEvent<'a>>> Iterator
531
532
let id = self . id_map . derive ( id) ;
532
533
533
534
if let Some ( ref mut builder) = self . toc {
534
- let mut html_header = String :: new ( ) ;
535
- html :: push_html ( & mut html_header , self . buf . iter ( ) . map ( |( ev, _) | ev. clone ( ) ) ) ;
536
- let sec = builder. push ( level as u32 , html_header , id. clone ( ) ) ;
535
+ let mut text_header = String :: new ( ) ;
536
+ plain_text_from_events ( self . buf . iter ( ) . map ( |( ev, _) | ev. clone ( ) ) , & mut text_header ) ;
537
+ let sec = builder. push ( level as u32 , text_header , id. clone ( ) ) ;
537
538
self . buf . push_front ( ( Event :: Html ( format ! ( "{sec} " ) . into ( ) ) , 0 ..0 ) ) ;
538
539
}
539
540
@@ -1396,10 +1397,23 @@ impl Markdown<'_> {
1396
1397
}
1397
1398
1398
1399
impl MarkdownWithToc < ' _ > {
1399
- pub ( crate ) fn into_string ( self ) -> String {
1400
- let MarkdownWithToc { content : md, ids, error_codes : codes, edition, playground } = self ;
1400
+ pub ( crate ) fn into_parts ( self ) -> ( Toc , String ) {
1401
+ let MarkdownWithToc { content : md, links, ids, error_codes : codes, edition, playground } =
1402
+ self ;
1401
1403
1402
- let p = Parser :: new_ext ( md, main_body_opts ( ) ) . into_offset_iter ( ) ;
1404
+ // This is actually common enough to special-case
1405
+ if md. is_empty ( ) {
1406
+ return ( Toc { entries : Vec :: new ( ) } , String :: new ( ) ) ;
1407
+ }
1408
+ let mut replacer = |broken_link : BrokenLink < ' _ > | {
1409
+ links
1410
+ . iter ( )
1411
+ . find ( |link| & * link. original_text == & * broken_link. reference )
1412
+ . map ( |link| ( link. href . as_str ( ) . into ( ) , link. tooltip . as_str ( ) . into ( ) ) )
1413
+ } ;
1414
+
1415
+ let p = Parser :: new_with_broken_link_callback ( md, main_body_opts ( ) , Some ( & mut replacer) ) ;
1416
+ let p = p. into_offset_iter ( ) ;
1403
1417
1404
1418
let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
1405
1419
@@ -1413,7 +1427,11 @@ impl MarkdownWithToc<'_> {
1413
1427
html:: push_html ( & mut s, p) ;
1414
1428
}
1415
1429
1416
- format ! ( "<nav id=\" TOC\" >{toc}</nav>{s}" , toc = toc. into_toc( ) . print( ) )
1430
+ ( toc. into_toc ( ) , s)
1431
+ }
1432
+ pub ( crate ) fn into_string ( self ) -> String {
1433
+ let ( toc, s) = self . into_parts ( ) ;
1434
+ format ! ( "<nav id=\" TOC\" >{toc}</nav>{s}" , toc = toc. print( ) )
1417
1435
}
1418
1436
}
1419
1437
@@ -1592,7 +1610,16 @@ pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> Strin
1592
1610
1593
1611
let p = Parser :: new_with_broken_link_callback ( md, summary_opts ( ) , Some ( & mut replacer) ) ;
1594
1612
1595
- for event in p {
1613
+ plain_text_from_events ( p, & mut s) ;
1614
+
1615
+ s
1616
+ }
1617
+
1618
+ pub ( crate ) fn plain_text_from_events < ' a > (
1619
+ events : impl Iterator < Item = pulldown_cmark:: Event < ' a > > ,
1620
+ s : & mut String ,
1621
+ ) {
1622
+ for event in events {
1596
1623
match & event {
1597
1624
Event :: Text ( text) => s. push_str ( text) ,
1598
1625
Event :: Code ( code) => {
@@ -1607,8 +1634,6 @@ pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> Strin
1607
1634
_ => ( ) ,
1608
1635
}
1609
1636
}
1610
-
1611
- s
1612
1637
}
1613
1638
1614
1639
#[ derive( Debug ) ]
0 commit comments