Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::prelude::*;
use crate::shared::TextPrintMode;
use biome_formatter::write;
use biome_markdown_syntax::{MdFencedCodeBlock, MdFencedCodeBlockFields};
use biome_rowan::TextSize;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatMdFencedCodeBlock;
Expand All @@ -18,18 +19,15 @@ impl FormatNodeRule<MdFencedCodeBlock> for FormatMdFencedCodeBlock {
} = node.as_fields();

let l_fence = l_fence?;
let fence_text = l_fence.text();
// SAFETY: fence_text has at least one character.
let fence_char = fence_text.as_bytes()[0] as char;

// Compute the minimum fence length needed (CommonMark §4.5).
// The fence must be strictly longer than any same-character sequence
// in the content, otherwise the inner sequence would be parsed as a
// closing fence. E.g. if the content contains ``` (3 backticks),
// the outer fence needs at least 4.
let max_inner = longest_fence_char_sequence(node, fence_char);
let max_inner = longest_fence_char_sequence(node, '`');
let fence_len = (max_inner + 1).max(3);
let normalized_fence: String = std::iter::repeat_n(fence_char, fence_len).collect();
let normalized_fence: String = std::iter::repeat_n('`', fence_len).collect();

write!(
f,
Expand Down Expand Up @@ -60,6 +58,8 @@ impl FormatNodeRule<MdFencedCodeBlock> for FormatMdFencedCodeBlock {
&text(&normalized_fence, r_fence.text_trimmed_range().start())
)]
)?;
} else {
write!(f, [text(&normalized_fence, TextSize::default())])?;
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl FormatNodeRule<MdHardLine> for FormatMdHardLine {

if self.print_mode.is_pristine() {
// We intentionally format this code as is
return format_verbatim_node(node.syntax()).fmt(f);
return token.format().fmt(f);
}

let text_content = token.text();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::markdown::auxiliary::paragraph::FormatMdParagraphOptions;
use crate::prelude::*;
use crate::shared::{TextPrintMode, TrimMode};
use crate::verbatim::format_verbatim_node;
use biome_formatter::write;
use biome_markdown_syntax::{MdHeader, MdHeaderFields};
use biome_rowan::AstNode;
Expand All @@ -17,9 +16,7 @@ impl FormatNodeRule<MdHeader> for FormatMdHeader {
after,
} = node.as_fields();

write!(f, [format_verbatim_node(indent.syntax())])?;

write!(f, [before.format()])?;
write!(f, [indent.format(), before.format()])?;

if let Some(content) = content {
write!(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::prelude::*;
use biome_markdown_syntax::MdIndentToken;
use biome_rowan::AstNode;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatMdIndentToken;
impl FormatNodeRule<MdIndentToken> for FormatMdIndentToken {
fn fmt_fields(&self, node: &MdIndentToken, f: &mut MarkdownFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
node.md_indent_char_token().format().fmt(f)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
use crate::markdown::auxiliary::textual::FormatMdTextualOptions;
use crate::prelude::*;
use crate::shared::{TextPrintMode, TrimMode};
use biome_markdown_syntax::MdCodeNameList;

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatMdCodeNameList;
impl FormatRule<MdCodeNameList> for FormatMdCodeNameList {
type Context = MarkdownFormatContext;
fn fmt(&self, node: &MdCodeNameList, f: &mut MarkdownFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let mut joiner = f.join();

for entry in node.iter() {
joiner.entry(&entry.format().with_options(FormatMdTextualOptions {
print_mode: TextPrintMode::Trim(TrimMode::All),
should_remove: false,
trim_start: true,
}));
}

joiner.finish()
}
}
6 changes: 5 additions & 1 deletion crates/biome_markdown_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use biome_markdown_parser::parse_markdown;
#[ignore]
#[test]
fn quick_test() {
let source = "[*foo* bar][]
let source = "~~~~
aaa
~~~
~~~~

";
let parse = parse_markdown(source);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
```js
```js
console.log("hello");
```

```
```
some code
```

```rust
fn main() {}
```

```rust,ignore
fn main() {}
```

```rust, ignore expect_diagnostics
fn main() {}
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,26 @@ info: markdown/fenced_code_block_info_string.md
# Input

```md
```js
```js
console.log("hello");
```

```
```
some code
```

```rust
fn main() {}
```

```rust,ignore
fn main() {}
```

```rust, ignore expect_diagnostics
fn main() {}
```

```


Expand All @@ -36,4 +44,12 @@ some code
fn main() {}
```

```rust,ignore
fn main() {}
```

```rust, ignore expect_diagnostics
fn main() {}
```

```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like a regression

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed it

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ console.log ( "hello world" );
```diff
--- Prettier
+++ Biome
@@ -1,6 +1,6 @@
## plain js block

-```js
+```js
console.log ( "hello world" );
```

@@ -18,6 +18,6 @@

## js block with meta and extra spaces (only the first set of spaces should be changed)
Expand All @@ -53,7 +61,7 @@ console.log ( "hello world" );
```md
## plain js block

```js
```js
console.log ( "hello world" );
```

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@ bar
@@ -1,7 +1,5 @@
## foo
-
-```
+~~~
```
bar
-```
```
-
+~~~
# baz
```

# Output

```md
## foo
~~~
```
bar
~~~
```
# baz
```

This file was deleted.

Loading
Loading