From 6fe665ab947fa00391e231818f718e4554e35f37 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Thu, 30 May 2024 16:06:21 -0400 Subject: [PATCH] rustdoc_to_markdown: Support bold and italics (#12501) This PR extends `rustdoc_to_markdown` with support for bold and italic text. Release Notes: - N/A --- .../src/markdown_writer.rs | 12 +++++++++-- .../src/rustdoc_to_markdown.rs | 21 ++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/rustdoc_to_markdown/src/markdown_writer.rs b/crates/rustdoc_to_markdown/src/markdown_writer.rs index a1485f0d31..2d74911072 100644 --- a/crates/rustdoc_to_markdown/src/markdown_writer.rs +++ b/crates/rustdoc_to_markdown/src/markdown_writer.rs @@ -132,8 +132,12 @@ impl MarkdownWriter { fn start_tag(&mut self, tag: &HtmlElement) -> StartTagOutcome { if tag.is_inline() && self.is_inside("p") { - if !self.markdown.ends_with(' ') { - self.push_str(" "); + if let Some(parent) = self.current_element_stack.iter().last() { + if !parent.is_inline() { + if !self.markdown.ends_with(' ') { + self.push_str(" "); + } + } } } @@ -146,6 +150,8 @@ impl MarkdownWriter { "h5" => self.push_str("\n\n##### "), "h6" => self.push_str("\n\n###### "), "p" => self.push_blank_line(), + "strong" => self.push_str("**"), + "em" => self.push_str("_"), "code" => { if !self.is_inside("pre") { self.push_str("`"); @@ -219,6 +225,8 @@ impl MarkdownWriter { fn end_tag(&mut self, tag: &HtmlElement) { match tag.tag.as_str() { "h1" | "h2" | "h3" | "h4" | "h5" | "h6" => self.push_str("\n\n"), + "strong" => self.push_str("**"), + "em" => self.push_str("_"), "code" => { if !self.is_inside("pre") { 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 456b130fbe..54cf11c587 100644 --- a/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs +++ b/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs @@ -114,7 +114,7 @@ mod tests { let expected = indoc! {" ## Serde - Serde is a framework for serializing and deserializing Rust data structures efficiently and generically. + Serde is a framework for _**ser**_ializing and _**de**_serializing Rust data structures efficiently and generically. The Serde ecosystem consists of data structures that know how to serialize and deserialize themselves along with data formats that know how to serialize and deserialize other things. Serde provides the layer by which these two groups interact with each other, allowing any supported data structure to be serialized and deserialized using any supported data format. @@ -132,6 +132,25 @@ mod tests { ) } + #[test] + fn test_styled_text() { + let html = indoc! {r#" +

This text is bolded.

+

This text is italicized.

+ "#}; + let expected = indoc! {" + This text is **bolded**. + + This text is _italicized_. + "} + .trim(); + + assert_eq!( + convert_rustdoc_to_markdown(html.as_bytes()).unwrap(), + expected + ) + } + #[test] fn test_rust_code_block() { let html = indoc! {r#"