Visitar URL original
Fix markdown formatting on stdin, add stdin test · astral-sh/ruff@42b2ed4 · GitHub
Skip to content

Commit 42b2ed4

Browse files
committed
Fix markdown formatting on stdin, add stdin test
1 parent c947793 commit 42b2ed4

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

‎crates/ruff/tests/cli/format.rs‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,3 +2478,92 @@ fn markdown_formatting_preview_enabled() -> Result<()> {
24782478
"#);
24792479
Ok(())
24802480
}
2481+
2482+
#[test]
2483+
fn markdown_formatting_stdin() -> Result<()> {
2484+
let test = CliTest::new()?;
2485+
2486+
let fixtures = Path::new("resources").join("test").join("fixtures");
2487+
let unformatted = fs::read(fixtures.join("unformatted.md")).unwrap();
2488+
assert_cmd_snapshot!(test.format_command()
2489+
.args(["--isolated", "--preview", "--stdin-filename", "unformatted.md"])
2490+
.arg("-")
2491+
.pass_stdin(unformatted), @r#"
2492+
success: true
2493+
exit_code: 0
2494+
----- stdout -----
2495+
This is a markdown document with some fenced code blocks with unformatted code.
2496+
2497+
Unlabeled Python code:
2498+
2499+
```
2500+
print( "hello" )
2501+
```
2502+
2503+
Unlabeled Rust code:
2504+
2505+
```
2506+
fn thing() {
2507+
println!( "hello" );
2508+
}
2509+
```
2510+
2511+
Labeled Python code:
2512+
2513+
```py
2514+
print("hello")
2515+
2516+
2517+
def foo():
2518+
pass
2519+
```
2520+
2521+
Labeled Python stub:
2522+
2523+
```pyi
2524+
print("hello")
2525+
2526+
def foo():
2527+
pass
2528+
```
2529+
2530+
Labeled Rust code:
2531+
```rust
2532+
fn thing() {
2533+
println!( "hello" );
2534+
}
2535+
```
2536+
2537+
Indented code blocks should also work:
2538+
2539+
* List item
2540+
2541+
```py
2542+
print("hello")
2543+
```
2544+
2545+
Block quoted code blocks may not be supported:
2546+
2547+
> Quoted text
2548+
>
2549+
> ```py
2550+
> print( "hello" )
2551+
> ```
2552+
2553+
2554+
Blacken-docs supports ignore directives:
2555+
2556+
<!-- blacken-docs:off -->
2557+
```py
2558+
print("hello")
2559+
```
2560+
<!-- blacken-docs:on -->
2561+
2562+
```py
2563+
print("hello")
2564+
```
2565+
2566+
----- stderr -----
2567+
"#);
2568+
Ok(())
2569+
}

‎crates/ruff_linter/src/source_kind.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ impl SourceKind {
130130
Ok(notebook
131131
.is_python_notebook()
132132
.then_some(Self::IpyNotebook(Box::new(notebook))))
133+
} else if source_type.is_markdown() {
134+
Ok(Some(Self::Markdown(source_code)))
133135
} else {
134136
Ok(Some(Self::Python(source_code)))
135137
}

0 commit comments

Comments
 (0)