Skip to content

Commit

Permalink
Fix padded code (text) in mdast
Browse files Browse the repository at this point in the history
Closes GH-122.
Closes GH-123.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
yshavit authored Aug 13, 2024
1 parent 690a0b1 commit 6144c58
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/to_mdast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,16 @@ fn on_exit_raw_text(context: &mut CompileContext) -> Result<(), message::Message
}
}

let value_bytes = value.as_bytes();
if value.len() > 2
&& value_bytes[0] == b' '
&& value_bytes[value.len() - 1] == b' '
&& !value_bytes.iter().all(|b| *b == b' ')
{
value.remove(0);
value.pop();
}

match context.tail_mut() {
Node::InlineCode(node) => node.value = value,
Node::InlineMath(node) => node.value = value,
Expand Down
30 changes: 30 additions & 0 deletions tests/code_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,35 @@ fn code_text() -> Result<(), message::Message> {
"should support code (text) as `InlineCode`s in mdast"
);

assert_eq!(
to_mdast("` alpha `", &Default::default())?,
Node::Root(Root {
children: vec![Node::Paragraph(Paragraph {
children: vec![Node::InlineCode(InlineCode {
value: " alpha".into(),
position: Some(Position::new(1, 1, 0, 1, 11, 10))
}),],
position: Some(Position::new(1, 1, 0, 1, 11, 10))
})],
position: Some(Position::new(1, 1, 0, 1, 11, 10))
}),
"should strip one space from each side of `InlineCode` if the value starts and ends with space"
);

assert_eq!(
to_mdast("` `", &Default::default())?,
Node::Root(Root {
children: vec![Node::Paragraph(Paragraph {
children: vec![Node::InlineCode(InlineCode {
value: " ".into(),
position: Some(Position::new(1, 1, 0, 1, 6, 5))
}),],
position: Some(Position::new(1, 1, 0, 1, 6, 5))
})],
position: Some(Position::new(1, 1, 0, 1, 6, 5))
}),
"should not strip any whitespace if `InlineCode` is all whitespace"
);

Ok(())
}

0 comments on commit 6144c58

Please sign in to comment.