rustdoc_to_markdown: Recognize Rust code blocks (#12458)

This PR makes it so Rust code blocks are recognized and
syntax-highlighted when converting from rustdoc to Markdown.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-05-29 18:57:20 -04:00 committed by GitHub
parent b8d9713b4f
commit a5011996fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -137,7 +137,23 @@ impl MarkdownWriter {
self.push_str("`") self.push_str("`")
} }
} }
"pre" => self.push_str("\n```\n"), "pre" => {
let attrs = tag.attrs.borrow();
let classes = attrs
.iter()
.find(|attr| attr.name.local.to_string() == "class")
.map(|attr| {
attr.value
.split(' ')
.map(|class| class.trim())
.collect::<Vec<_>>()
})
.unwrap_or_default();
let is_rust = classes.into_iter().any(|class| class == "rust");
let language = if is_rust { "rs" } else { "" };
self.push_str(&format!("\n```{language}\n"))
}
"ul" | "ol" => self.push_newline(), "ul" | "ol" => self.push_newline(),
"li" => self.push_str("- "), "li" => self.push_str("- "),
"summary" => { "summary" => {