mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-08 07:35:01 +03:00
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
This commit is contained in:
parent
a5011996fb
commit
bdf627ce07
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -8640,6 +8640,7 @@ dependencies = [
|
|||||||
"html5ever",
|
"html5ever",
|
||||||
"indoc",
|
"indoc",
|
||||||
"markup5ever_rcdom",
|
"markup5ever_rcdom",
|
||||||
|
"pretty_assertions",
|
||||||
"regex",
|
"regex",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -19,3 +19,4 @@ regex.workspace = true
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
indoc.workspace = true
|
indoc.workspace = true
|
||||||
|
pretty_assertions.workspace = true
|
||||||
|
@ -114,9 +114,8 @@ impl MarkdownWriter {
|
|||||||
self.visit_node(child)?;
|
self.visit_node(child)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.current_element_stack.pop_back();
|
|
||||||
|
|
||||||
if let Some(current_element) = current_element {
|
if let Some(current_element) = current_element {
|
||||||
|
self.current_element_stack.pop_back();
|
||||||
self.end_tag(¤t_element);
|
self.end_tag(¤t_element);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,3 +36,53 @@ pub fn convert_rustdoc_to_markdown(mut html: impl Read) -> Result<String> {
|
|||||||
|
|
||||||
Ok(markdown)
|
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#"
|
||||||
|
<pre class="rust rust-example-rendered"><code><span class="kw">use </span>axum::extract::{Path, Query, Json};
|
||||||
|
<span class="kw">use </span>std::collections::HashMap;
|
||||||
|
|
||||||
|
<span class="comment">// `Path` gives you the path parameters and deserializes them.
|
||||||
|
</span><span class="kw">async fn </span>path(Path(user_id): Path<u32>) {}
|
||||||
|
|
||||||
|
<span class="comment">// `Query` gives you the query parameters and deserializes them.
|
||||||
|
</span><span class="kw">async fn </span>query(Query(params): Query<HashMap<String, String>>) {}
|
||||||
|
|
||||||
|
<span class="comment">// Buffer the request body and deserialize it as JSON into a
|
||||||
|
// `serde_json::Value`. `Json` supports any type that implements
|
||||||
|
// `serde::Deserialize`.
|
||||||
|
</span><span class="kw">async fn </span>json(Json(payload): Json<serde_json::Value>) {}</code></pre>
|
||||||
|
"#};
|
||||||
|
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<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>) {}
|
||||||
|
```
|
||||||
|
"}
|
||||||
|
.trim();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
convert_rustdoc_to_markdown(html.as_bytes()).unwrap(),
|
||||||
|
expected
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user