diff --git a/src/comment.rs b/src/comment.rs index 709031dda44..d9751d8730e 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -462,9 +462,9 @@ impl ItemizedBlock { fn get_marker_length(trimmed: &str) -> Option { // https://spec.commonmark.org/0.30/#bullet-list-marker or // https://spec.commonmark.org/0.30/#block-quote-marker - let itemized_start = ["* ", "- ", "> ", "+ "]; - if itemized_start.iter().any(|s| trimmed.starts_with(s)) { - return Some(2); // All items in `itemized_start` have length 2. + let itemized_start = ["* ", "- ", ">", "+ "]; + if let Some(s) = itemized_start.iter().find(|&&s| trimmed.starts_with(s)) { + return Some(s.len()); } // https://spec.commonmark.org/0.30/#ordered-list-marker, where at most 2 digits are @@ -492,13 +492,14 @@ impl ItemizedBlock { // Markdown blockquote start with a "> " if line.trim_start().starts_with('>') { - // remove the original +2 indent because there might be multiple nested block quotes + // remove the original +1 indent because there might be multiple nested block quotes // and it's easier to reason about the final indent by just taking the length // of the new line_start. We update the indent because it effects the max width // of each formatted line. - line_start = itemized_block_quote_start(line, line_start, 2); + line_start = itemized_block_quote_start(line, line_start, 1); indent = line_start.len(); } + Some(ItemizedBlock { lines: vec![line[indent..].to_string()], indent, @@ -551,18 +552,26 @@ impl ItemizedBlock { /// The original line_start likely contains indentation (whitespaces), which we'd like to /// replace with '> ' characters. fn itemized_block_quote_start(line: &str, mut line_start: String, remove_indent: usize) -> String { - let quote_level = line - .chars() - .take_while(|c| !c.is_alphanumeric()) - .fold(0, |acc, c| if c == '>' { acc + 1 } else { acc }); + let mut quote_level = 0; + let mut chars = line.trim_start().chars().peekable(); + + while chars.peek() == Some(&'>') { + chars.next(); + quote_level += 1; + // Skip all spaces after '>' + while chars.peek() == Some(&' ') { + chars.next(); + } + } for _ in 0..remove_indent { line_start.pop(); } for _ in 0..quote_level { - line_start.push_str("> ") + line_start.push_str("> "); } + line_start } diff --git a/tests/source/itemized-blocks/no_wrap.rs b/tests/source/itemized-blocks/no_wrap.rs index e5699e76684..4dcb2efdf50 100644 --- a/tests/source/itemized-blocks/no_wrap.rs +++ b/tests/source/itemized-blocks/no_wrap.rs @@ -79,3 +79,12 @@ fn func2() {} /// * `tag` is a tag that identifies the message type /// * `msg` is the (serialized) message fn func3() {} + +/// >>This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// >> This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > >This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > > This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > > This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > >= >> >>= >>> >>>= This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > > = >> >>= >>> >>>= This is a blockquote comment that is very long and should be wrapped according to max_width setting +fn func4() {} diff --git a/tests/source/itemized-blocks/wrap.rs b/tests/source/itemized-blocks/wrap.rs index 768461a43f9..98b5c9a15b3 100644 --- a/tests/source/itemized-blocks/wrap.rs +++ b/tests/source/itemized-blocks/wrap.rs @@ -87,3 +87,12 @@ fn func2() {} /// * `tag` is a tag that identifies the message type /// * `msg` is the (serialized) message fn func3() {} + +/// >>This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// >> This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > >This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > > This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > > This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > >= >> >>= >>> >>>= This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > > = >> >>= >>> >>>= This is a blockquote comment that is very long and should be wrapped according to max_width setting +fn func4() {} diff --git a/tests/target/itemized-blocks/no_wrap.rs b/tests/target/itemized-blocks/no_wrap.rs index 86818b44745..09508f6ae5b 100644 --- a/tests/target/itemized-blocks/no_wrap.rs +++ b/tests/target/itemized-blocks/no_wrap.rs @@ -79,3 +79,12 @@ fn func2() {} /// * `tag` is a tag that identifies the message type /// * `msg` is the (serialized) message fn func3() {} + +/// >>This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// >> This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > >This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > > This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > > This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > >= >> >>= >>> >>>= This is a blockquote comment that is very long and should be wrapped according to max_width setting +/// > > = >> >>= >>> >>>= This is a blockquote comment that is very long and should be wrapped according to max_width setting +fn func4() {} diff --git a/tests/target/itemized-blocks/wrap.rs b/tests/target/itemized-blocks/wrap.rs index 4826590ea59..8cdeed83fd4 100644 --- a/tests/target/itemized-blocks/wrap.rs +++ b/tests/target/itemized-blocks/wrap.rs @@ -147,3 +147,26 @@ fn func2() {} /// type /// * `msg` is the (serialized) message fn func3() {} + +/// >>This is a blockquote comment that is very +/// > > long and should be wrapped according to +/// > > max_width setting +/// >> This is a blockquote comment that is very +/// > > long and should be wrapped according to +/// > > max_width setting +/// > >This is a blockquote comment that is very +/// > > long and should be wrapped according to +/// > > max_width setting +/// > > This is a blockquote comment that is +/// > > very long and should be wrapped according +/// > > to max_width setting +/// > > This is a blockquote comment that is +/// > > very long and should be wrapped according +/// > > to max_width setting +/// > >=>> >>= >>> >>>= This is a blockquote +/// > > comment that is very long and should be +/// > > wrapped according to max_width setting +/// > > = >> >>= >>> >>>= This is a blockquote +/// > > comment that is very long and should be +/// > > wrapped according to max_width setting +fn func4() {}