Skip to content

Commit

Permalink
Merge pull request #465 from edwar4rd/ol-width
Browse files Browse the repository at this point in the history
Add option for specifying a minimum width of ordered lists
  • Loading branch information
kivikakk authored Oct 22, 2024
2 parents 96a64d1 + 353270a commit 5a8e449
Show file tree
Hide file tree
Showing 3 changed files with 35 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
16 changes: 16 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,22 @@ pub struct RenderOptions {
/// "<ul class=\"contains-task-list\">\n<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> Foo</li>\n</ul>\n");
/// ```
pub tasklist_classes: 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 5a8e449

Please sign in to comment.