Skip to content

Commit ce3e2ad

Browse files
authored
Merge pull request #486 from silverpill/fix-list-item
Prevent panic in format_item
2 parents f4853af + 2ec6ec5 commit ce3e2ad

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/cm.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,19 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
465465
let marker_width = if parent.list_type == ListType::Bullet {
466466
2
467467
} else {
468-
let last_stack = self.ol_stack.last_mut().unwrap();
469-
let list_number = *last_stack;
470-
if entering {
471-
*last_stack += 1;
472-
}
468+
let list_number = if let Some(last_stack) = self.ol_stack.last_mut() {
469+
let list_number = *last_stack;
470+
if entering {
471+
*last_stack += 1;
472+
};
473+
list_number
474+
} else {
475+
match node.data.borrow().value {
476+
NodeValue::Item(ref ni) => ni.start,
477+
NodeValue::TaskItem(_) => parent.start,
478+
_ => unreachable!(),
479+
}
480+
};
473481
let list_delim = parent.delimiter;
474482
write!(
475483
listmarker,

src/tests/commonmark.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::cell::RefCell;
22

3-
use self::nodes::{Ast, LineColumn};
3+
use self::nodes::{Ast, LineColumn, ListType, NodeList};
44

55
use super::*;
66
use ntest::test_case;
@@ -41,6 +41,33 @@ fn commonmark_avoids_spurious_backslash() {
4141
);
4242
}
4343

44+
#[test]
45+
fn commonmark_renders_single_list_item() {
46+
let arena = Arena::new();
47+
let options = Options::default();
48+
let empty = LineColumn { line: 0, column: 0 };
49+
let ast = |val: NodeValue| arena.alloc(AstNode::new(RefCell::new(Ast::new(val, empty))));
50+
let list_options = NodeList {
51+
list_type: ListType::Ordered,
52+
start: 1,
53+
..Default::default()
54+
};
55+
let list = ast(NodeValue::List(list_options));
56+
let item = ast(NodeValue::Item(list_options));
57+
let p = ast(NodeValue::Paragraph);
58+
p.append(ast(NodeValue::Text("Item 1".to_owned())));
59+
item.append(p);
60+
list.append(item);
61+
let mut output = vec![];
62+
cm::format_document(item, &options, &mut output).unwrap();
63+
compare_strs(
64+
&String::from_utf8(output).unwrap(),
65+
"1. Item 1\n",
66+
"rendered",
67+
"<synthetic>",
68+
);
69+
}
70+
4471
#[test_case("$$x^2$$ and $1 + 2$ and $`y^2`$", "$$x^2$$ and $1 + 2$ and $`y^2`$\n")]
4572
#[test_case("$$\nx^2\n$$", "$$\nx^2\n$$\n")]
4673
#[test_case("```math\nx^2\n```", "``` math\nx^2\n```\n")]

0 commit comments

Comments
 (0)