From 353270a9e297a7abe7ac9d46b1684e7f0d1cf0ed Mon Sep 17 00:00:00 2001 From: edwar4rd Date: Sat, 7 Sep 2024 01:04:13 +0800 Subject: [PATCH] cm: add option for specifying a minimum width of ordered lists --- src/cm.rs | 18 ++++++++++++++---- src/parser/mod.rs | 16 ++++++++++++++++ src/tests/commonmark.rs | 10 +++++----- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/cm.rs b/src/cm.rs index fc15a1ad..9d46d387 100644 --- a/src/cm.rs +++ b/src/cm.rs @@ -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() }; @@ -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(); } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index fb9d4652..fc81c157 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -909,6 +909,22 @@ pub struct RenderOptions { /// "\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] diff --git a/src/tests/commonmark.rs b/src/tests/commonmark.rs index 5fb0a268..8add8c12 100644 --- a/src/tests/commonmark.rs +++ b/src/tests/commonmark.rs @@ -64,10 +64,10 @@ 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, ); @@ -75,7 +75,7 @@ fn commonmark_relist() { 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), ); }