Skip to content

Commit

Permalink
fix: adjust newline handling and code block formatting
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
izyuumi committed Mar 12, 2024
1 parent cf23033 commit e6ae0bb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ pub enum ParseHTMLTypeError {
pub fn safe_parse_html(input: String) -> Result<Node, ParseHTMLTypeError> {
let mut current_index = 0;
let mut nodes = Vec::new();
let input = input.replace('\n', "");
let mut stack: Vec<Node> = Vec::new();

while current_index < input.len() {
Expand Down
2 changes: 1 addition & 1 deletion src/to_md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
16 changes: 14 additions & 2 deletions tests/to_md_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,20 @@ mod to_md_tests {

#[test]
fn code_block() {
let input = "<pre><code class=\"language-rust\">let x: i32 = 123;</code></pre>".to_string();
let expected = "```rust\nlet x: i32 = 123;```\n".to_string();
let input = "<pre><code class=\"language-rust\">
let x: i32 = 123;
let y: i32 = 456;
let z = x + y;
println!(\"{}\", z);
</code></pre>"
.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);
}

Expand Down

0 comments on commit e6ae0bb

Please sign in to comment.