Skip to content

Commit

Permalink
cm: add option for specifying a minimum width of ordered lists
Browse files Browse the repository at this point in the history
  • Loading branch information
edwar4rd committed Sep 6, 2024
1 parent 1a33c63 commit d18616b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
18 changes: 14 additions & 4 deletions src/cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,16 +473,22 @@ impl<'a, 'o, 'c> CommonMarkFormatter<'a, 'o, 'c> {
let list_delim = parent.delimiter;
write!(
listmarker,
"{}{}{}",
"{}{} ",
list_number,
if list_delim == ListDelimType::Paren {
")"
} else {
"."
},
if list_number < 10 { " " } else { " " }
}
)
.unwrap();
let mut current_len = listmarker.len();

while current_len < self.options.render.ol_width {
write!(listmarker, " ").unwrap();
current_len += 1;
}

listmarker.len()
};

Expand All @@ -498,7 +504,11 @@ impl<'a, 'o, 'c> CommonMarkFormatter<'a, 'o, 'c> {
write!(self.prefix, " ").unwrap();
}
} else {
let new_len = self.prefix.len() - marker_width;
let new_len = if self.prefix.len() > marker_width {
self.prefix.len() - marker_width
} else {
0
};
self.prefix.truncate(new_len);
self.cr();
}
Expand Down
17 changes: 17 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,23 @@ pub struct RenderOptions {
/// "<p><figure><img src=\"https://example.com/image.png\" alt=\"image\" title=\"this is an image\" /><figcaption>this is an image</figcaption></figure></p>\n");
/// ```
pub figure_with_caption: bool,

/// Render ordered list with a minimum marker width.
/// Having a width lower than 3 doesn't do anything.
///
/// ```rust
/// # use comrak::{markdown_to_commonmark, Options};
/// let mut options = Options::default();
/// let input = "1. Something";
///
/// assert_eq!(markdown_to_commonmark(input, &options),
/// "1. Something\n");
///
/// options.render.ol_width = 5;
/// assert_eq!(markdown_to_commonmark(input, &options),
/// "1. Something\n");
/// ```
pub ol_width: usize,
}

#[non_exhaustive]
Expand Down
10 changes: 5 additions & 5 deletions src/tests/commonmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@ fn wikilinks(markdown: &str, cm: &str) {
fn commonmark_relist() {
commonmark(
concat!("3. one\n", "5. two\n",),
// Note that right now we always include enough room for up to two
// digits. TODO: Ideally we determine the maximum digit length before
// getting this far.
concat!("3. one\n", "4. two\n",),
// Note that right now we always include enough room for up to an user
// defined number of digits. TODO: Ideally we determine the maximum
// digit length before getting this far.
concat!("3. one\n", "4. two\n",),
None,
);

let mut options = Options::default();
options.extension.tasklist = true;
commonmark(
concat!("3. [ ] one\n", "5. [ ] two\n",),
concat!("3. [ ] one\n", "4. [ ] two\n",),
concat!("3. [ ] one\n", "4. [ ] two\n",),
Some(&options),
);
}

0 comments on commit d18616b

Please sign in to comment.