1 //! Transforms markdown
2 use ide_db::rust_doc::is_rust_fence;
4 const RUSTDOC_FENCES: [&str; 2] = ["```", "~~~"];
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) = RUSTDOC_FENCES.into_iter().find_map(|fence| line.strip_prefix(fence))
18 in_code_block ^= true;
21 is_rust = is_rust_fence(header);
30 let trimmed = line.trim_start();
31 if trimmed.starts_with("##") {
36 processed_lines.push(line);
38 processed_lines.join("\n")
41 fn code_line_ignored_by_rustdoc(line: &str) -> bool {
42 let trimmed = line.trim();
43 trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t")
51 fn test_format_docs_adds_rust() {
52 let comment = "```\nfn some_rust() {}\n```";
53 assert_eq!(format_docs(comment), "```rust\nfn some_rust() {}\n```");
57 fn test_format_docs_handles_plain_text() {
58 let comment = "```text\nthis is plain text\n```";
59 assert_eq!(format_docs(comment), "```text\nthis is plain text\n```");
63 fn test_format_docs_handles_non_rust() {
64 let comment = "```sh\nsupposedly shell code\n```";
65 assert_eq!(format_docs(comment), "```sh\nsupposedly shell code\n```");
69 fn test_format_docs_handles_rust_alias() {
70 let comment = "```ignore\nlet z = 55;\n```";
71 assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
75 fn test_format_docs_handles_complex_code_block_attrs() {
76 let comment = "```rust,no_run\nlet z = 55;\n```";
77 assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
81 fn test_format_docs_handles_error_codes() {
82 let comment = "```compile_fail,E0641\nlet b = 0 as *const _;\n```";
83 assert_eq!(format_docs(comment), "```rust\nlet b = 0 as *const _;\n```");
87 fn test_format_docs_skips_comments_in_rust_block() {
89 "```rust\n # skip1\n# skip2\n#stay1\nstay2\n#\n #\n # \n #\tskip3\n\t#\t\n```";
90 assert_eq!(format_docs(comment), "```rust\n#stay1\nstay2\n```");
94 fn test_format_docs_does_not_skip_lines_if_plain_text() {
96 "```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```";
99 "```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```",
104 fn test_format_docs_keeps_comments_outside_of_rust_block() {
105 let comment = " # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t";
106 assert_eq!(format_docs(comment), comment);
110 fn test_format_docs_preserves_newlines() {
111 let comment = "this\nis\nmultiline";
112 assert_eq!(format_docs(comment), comment);
116 fn test_code_blocks_in_comments_marked_as_rust() {
117 let comment = r#"```rust
126 format_docs(comment),
127 "```rust\nfn main(){}\n```\nSome comment.\n```rust\nlet a = 1;\n```"
132 fn test_code_blocks_in_comments_marked_as_text() {
133 let comment = r#"```text
143 format_docs(comment),
144 "```text\nfiller\ntext\n```\nSome comment.\n```rust\nlet a = 1;\n```"
149 fn test_format_docs_handles_escape_double_hashes() {
150 let comment = r#"```rust
155 assert_eq!(format_docs(comment), "```rust\nlet s = \"foo\n# bar # baz\";\n```");