Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -26,7 +26,7 @@ impl FormatNodeRule<MdInlineImage> for FormatMdInlineImage {
l_brack_token.format(),
alt.format()
.with_options(FormatMdFormatInlineItemListOptions {
print_mode: TextPrintMode::Trim(TrimMode::All),
print_mode: TextPrintMode::trim_all(),
keep_fences_in_italics: false
}),
r_brack_token.format(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::markdown::lists::inline_item_list::FormatMdFormatInlineItemListOptions;
use crate::prelude::*;
use crate::shared::{TextPrintMode, TrimMode};
use crate::shared::TextPrintMode;
use biome_formatter::write;
use biome_markdown_syntax::{MdInlineLink, MdInlineLinkFields};

Expand All @@ -24,15 +24,15 @@ impl FormatNodeRule<MdInlineLink> for FormatMdInlineLink {
l_brack_token.format(),
text.format()
.with_options(FormatMdFormatInlineItemListOptions {
print_mode: TextPrintMode::Trim(TrimMode::All),
print_mode: TextPrintMode::trim_all(),
keep_fences_in_italics: false
}),
r_brack_token.format(),
l_paren_token.format(),
destination
.format()
.with_options(FormatMdFormatInlineItemListOptions {
print_mode: TextPrintMode::Trim(TrimMode::All),
print_mode: TextPrintMode::trim_all(),
keep_fences_in_italics: false
})
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::markdown::lists::inline_item_list::FormatMdFormatInlineItemListOptions;
use crate::prelude::*;
use crate::shared::{TextPrintMode, TrimMode};
use crate::shared::TextPrintMode;
use biome_markdown_syntax::{MdLinkDestination, MdLinkDestinationFields};

#[derive(Debug, Clone, Default)]
Expand All @@ -12,7 +12,7 @@ impl FormatNodeRule<MdLinkDestination> for FormatMdLinkDestination {
content
.format()
.with_options(FormatMdFormatInlineItemListOptions {
print_mode: TextPrintMode::Trim(TrimMode::All),
print_mode: TextPrintMode::trim_all(),
keep_fences_in_italics: false,
})
.fmt(f)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::markdown::lists::inline_item_list::FormatMdFormatInlineItemListOptions;
use crate::prelude::*;
use crate::shared::{TextPrintMode, TrimMode};
use crate::shared::TextPrintMode;
use biome_formatter::write;
use biome_markdown_syntax::{MdLinkTitle, MdLinkTitleFields};

Expand All @@ -15,7 +15,7 @@ impl FormatNodeRule<MdLinkTitle> for FormatMdLinkTitle {
[content
.format()
.with_options(FormatMdFormatInlineItemListOptions {
print_mode: TextPrintMode::Trim(TrimMode::All),
print_mode: TextPrintMode::trim_all(),
keep_fences_in_italics: false
})]
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::markdown::lists::inline_item_list::FormatMdFormatInlineItemListOptions;
use crate::prelude::*;
use crate::shared::{TextPrintMode, TrimMode};
use crate::shared::TextPrintMode;
use biome_formatter::write;
use biome_markdown_syntax::{MdReferenceLink, MdReferenceLinkFields};

Expand All @@ -21,7 +21,7 @@ impl FormatNodeRule<MdReferenceLink> for FormatMdReferenceLink {
l_brack_token.format(),
text.format()
.with_options(FormatMdFormatInlineItemListOptions {
print_mode: TextPrintMode::Trim(TrimMode::All),
print_mode: TextPrintMode::trim_all(),
keep_fences_in_italics: true
}),
r_brack_token.format(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::markdown::lists::inline_item_list::FormatMdFormatInlineItemListOptions;
use crate::prelude::*;
use crate::shared::{TextPrintMode, TrimMode};
use crate::shared::TextPrintMode;
use biome_formatter::write;
use biome_markdown_syntax::{MdSetextHeader, MdSetextHeaderFields};

Expand Down Expand Up @@ -31,7 +31,7 @@ impl FormatNodeRule<MdSetextHeader> for FormatMdSetextHeader {
content
.format()
.with_options(FormatMdFormatInlineItemListOptions {
print_mode: TextPrintMode::Trim(TrimMode::All),
print_mode: TextPrintMode::trim_all(),
keep_fences_in_italics: false
}),
format_removed(&underline_token)
Expand Down
14 changes: 10 additions & 4 deletions crates/biome_markdown_formatter/src/markdown/auxiliary/textual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use biome_markdown_syntax::{MdTextual, MdTextualFields};
#[derive(Debug, Clone, Default)]
pub(crate) struct FormatMdTextual {
should_remove: bool,
trim_start: bool,
print_mode: TextPrintMode,
}
impl FormatNodeRule<MdTextual> for FormatMdTextual {
Expand Down Expand Up @@ -70,7 +69,16 @@ impl FormatNodeRule<MdTextual> for FormatMdTextual {
)]
)
}
} else if self.trim_start {
} else if self.print_mode.is_all() {
let trimmed_text = value_token.text().trim_start().trim_end();
write!(
f,
[format_replaced(
&value_token,
&text(trimmed_text, value_token.text_trimmed_range().start())
)]
)
} else if self.print_mode.is_start() {
let trimmed_text = value_token.text().trim_start();
write!(
f,
Expand All @@ -88,7 +96,6 @@ impl FormatNodeRule<MdTextual> for FormatMdTextual {
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct FormatMdTextualOptions {
pub(crate) should_remove: bool,
pub(crate) trim_start: bool,
pub(crate) print_mode: TextPrintMode,
}

Expand All @@ -97,7 +104,6 @@ impl FormatRuleWithOptions<MdTextual> for FormatMdTextual {

fn with_options(mut self, options: Self::Options) -> Self {
self.should_remove = options.should_remove;
self.trim_start = options.trim_start;
self.print_mode = options.print_mode;
self
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
use crate::markdown::auxiliary::textual::FormatMdTextualOptions;
use crate::prelude::*;
use crate::shared::TextPrintMode;
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_all(),
should_remove: false,
}));
}

joiner.finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ impl FormatRule<MdInlineItemList> for FormatMdInlineItemList {
f,
[text.format().with_options(FormatMdTextualOptions {
should_remove: true,
trim_start: false,
..Default::default()
})]
)
Expand All @@ -53,7 +52,6 @@ impl FormatRule<MdInlineItemList> for FormatMdInlineItemList {
[
text.format().with_options(FormatMdTextualOptions {
should_remove: true,
trim_start: false,
..Default::default()
}),
hard_line_break()
Expand All @@ -65,8 +63,11 @@ impl FormatRule<MdInlineItemList> for FormatMdInlineItemList {
} else {
joiner.entry(&text.format().with_options(FormatMdTextualOptions {
should_remove: false,
trim_start: self.print_mode.is_start() && index == 0,
..Default::default()
print_mode: if self.print_mode.is_start() && index == 0 {
self.print_mode
} else {
TextPrintMode::default()
},
}));
}
}
Expand Down Expand Up @@ -178,14 +179,13 @@ impl FormatMdInlineItemList {
AnyMdInline::MdTextual(text) => {
joiner.entry(&text.format().with_options(FormatMdTextualOptions {
should_remove: true,
trim_start: true,
..Default::default()
print_mode: TextPrintMode::trim_start(),
}));
}
AnyMdInline::MdHardLine(hard_line) => {
joiner.entry(&hard_line.format().with_options(
FormatMdFormatHardLineOptions {
print_mode: TextPrintMode::Trim(TrimMode::All),
print_mode: TextPrintMode::trim_all(),
},
));
}
Expand Down
8 changes: 8 additions & 0 deletions crates/biome_markdown_formatter/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,12 @@ impl TextPrintMode {
pub(crate) const fn is_clean(&self) -> bool {
matches!(self, Self::Clean)
}

pub(crate) const fn trim_start() -> Self {
Self::Trim(TrimMode::Start)
}

pub(crate) const fn trim_all() -> Self {
Self::Trim(TrimMode::All)
}
}
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
Expand Up @@ -37,13 +37,13 @@ console.log("hello world!");
````
~~~~
```

~~~python
def hello():
pass
~~~

~~~~
```
```
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() {}
```
Loading
Loading