1 //! Transforms markdown
2 use ide_db::helpers::rust_doc::is_rust_fence;
4 const RUSTDOC_FENCE: &str = "```";
6 pub(crate) fn format_docs(src: &str) -> String {
7 let mut processed_lines = Vec::new();
8 let mut in_code_block = false;
9 let mut is_rust = false;
11 for mut line in src.lines() {
12 if in_code_block && is_rust && code_line_ignored_by_rustdoc(line) {
16 if let Some(header) = line.strip_prefix(RUSTDOC_FENCE) {
17 in_code_block ^= true;
20 is_rust = is_rust_fence(header);
29 let trimmed = line.trim_start();
30 if trimmed.starts_with("##") {
35 processed_lines.push(line);
37 processed_lines.join("\n")
40 fn code_line_ignored_by_rustdoc(line: &str) -> bool {
41 let trimmed = line.trim();
42 trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t")
50 fn test_format_docs_adds_rust() {
51 let comment = "```\nfn some_rust() {}\n```";
52 assert_eq!(format_docs(comment), "```rust\nfn some_rust() {}\n```");
56 fn test_format_docs_handles_plain_text() {
57 let comment = "```text\nthis is plain text\n```";
58 assert_eq!(format_docs(comment), "```text\nthis is plain text\n```");
62 fn test_format_docs_handles_non_rust() {
63 let comment = "```sh\nsupposedly shell code\n```";
64 assert_eq!(format_docs(comment), "```sh\nsupposedly shell code\n```");
68 fn test_format_docs_handles_rust_alias() {
69 let comment = "```ignore\nlet z = 55;\n```";
70 assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
74 fn test_format_docs_handles_complex_code_block_attrs() {
75 let comment = "```rust,no_run\nlet z = 55;\n```";
76 assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
80 fn test_format_docs_handles_error_codes() {
81 let comment = "```compile_fail,E0641\nlet b = 0 as *const _;\n```";
82 assert_eq!(format_docs(comment), "```rust\nlet b = 0 as *const _;\n```");
86 fn test_format_docs_skips_comments_in_rust_block() {
88 "```rust\n # skip1\n# skip2\n#stay1\nstay2\n#\n #\n # \n #\tskip3\n\t#\t\n```";
89 assert_eq!(format_docs(comment), "```rust\n#stay1\nstay2\n```");
93 fn test_format_docs_does_not_skip_lines_if_plain_text() {
95 "```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```";
98 "```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```",
103 fn test_format_docs_keeps_comments_outside_of_rust_block() {
104 let comment = " # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t";
105 assert_eq!(format_docs(comment), comment);
109 fn test_format_docs_preserves_newlines() {
110 let comment = "this\nis\nmultiline";
111 assert_eq!(format_docs(comment), comment);
115 fn test_code_blocks_in_comments_marked_as_rust() {
116 let comment = r#"```rust
125 format_docs(comment),
126 "```rust\nfn main(){}\n```\nSome comment.\n```rust\nlet a = 1;\n```"
131 fn test_code_blocks_in_comments_marked_as_text() {
132 let comment = r#"```text
142 format_docs(comment),
143 "```text\nfiller\ntext\n```\nSome comment.\n```rust\nlet a = 1;\n```"
148 fn test_format_docs_handles_escape_double_hashes() {
149 let comment = r#"```rust
154 assert_eq!(format_docs(comment), "```rust\nlet s = \"foo\n# bar # baz\";\n```");