Skip to content

Commit b7b9cd5

Browse files
committed
Fix: only shows one # when we encounter ##
1 parent 5051717 commit b7b9cd5

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

crates/rust-analyzer/src/markdown.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! Transforms markdown
2+
use std::borrow::Cow;
3+
24
use ide_db::helpers::rust_doc::is_rust_fence;
35

46
const RUSTDOC_FENCE: &str = "```";
@@ -8,8 +10,9 @@ pub(crate) fn format_docs(src: &str) -> String {
810
let mut in_code_block = false;
911
let mut is_rust = false;
1012

11-
for mut line in src.lines() {
12-
if in_code_block && is_rust && code_line_ignored_by_rustdoc(line) {
13+
for line in src.lines() {
14+
let mut line = Cow::from(line);
15+
if in_code_block && is_rust && code_line_ignored_by_rustdoc(&line) {
1316
continue;
1417
}
1518

@@ -20,11 +23,18 @@ pub(crate) fn format_docs(src: &str) -> String {
2023
is_rust = is_rust_fence(header);
2124

2225
if is_rust {
23-
line = "```rust";
26+
line = Cow::Borrowed("```rust");
2427
}
2528
}
2629
}
2730

31+
if in_code_block {
32+
let trimmed = line.trim();
33+
if trimmed.starts_with("##") {
34+
line = Cow::Owned(line.replacen("##", "#", 1));
35+
}
36+
}
37+
2838
processed_lines.push(line);
2939
}
3040
processed_lines.join("\n")
@@ -136,4 +146,14 @@ let a = 1;
136146
"```text\nfiller\ntext\n```\nSome comment.\n```rust\nlet a = 1;\n```"
137147
);
138148
}
149+
150+
#[test]
151+
fn test_format_docs_handles_escape_double_hashes() {
152+
let comment = r#"```rust
153+
let s = "foo
154+
## bar # baz";
155+
```"#;
156+
157+
assert_eq!(format_docs(comment), "```rust\nlet s = \"foo\n# bar # baz\";\n```");
158+
}
139159
}

0 commit comments

Comments
 (0)