diff --git a/crates/rustdoc_to_markdown/src/markdown_writer.rs b/crates/rustdoc_to_markdown/src/markdown_writer.rs index b48a780c3558b17cbe9de893785c0f62537817d3..ffc910a6213cd0fbb9ab09a07214fd57801dfe27 100644 --- a/crates/rustdoc_to_markdown/src/markdown_writer.rs +++ b/crates/rustdoc_to_markdown/src/markdown_writer.rs @@ -148,10 +148,21 @@ impl MarkdownWriter { .collect::>() }) .unwrap_or_default(); - let is_rust = classes.into_iter().any(|class| class == "rust"); - let language = if is_rust { "rs" } else { "" }; + let is_rust = classes.iter().any(|class| class == &"rust"); + let language = is_rust + .then(|| "rs") + .or_else(|| { + classes.iter().find_map(|class| { + if let Some((_, language)) = class.split_once("language-") { + Some(language.trim()) + } else { + None + } + }) + }) + .unwrap_or(""); - self.push_str(&format!("\n```{language}\n")) + self.push_str(&format!("\n\n```{language}\n")) } "ul" | "ol" => self.push_newline(), "li" => self.push_str("- "), diff --git a/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs b/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs index ac34213e2c6ac28b63727954226cf870b07b8a38..7a2d52a4e6a4197af89b4a78881663679a7a8cc5 100644 --- a/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs +++ b/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs @@ -45,7 +45,7 @@ mod tests { use super::*; #[test] - fn test_code_blocks() { + fn test_rust_code_block() { let html = indoc! {r#"
use axum::extract::{Path, Query, Json};
             use std::collections::HashMap;
@@ -85,4 +85,36 @@ mod tests {
             expected
         )
     }
+
+    #[test]
+    fn test_toml_code_block() {
+        let html = indoc! {r##"
+            

ยงRequired dependencies

+

To use axum there are a few dependencies you have to pull in as well:

+
[dependencies]
+            axum = "<latest-version>"
+            tokio = { version = "<latest-version>", features = ["full"] }
+            tower = "<latest-version>"
+            
+ "##}; + let expected = indoc! {r#" + ## Required dependencies + + To use axum there are a few dependencies you have to pull in as well: + + ```toml + [dependencies] + axum = "" + tokio = { version = "", features = ["full"] } + tower = "" + + ``` + "#} + .trim(); + + assert_eq!( + convert_rustdoc_to_markdown(html.as_bytes()).unwrap(), + expected + ) + } }