Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,9 @@ impl ItemizedBlock {
fn get_marker_length(trimmed: &str) -> Option<usize> {
// 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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 9 additions & 0 deletions tests/source/itemized-blocks/no_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
9 changes: 9 additions & 0 deletions tests/source/itemized-blocks/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
9 changes: 9 additions & 0 deletions tests/target/itemized-blocks/no_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
23 changes: 23 additions & 0 deletions tests/target/itemized-blocks/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Loading