Skip to content

Commit

Permalink
Merge pull request #486 from silverpill/fix-list-item
Browse files Browse the repository at this point in the history
Prevent panic in format_item
  • Loading branch information
kivikakk authored Nov 14, 2024
2 parents f4853af + 2ec6ec5 commit ce3e2ad
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,19 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
let marker_width = if parent.list_type == ListType::Bullet {
2
} else {
let last_stack = self.ol_stack.last_mut().unwrap();
let list_number = *last_stack;
if entering {
*last_stack += 1;
}
let list_number = if let Some(last_stack) = self.ol_stack.last_mut() {
let list_number = *last_stack;
if entering {
*last_stack += 1;
};
list_number
} else {
match node.data.borrow().value {
NodeValue::Item(ref ni) => ni.start,
NodeValue::TaskItem(_) => parent.start,
_ => unreachable!(),
}
};
let list_delim = parent.delimiter;
write!(
listmarker,
Expand Down
29 changes: 28 additions & 1 deletion src/tests/commonmark.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cell::RefCell;

use self::nodes::{Ast, LineColumn};
use self::nodes::{Ast, LineColumn, ListType, NodeList};

use super::*;
use ntest::test_case;
Expand Down Expand Up @@ -41,6 +41,33 @@ fn commonmark_avoids_spurious_backslash() {
);
}

#[test]
fn commonmark_renders_single_list_item() {
let arena = Arena::new();
let options = Options::default();
let empty = LineColumn { line: 0, column: 0 };
let ast = |val: NodeValue| arena.alloc(AstNode::new(RefCell::new(Ast::new(val, empty))));
let list_options = NodeList {
list_type: ListType::Ordered,
start: 1,
..Default::default()
};
let list = ast(NodeValue::List(list_options));
let item = ast(NodeValue::Item(list_options));
let p = ast(NodeValue::Paragraph);
p.append(ast(NodeValue::Text("Item 1".to_owned())));
item.append(p);
list.append(item);
let mut output = vec![];
cm::format_document(item, &options, &mut output).unwrap();
compare_strs(
&String::from_utf8(output).unwrap(),
"1. Item 1\n",
"rendered",
"<synthetic>",
);
}

#[test_case("$$x^2$$ and $1 + 2$ and $`y^2`$", "$$x^2$$ and $1 + 2$ and $`y^2`$\n")]
#[test_case("$$\nx^2\n$$", "$$\nx^2\n$$\n")]
#[test_case("```math\nx^2\n```", "``` math\nx^2\n```\n")]
Expand Down

0 comments on commit ce3e2ad

Please sign in to comment.