From bdf627ce07e7f0d4dc72eafca3be276c4ed8ca5d Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Wed, 29 May 2024 19:23:06 -0400 Subject: [PATCH] rustdoc_to_markdown: Fix code blocks (#12460) This PR fixes an issue in `rustdoc_to_markdown` with code blocks being trimmed incorrectly. We were erroneously popping from the current element stack even if we didn't push an element onto the stack. Added test coverage for this case as well, so we don't regress. Release Notes: - N/A --- Cargo.lock | 1 + crates/rustdoc_to_markdown/Cargo.toml | 1 + .../src/markdown_writer.rs | 3 +- .../src/rustdoc_to_markdown.rs | 50 +++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 510303fc1e7ea7ede0ccf2afd7853e553f41064d..43a5ac03cc9933a44b885646fc49e17ebe9d8f1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8640,6 +8640,7 @@ dependencies = [ "html5ever", "indoc", "markup5ever_rcdom", + "pretty_assertions", "regex", ] diff --git a/crates/rustdoc_to_markdown/Cargo.toml b/crates/rustdoc_to_markdown/Cargo.toml index 18bb21f9d9b94e0efe7a9ec959cefd7adca127c6..58a60bc7bf330ecf8b1f05b65a128ffb16a3356a 100644 --- a/crates/rustdoc_to_markdown/Cargo.toml +++ b/crates/rustdoc_to_markdown/Cargo.toml @@ -19,3 +19,4 @@ regex.workspace = true [dev-dependencies] indoc.workspace = true +pretty_assertions.workspace = true diff --git a/crates/rustdoc_to_markdown/src/markdown_writer.rs b/crates/rustdoc_to_markdown/src/markdown_writer.rs index cf71982ecb4f3a05b750d22e1a999fc56891a8f2..b48a780c3558b17cbe9de893785c0f62537817d3 100644 --- a/crates/rustdoc_to_markdown/src/markdown_writer.rs +++ b/crates/rustdoc_to_markdown/src/markdown_writer.rs @@ -114,9 +114,8 @@ impl MarkdownWriter { self.visit_node(child)?; } - self.current_element_stack.pop_back(); - if let Some(current_element) = current_element { + self.current_element_stack.pop_back(); self.end_tag(¤t_element); } diff --git a/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs b/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs index dbbafa2c5c1faa60cdc50bd9c91ea604be1669eb..ac34213e2c6ac28b63727954226cf870b07b8a38 100644 --- a/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs +++ b/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs @@ -36,3 +36,53 @@ pub fn convert_rustdoc_to_markdown(mut html: impl Read) -> Result { Ok(markdown) } + +#[cfg(test)] +mod tests { + use indoc::indoc; + use pretty_assertions::assert_eq; + + use super::*; + + #[test] + fn test_code_blocks() { + let html = indoc! {r#" +
use axum::extract::{Path, Query, Json};
+            use std::collections::HashMap;
+
+            // `Path` gives you the path parameters and deserializes them.
+            async fn path(Path(user_id): Path<u32>) {}
+
+            // `Query` gives you the query parameters and deserializes them.
+            async fn query(Query(params): Query<HashMap<String, String>>) {}
+
+            // Buffer the request body and deserialize it as JSON into a
+            // `serde_json::Value`. `Json` supports any type that implements
+            // `serde::Deserialize`.
+            async fn json(Json(payload): Json<serde_json::Value>) {}
+ "#}; + let expected = indoc! {" + ```rs + use axum::extract::{Path, Query, Json}; + use std::collections::HashMap; + + // `Path` gives you the path parameters and deserializes them. + async fn path(Path(user_id): Path) {} + + // `Query` gives you the query parameters and deserializes them. + async fn query(Query(params): Query>) {} + + // Buffer the request body and deserialize it as JSON into a + // `serde_json::Value`. `Json` supports any type that implements + // `serde::Deserialize`. + async fn json(Json(payload): Json) {} + ``` + "} + .trim(); + + assert_eq!( + convert_rustdoc_to_markdown(html.as_bytes()).unwrap(), + expected + ) + } +}