@@ -632,11 +632,8 @@ fn render_heading<'a, T>(
632632 context. write_all ( b">" ) ?;
633633
634634 if let Some ( ref prefix) = context. options . extension . header_ids {
635- let mut text_content = Vec :: with_capacity ( 20 ) ;
636- collect_text ( node, & mut text_content) ;
637-
638- let mut id = String :: from_utf8 ( text_content) . unwrap ( ) ;
639- id = context. anchorizer . anchorize ( id) ;
635+ let text_content = collect_text ( node) ;
636+ let id = context. anchorizer . anchorize ( text_content) ;
640637 write ! (
641638 context,
642639 "<a href=\" #{}\" aria-hidden=\" true\" class=\" anchor\" id=\" {}{}\" ></a>" ,
@@ -648,12 +645,10 @@ fn render_heading<'a, T>(
648645 }
649646 }
650647 Some ( adapter) => {
651- let mut text_content = Vec :: with_capacity ( 20 ) ;
652- collect_text ( node, & mut text_content) ;
653- let content = String :: from_utf8 ( text_content) . unwrap ( ) ;
648+ let text_content = collect_text ( node) ;
654649 let heading = HeadingMeta {
655650 level : nh. level ,
656- content,
651+ content : text_content ,
657652 } ;
658653
659654 if entering {
@@ -1587,22 +1582,30 @@ fn render_wiki_link<'a, T>(
15871582
15881583// Helpers
15891584
1585+ /// Recurses through a node and all of its children in depth-first (document)
1586+ /// order, returning the concatenated literal contents of text, code and math
1587+ /// blocks. Line breaks and soft breaks are represented as a single whitespace
1588+ /// character.
1589+ pub fn collect_text < ' a > ( node : & ' a AstNode < ' a > ) -> String {
1590+ let mut text = String :: with_capacity ( 20 ) ;
1591+ collect_text_append ( node, & mut text) ;
1592+ text
1593+ }
1594+
15901595/// Recurses through a node and all of its children in depth-first (document)
15911596/// order, appending the literal contents of text, code and math blocks to
15921597/// an output buffer. Line breaks and soft breaks are represented as a single
15931598/// whitespace character.
1594- pub fn collect_text < ' a > ( node : & ' a AstNode < ' a > , output : & mut Vec < u8 > ) {
1599+ pub fn collect_text_append < ' a > ( node : & ' a AstNode < ' a > , output : & mut String ) {
15951600 match node. data . borrow ( ) . value {
15961601 NodeValue :: Text ( ref literal) | NodeValue :: Code ( NodeCode { ref literal, .. } ) => {
1597- output. extend_from_slice ( literal. as_bytes ( ) )
1598- }
1599- NodeValue :: LineBreak | NodeValue :: SoftBreak => output. push ( b' ' ) ,
1600- NodeValue :: Math ( NodeMath { ref literal, .. } ) => {
1601- output. extend_from_slice ( literal. as_bytes ( ) )
1602+ output. push_str ( literal)
16021603 }
1604+ NodeValue :: LineBreak | NodeValue :: SoftBreak => output. push ( ' ' ) ,
1605+ NodeValue :: Math ( NodeMath { ref literal, .. } ) => output. push_str ( literal) ,
16031606 _ => {
16041607 for n in node. children ( ) {
1605- collect_text ( n, output) ;
1608+ collect_text_append ( n, output) ;
16061609 }
16071610 }
16081611 }
0 commit comments