Skip to content

Commit

Permalink
feat!: correctly round-trip code blocks with no trailing newline
Browse files Browse the repository at this point in the history
  • Loading branch information
notriddle committed Oct 4, 2024
1 parent 8a9573d commit db76794
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ where
}
if last_was_text_without_trailing_newline {
formatter.write_char('\n')?;
padding(formatter, &state.padding)?;
}
match state.code_block {
Some(CodeBlockKind::Fenced) => {
Expand Down
30 changes: 30 additions & 0 deletions tests/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,36 @@ mod end {
assert_eq!(es([Start(Tag::CodeBlock(CodeBlockKind::Fenced("".into()))), End(TagEnd::CodeBlock)]), "\n````\n````");
}
#[test]
fn codeblock_in_list_item() {
assert_eq!(es([
Start(Tag::List(None)),
Start(Tag::Item),
Start(Tag::CodeBlock(CodeBlockKind::Fenced("".into()))),
Text("foo".into()),
End(TagEnd::CodeBlock),
End(TagEnd::Item),
End(TagEnd::List(false)),
Start(Tag::Paragraph),
Text("bar".into()),
End(TagEnd::Paragraph),
]), "* \n ````\n foo\n ````\n\nbar");
}
#[test]
fn codeblock_indented_in_list_item() {
assert_eq!(es([
Start(Tag::List(None)),
Start(Tag::Item),
Start(Tag::CodeBlock(CodeBlockKind::Indented)),
Text("foo".into()),
End(TagEnd::CodeBlock),
End(TagEnd::Item),
End(TagEnd::List(false)),
Start(Tag::Paragraph),
Text("bar".into()),
End(TagEnd::Paragraph),
]), "* \n foo\n \n\nbar");
}
#[test]
fn footnote_definition() {
assert_eq!(s(End(TagEnd::FootnoteDefinition)), "");
}
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/snapshots/stupicat-indented-code-block
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ codeblock:

fn main() {
println!("Hello, world!");
}
}

2 changes: 1 addition & 1 deletion tests/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ mod codeblock {
assert_eq!(
fmts_both(" <!-- foo -->\n\n <!-- foo -->"),
(
" <!-- foo -->\n\n <!-- foo -->\n".into(),
" <!-- foo -->\n\n <!-- foo -->\n ".into(),
State {
newlines_before_start: 2,
..Default::default()
Expand Down

0 comments on commit db76794

Please sign in to comment.