>()
- })
- .unwrap_or_default()
- }
-
- /// Returns whether this [`HtmlElement`] has the specified class.
- pub fn has_class(&self, class: &str) -> bool {
- self.has_any_classes(&[class])
- }
-
- /// Returns whether this [`HtmlElement`] has any of the specified classes.
- pub fn has_any_classes(&self, classes: &[&str]) -> bool {
- self.attrs.borrow().iter().any(|attr| {
- attr.name.local.to_string() == "class"
- && attr
- .value
- .split(' ')
- .any(|class| classes.contains(&class.trim()))
- })
- }
-}
-
const RUSTDOC_ITEM_NAME_CLASS: &str = "item-name";
enum StartTagOutcome {
@@ -179,6 +131,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(" ");
+ }
+ }
+
match tag.tag.as_str() {
"head" | "script" | "nav" => return StartTagOutcome::Skip,
"h1" => self.push_str("\n\n# "),
@@ -187,6 +145,7 @@ impl MarkdownWriter {
"h4" => self.push_str("\n\n#### "),
"h5" => self.push_str("\n\n##### "),
"h6" => self.push_str("\n\n###### "),
+ "p" => self.push_blank_line(),
"code" => {
if !self.is_inside("pre") {
self.push_str("`");
@@ -305,14 +264,16 @@ impl MarkdownWriter {
return Ok(());
}
- let trimmed_text = text.trim_matches(|char| char == '\n' || char == '\r' || char == '§');
+ let text = text
+ .trim_matches(|char| char == '\n' || char == '\r' || char == '§')
+ .replace('\n', " ");
if self.is_inside_item_name() && !self.is_inside("span") && !self.is_inside("code") {
- self.push_str(&format!("`{trimmed_text}`"));
+ self.push_str(&format!("`{text}`"));
return Ok(());
}
- self.push_str(trimmed_text);
+ self.push_str(&text);
Ok(())
}
diff --git a/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs b/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs
index b900c8e795..456b130fbe 100644
--- a/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs
+++ b/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs
@@ -2,6 +2,7 @@
#![deny(missing_docs)]
+mod html_element;
mod markdown_writer;
use std::io::Read;
@@ -65,6 +66,72 @@ mod tests {
)
}
+ #[test]
+ fn test_single_paragraph() {
+ let html = indoc! {r#"
+ In particular, the last point is what sets axum
apart from other frameworks.
+ axum
doesn’t have its own middleware system but instead uses
+ tower::Service
. This means axum
gets timeouts, tracing, compression,
+ authorization, and more, for free. It also enables you to share middleware with
+ applications written using hyper
or tonic
.
+ "#};
+ let expected = indoc! {"
+ In particular, the last point is what sets `axum` apart from other frameworks. `axum` doesn’t have its own middleware system but instead uses `tower::Service`. This means `axum` gets timeouts, tracing, compression, authorization, and more, for free. It also enables you to share middleware with applications written using `hyper` or `tonic`.
+ "}
+ .trim();
+
+ assert_eq!(
+ convert_rustdoc_to_markdown(html.as_bytes()).unwrap(),
+ expected
+ )
+ }
+
+ #[test]
+ fn test_multiple_paragraphs() {
+ let html = indoc! {r##"
+ §Serde
+ Serde is a framework for serializing and deserializing 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.
+ See the Serde website https://serde.rs/ for additional documentation and
+ usage examples.
+ §Design
+ Where many other languages rely on runtime reflection for serializing data,
+ Serde is instead built on Rust’s powerful trait system. A data structure
+ that knows how to serialize and deserialize itself is one that implements
+ Serde’s Serialize
and Deserialize
traits (or uses Serde’s derive
+ attribute to automatically generate implementations at compile time). This
+ avoids any overhead of reflection or runtime type information. In fact in
+ many situations the interaction between data structure and data format can
+ be completely optimized away by the Rust compiler, leaving Serde
+ serialization to perform the same speed as a handwritten serializer for the
+ specific selection of data structure and data format.
+ "##};
+ let expected = indoc! {"
+ ## Serde
+
+ Serde is a framework for serializing and deserializing 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.
+
+ See the Serde website https://serde.rs/ for additional documentation and usage examples.
+
+ ### Design
+
+ Where many other languages rely on runtime reflection for serializing data, Serde is instead built on Rust’s powerful trait system. A data structure that knows how to serialize and deserialize itself is one that implements Serde’s `Serialize` and `Deserialize` traits (or uses Serde’s derive attribute to automatically generate implementations at compile time). This avoids any overhead of reflection or runtime type information. In fact in many situations the interaction between data structure and data format can be completely optimized away by the Rust compiler, leaving Serde serialization to perform the same speed as a handwritten serializer for the specific selection of data structure and data format.
+ "}
+ .trim();
+
+ assert_eq!(
+ convert_rustdoc_to_markdown(html.as_bytes()).unwrap(),
+ expected
+ )
+ }
+
#[test]
fn test_rust_code_block() {
let html = indoc! {r#"
@@ -201,8 +268,9 @@ mod tests {
let expected = indoc! {r#"
## Feature flags
- axum uses a set of feature flags to reduce the amount of compiled and
- optional dependencies.The following optional features are available:
+ axum uses a set of feature flags to reduce the amount of compiled and optional dependencies.
+
+ The following optional features are available:
| Name | Description | Default? |
| --- | --- | --- |