@@ -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 , CodeBlockKind , CowStr , Event , LinkType , OffsetIter , Options , Parser , Tag ,
@@ -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 ,
@@ -534,9 +535,9 @@ impl<'a, 'b, 'ids, I: Iterator<Item = SpannedEvent<'a>>> Iterator
534
535
let id = self . id_map . derive ( id) ;
535
536
536
537
if let Some ( ref mut builder) = self . toc {
537
- let mut html_header = String :: new ( ) ;
538
- html :: push_html ( & mut html_header , self . buf . iter ( ) . map ( |( ev, _) | ev. clone ( ) ) ) ;
539
- let sec = builder. push ( level as u32 , html_header , id. clone ( ) ) ;
538
+ let mut text_header = String :: new ( ) ;
539
+ plain_text_from_events ( self . buf . iter ( ) . map ( |( ev, _) | ev. clone ( ) ) , & mut text_header ) ;
540
+ let sec = builder. push ( level as u32 , text_header , id. clone ( ) ) ;
540
541
self . buf . push_front ( ( Event :: Html ( format ! ( "{sec} " ) . into ( ) ) , 0 ..0 ) ) ;
541
542
}
542
543
@@ -1399,10 +1400,23 @@ impl Markdown<'_> {
1399
1400
}
1400
1401
1401
1402
impl MarkdownWithToc < ' _ > {
1402
- pub ( crate ) fn into_string ( self ) -> String {
1403
- let MarkdownWithToc { content : md, ids, error_codes : codes, edition, playground } = self ;
1403
+ pub ( crate ) fn into_parts ( self ) -> ( Toc , String ) {
1404
+ let MarkdownWithToc { content : md, links, ids, error_codes : codes, edition, playground } =
1405
+ self ;
1404
1406
1405
- let p = Parser :: new_ext ( md, main_body_opts ( ) ) . into_offset_iter ( ) ;
1407
+ // This is actually common enough to special-case
1408
+ if md. is_empty ( ) {
1409
+ return ( Toc { entries : Vec :: new ( ) } , String :: new ( ) ) ;
1410
+ }
1411
+ let mut replacer = |broken_link : BrokenLink < ' _ > | {
1412
+ links
1413
+ . iter ( )
1414
+ . find ( |link| & * link. original_text == & * broken_link. reference )
1415
+ . map ( |link| ( link. href . as_str ( ) . into ( ) , link. tooltip . as_str ( ) . into ( ) ) )
1416
+ } ;
1417
+
1418
+ let p = Parser :: new_with_broken_link_callback ( md, main_body_opts ( ) , Some ( & mut replacer) ) ;
1419
+ let p = p. into_offset_iter ( ) ;
1406
1420
1407
1421
let mut s = String :: with_capacity ( md. len ( ) * 3 / 2 ) ;
1408
1422
@@ -1416,7 +1430,11 @@ impl MarkdownWithToc<'_> {
1416
1430
html:: push_html ( & mut s, p) ;
1417
1431
}
1418
1432
1419
- format ! ( "<nav id=\" TOC\" >{toc}</nav>{s}" , toc = toc. into_toc( ) . print( ) )
1433
+ ( toc. into_toc ( ) , s)
1434
+ }
1435
+ pub ( crate ) fn into_string ( self ) -> String {
1436
+ let ( toc, s) = self . into_parts ( ) ;
1437
+ format ! ( "<nav id=\" TOC\" >{toc}</nav>{s}" , toc = toc. print( ) )
1420
1438
}
1421
1439
}
1422
1440
@@ -1595,7 +1613,16 @@ pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> Strin
1595
1613
1596
1614
let p = Parser :: new_with_broken_link_callback ( md, summary_opts ( ) , Some ( & mut replacer) ) ;
1597
1615
1598
- for event in p {
1616
+ plain_text_from_events ( p, & mut s) ;
1617
+
1618
+ s
1619
+ }
1620
+
1621
+ pub ( crate ) fn plain_text_from_events < ' a > (
1622
+ events : impl Iterator < Item = pulldown_cmark:: Event < ' a > > ,
1623
+ s : & mut String ,
1624
+ ) {
1625
+ for event in events {
1599
1626
match & event {
1600
1627
Event :: Text ( text) => s. push_str ( text) ,
1601
1628
Event :: Code ( code) => {
@@ -1610,8 +1637,6 @@ pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> Strin
1610
1637
_ => ( ) ,
1611
1638
}
1612
1639
}
1613
-
1614
- s
1615
1640
}
1616
1641
1617
1642
#[ derive( Debug ) ]
0 commit comments