1
1
//! Transforms markdown
2
+ use std:: borrow:: Cow ;
3
+
2
4
use ide_db:: helpers:: rust_doc:: is_rust_fence;
3
5
4
6
const RUSTDOC_FENCE : & str = "```" ;
@@ -8,8 +10,9 @@ pub(crate) fn format_docs(src: &str) -> String {
8
10
let mut in_code_block = false ;
9
11
let mut is_rust = false ;
10
12
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) {
13
16
continue ;
14
17
}
15
18
@@ -20,11 +23,18 @@ pub(crate) fn format_docs(src: &str) -> String {
20
23
is_rust = is_rust_fence ( header) ;
21
24
22
25
if is_rust {
23
- line = "```rust" ;
26
+ line = Cow :: Borrowed ( "```rust" ) ;
24
27
}
25
28
}
26
29
}
27
30
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
+
28
38
processed_lines. push ( line) ;
29
39
}
30
40
processed_lines. join ( "\n " )
@@ -136,4 +146,14 @@ let a = 1;
136
146
"```text\n filler\n text\n ```\n Some comment.\n ```rust\n let a = 1;\n ```"
137
147
) ;
138
148
}
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\n let s = \" foo\n # bar # baz\" ;\n ```" ) ;
158
+ }
139
159
}
0 commit comments