From e6ae0bb365facec0501edf53105ce1ffaf2ad5d3 Mon Sep 17 00:00:00 2001 From: Yumi Izumi Date: Tue, 12 Mar 2024 18:28:39 -0400 Subject: [PATCH] fix: adjust newline handling and code block formatting This commit modifies the newline handling in the HTML parsing function, removing the replacement of newline characters with an empty string. This change allows for the correct parsing of multi-line code blocks. Additionally, the code block formatting in the markdown conversion function is adjusted. The newline character after the language specification in the markdown code block is removed, ensuring correct formatting of the output markdown. Lastly, the test for the code block conversion is updated to reflect these changes, now testing for a multi-line code block with correct formatting. --- src/parser.rs | 1 - src/to_md.rs | 2 +- tests/to_md_tests.rs | 16 ++++++++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 83516c0..3548a66 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -60,7 +60,6 @@ pub enum ParseHTMLTypeError { pub fn safe_parse_html(input: String) -> Result { let mut current_index = 0; let mut nodes = Vec::new(); - let input = input.replace('\n', ""); let mut stack: Vec = Vec::new(); while current_index < input.len() { diff --git a/src/to_md.rs b/src/to_md.rs index 7c5d14a..6d64378 100644 --- a/src/to_md.rs +++ b/src/to_md.rs @@ -105,7 +105,7 @@ pub fn to_md(node: Node) -> String { .find(|class| class.starts_with("language-")) .map(|class| &class[9..]) { - res.push_str(&format!("```{}\n", language)); + res.push_str(&format!("```{}", language)); } else { res.push_str("```\n"); } diff --git a/tests/to_md_tests.rs b/tests/to_md_tests.rs index baffe86..c898a19 100644 --- a/tests/to_md_tests.rs +++ b/tests/to_md_tests.rs @@ -74,8 +74,20 @@ mod to_md_tests { #[test] fn code_block() { - let input = "
let x: i32 = 123;
".to_string(); - let expected = "```rust\nlet x: i32 = 123;```\n".to_string(); + let input = "

+let x: i32 = 123;
+let y: i32 = 456;
+let z = x + y;
+println!(\"{}\", z);
+
" + .to_string(); + let expected = "```rust +let x: i32 = 123; +let y: i32 = 456; +let z = x + y; +println!(\"{}\", z); +```\n" + .to_string(); assert_eq!(from_html_to_md(input), expected); }