1 //! Transforms markdown
3 const RUSTDOC_FENCE: &str = "```";
4 const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC: &[&str] = &[
18 pub(crate) fn format_docs(src: &str) -> String {
19 let mut processed_lines = Vec::new();
20 let mut in_code_block = false;
21 let mut is_rust = false;
23 for mut line in src.lines() {
24 if in_code_block && is_rust && code_line_ignored_by_rustdoc(line) {
28 if let Some(header) = line.strip_prefix(RUSTDOC_FENCE) {
29 in_code_block ^= true;
33 header.split(',').any(|sub| is_rust_specific_code_block_attribute(sub.trim()));
41 processed_lines.push(line);
43 processed_lines.join("\n")
46 fn is_rust_specific_code_block_attribute(attr: &str) -> bool {
47 if RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC.contains(&attr) {
50 attr.starts_with('E') && attr.len() == 5 && attr[1..].parse::<u32>().is_ok()
53 fn code_line_ignored_by_rustdoc(line: &str) -> bool {
54 let trimmed = line.trim();
55 trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t")
63 fn test_format_docs_adds_rust() {
64 let comment = "```\nfn some_rust() {}\n```";
65 assert_eq!(format_docs(comment), "```rust\nfn some_rust() {}\n```");
69 fn test_format_docs_handles_plain_text() {
70 let comment = "```text\nthis is plain text\n```";
71 assert_eq!(format_docs(comment), "```text\nthis is plain text\n```");
75 fn test_format_docs_handles_non_rust() {
76 let comment = "```sh\nsupposedly shell code\n```";
77 assert_eq!(format_docs(comment), "```sh\nsupposedly shell code\n```");
81 fn test_format_docs_handles_rust_alias() {
82 let comment = "```ignore\nlet z = 55;\n```";
83 assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
87 fn test_format_docs_handles_complex_code_block_attrs() {
88 let comment = "```rust,no_run\nlet z = 55;\n```";
89 assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
93 fn test_format_docs_handles_error_codes() {
94 let comment = "```compile_fail,E0641\nlet b = 0 as *const _;\n```";
95 assert_eq!(format_docs(comment), "```rust\nlet b = 0 as *const _;\n```");
99 fn test_format_docs_skips_comments_in_rust_block() {
101 "```rust\n # skip1\n# skip2\n#stay1\nstay2\n#\n #\n # \n #\tskip3\n\t#\t\n```";
102 assert_eq!(format_docs(comment), "```rust\n#stay1\nstay2\n```");
106 fn test_format_docs_does_not_skip_lines_if_plain_text() {
108 "```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```";
110 format_docs(comment),
111 "```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```",
116 fn test_format_docs_keeps_comments_outside_of_rust_block() {
117 let comment = " # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t";
118 assert_eq!(format_docs(comment), comment);
122 fn test_format_docs_preserves_newlines() {
123 let comment = "this\nis\nmultiline";
124 assert_eq!(format_docs(comment), comment);
128 fn test_code_blocks_in_comments_marked_as_rust() {
129 let comment = r#"```rust
138 format_docs(comment),
139 "```rust\nfn main(){}\n```\nSome comment.\n```rust\nlet a = 1;\n```"
144 fn test_code_blocks_in_comments_marked_as_text() {
145 let comment = r#"```text
155 format_docs(comment),
156 "```text\nfiller\ntext\n```\nSome comment.\n```rust\nlet a = 1;\n```"