@@ -460,11 +460,15 @@ impl ItemizedBlock {
460460 /// 1868. He wes buried in...
461461 /// ```
462462 fn get_marker_length ( trimmed : & str ) -> Option < usize > {
463+ if trimmed. starts_with ( '>' ) {
464+ return Some ( 0 ) ;
465+ }
466+
463467 // https://spec.commonmark.org/0.30/#bullet-list-marker or
464468 // https://spec.commonmark.org/0.30/#block-quote-marker
465- let itemized_start = [ "* " , "- " , ">" , " + "] ;
466- if let Some ( s ) = itemized_start. iter ( ) . find ( | & & s| trimmed. starts_with ( s) ) {
467- return Some ( s . len ( ) ) ;
469+ let itemized_start = [ "* " , "- " , "+ " ] ;
470+ if itemized_start. iter ( ) . any ( | & s| trimmed. starts_with ( s) ) {
471+ return Some ( 2 ) ;
468472 }
469473
470474 // https://spec.commonmark.org/0.30/#ordered-list-marker, where at most 2 digits are
@@ -485,25 +489,47 @@ impl ItemizedBlock {
485489 /// Creates a new `ItemizedBlock` described with the given `line`.
486490 /// Returns `None` if `line` doesn't start an item.
487491 fn new ( line : & str ) -> Option < ItemizedBlock > {
488- let marker_length = ItemizedBlock :: get_marker_length ( line. trim_start ( ) ) ?;
489- let space_to_marker = line. chars ( ) . take_while ( |c| c. is_whitespace ( ) ) . count ( ) ;
490- let mut indent = space_to_marker + marker_length;
491- let mut line_start = " " . repeat ( indent) ;
492-
493- // Markdown blockquote start with a "> "
494- if line. trim_start ( ) . starts_with ( '>' ) {
495- // remove the original +1 indent because there might be multiple nested block quotes
496- // and it's easier to reason about the final indent by just taking the length
497- // of the new line_start. We update the indent because it effects the max width
498- // of each formatted line.
499- line_start = itemized_block_quote_start ( line, line_start, 1 ) ;
500- indent = line_start. len ( ) ;
492+ let trimmed = line. trim_start ( ) ;
493+ let marker_length = ItemizedBlock :: get_marker_length ( trimmed) ?;
494+ let space_to_marker = line
495+ . chars ( )
496+ . take_while ( |& c| c == ' ' || c == '\t' )
497+ . fold ( 0 , |acc, c| acc + if c == ' ' { 1 } else { 4 } ) ;
498+
499+ let mut line_start = " " . repeat ( space_to_marker + marker_length) ;
500+ let mut i = line. len ( ) - trimmed. len ( ) + marker_length;
501+
502+ // Markdown blockquote start with a '>'
503+ if trimmed. starts_with ( '>' ) {
504+ // Determine the line_start when formatting markdown block quotes.
505+ // The original line_start likely contains indentation (whitespaces),
506+ // which we'd like to replace with '> ' characters.
507+ let mut quote_level = 0 ;
508+
509+ let bytes = line. as_bytes ( ) ;
510+ while i < bytes. len ( ) && bytes[ i] == b'>' {
511+ i += 1 ;
512+ quote_level += 1 ;
513+
514+ let mut whitespace_width = 0 ;
515+ while i < bytes. len ( ) && ( bytes[ i] == b' ' || bytes[ i] == b'\t' ) {
516+ whitespace_width += if bytes[ i] == b' ' { 1 } else { 4 } ;
517+ if whitespace_width > 4 {
518+ break ;
519+ }
520+ i += 1 ;
521+ }
522+ }
523+
524+ for _ in 0 ..quote_level {
525+ line_start. push_str ( "> " ) ;
526+ }
501527 }
502528
503529 Some ( ItemizedBlock {
504- lines : vec ! [ line[ indent ..] . to_string( ) ] ,
505- indent,
506- opener : line[ ..indent ] . to_string ( ) ,
530+ lines : vec ! [ line[ i ..] . to_string( ) ] ,
531+ indent : line_start . len ( ) ,
532+ opener : line[ ..i ] . to_string ( ) ,
507533 line_start,
508534 } )
509535 }
@@ -548,27 +574,6 @@ impl ItemizedBlock {
548574 }
549575}
550576
551- /// Determine the line_start when formatting markdown block quotes.
552- /// The original line_start likely contains indentation (whitespaces), which we'd like to
553- /// replace with '> ' characters.
554- fn itemized_block_quote_start ( line : & str , mut line_start : String , remove_indent : usize ) -> String {
555- let quote_level = line
556- . chars ( )
557- . take_while ( |& c| matches ! ( c, '>' | ' ' ) )
558- . filter ( |& c| c == '>' )
559- . count ( ) ;
560-
561- for _ in 0 ..remove_indent {
562- line_start. pop ( ) ;
563- }
564-
565- for _ in 0 ..quote_level {
566- line_start. push_str ( "> " ) ;
567- }
568-
569- line_start
570- }
571-
572577struct CommentRewrite < ' a > {
573578 result : String ,
574579 code_block_buffer : String ,
0 commit comments