diff --git a/crates/biome_markdown_formatter/src/markdown/auxiliary/hard_line.rs b/crates/biome_markdown_formatter/src/markdown/auxiliary/hard_line.rs index 5841f6981402..28f60443d5e9 100644 --- a/crates/biome_markdown_formatter/src/markdown/auxiliary/hard_line.rs +++ b/crates/biome_markdown_formatter/src/markdown/auxiliary/hard_line.rs @@ -1,7 +1,8 @@ use crate::prelude::*; use crate::shared::TextPrintMode; use biome_formatter::{FormatRuleWithOptions, write}; -use biome_markdown_syntax::MdHardLine; +use biome_markdown_syntax::{MarkdownSyntaxKind, MdHardLine}; +use biome_rowan::Direction; #[derive(Debug, Clone, Default)] pub(crate) struct FormatMdHardLine { @@ -28,6 +29,20 @@ impl FormatNodeRule for FormatMdHardLine { ] ) } else { + // Detect if the hard line break is the last one of the paragraph. + let is_last_hard_line = match node.syntax().siblings(Direction::Next).nth(1) { + None => true, + Some(s) => { + s.kind() == MarkdownSyntaxKind::MD_TEXTUAL && s.text_trimmed().is_empty() + } + }; + + if is_last_hard_line { + // Drop the two-space marker but keep a single newline so the + // paragraph still terminates on its own line. + return write!(f, [format_removed(&token), hard_line_break()]); + } + // Given two or more spaces in MdHardLine, only two spaces has semantic meaning // so we are adding back two spaces as required by the spec // https://spec.commonmark.org/0.31.2/#hard-line-break diff --git a/crates/biome_markdown_formatter/tests/specs/markdown/hard_line.md b/crates/biome_markdown_formatter/tests/specs/markdown/hard_line.md index ffbad94e0c83..f967e741e090 100644 --- a/crates/biome_markdown_formatter/tests/specs/markdown/hard_line.md +++ b/crates/biome_markdown_formatter/tests/specs/markdown/hard_line.md @@ -6,3 +6,9 @@ form no hard line here + +foo +bar with empty line after + +foo +bar without empty line after \ No newline at end of file diff --git a/crates/biome_markdown_formatter/tests/specs/markdown/hard_line.md.snap b/crates/biome_markdown_formatter/tests/specs/markdown/hard_line.md.snap index a5466203b521..1e9dfc46d71e 100644 --- a/crates/biome_markdown_formatter/tests/specs/markdown/hard_line.md.snap +++ b/crates/biome_markdown_formatter/tests/specs/markdown/hard_line.md.snap @@ -15,6 +15,11 @@ form no hard line here +foo +bar with empty line after + +foo +bar without empty line after ``` @@ -30,4 +35,8 @@ form no hard line here +foo +bar with empty line after +foo +bar without empty line after ``` diff --git a/crates/biome_markdown_formatter/tests/specs/prettier/markdown/spec/example-186.md.snap b/crates/biome_markdown_formatter/tests/specs/prettier/markdown/spec/example-186.md.snap deleted file mode 100644 index 40672012507a..000000000000 --- a/crates/biome_markdown_formatter/tests/specs/prettier/markdown/spec/example-186.md.snap +++ /dev/null @@ -1,31 +0,0 @@ ---- -source: crates/biome_formatter_test/src/snapshot_builder.rs -info: markdown/spec/example-186.md ---- - -# Input - -```md -aaa -bbb - -``` - - -# Prettier differences - -```diff ---- Prettier -+++ Biome -@@ -1,2 +1,2 @@ - aaa --bbb -+bbb -``` - -# Output - -```md -aaa -bbb -``` diff --git a/crates/biome_markdown_formatter/tests/specs/prettier/markdown/spec/example-609.md.snap b/crates/biome_markdown_formatter/tests/specs/prettier/markdown/spec/example-609.md.snap deleted file mode 100644 index 23aed5df912b..000000000000 --- a/crates/biome_markdown_formatter/tests/specs/prettier/markdown/spec/example-609.md.snap +++ /dev/null @@ -1,28 +0,0 @@ ---- -source: crates/biome_formatter_test/src/snapshot_builder.rs -info: markdown/spec/example-609.md ---- - -# Input - -```md -foo - -``` - - -# Prettier differences - -```diff ---- Prettier -+++ Biome -@@ -1 +1 @@ --foo -+foo -``` - -# Output - -```md -foo -```