Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for specifying a minimum width of ordered lists #465

Merged
merged 1 commit into from
Oct 22, 2024
Merged
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
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),
);
}