From e16821778c1f35633aa857050b5b815abb83e2cb Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Wed, 13 Dec 2023 18:45:27 +0100 Subject: [PATCH] fix markdown content --- .../parser/markdown/markdown_html_parser.rs | 16 ++++++--- crates/wysiwyg/src/tests/test_set_content.rs | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/crates/wysiwyg/src/dom/parser/markdown/markdown_html_parser.rs b/crates/wysiwyg/src/dom/parser/markdown/markdown_html_parser.rs index 2cd46efe7..88399cb67 100644 --- a/crates/wysiwyg/src/dom/parser/markdown/markdown_html_parser.rs +++ b/crates/wysiwyg/src/dom/parser/markdown/markdown_html_parser.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use md_parser::Event; use pulldown_cmark as md_parser; use crate::{dom::MarkdownParseError, UnicodeString}; @@ -28,12 +29,17 @@ impl MarkdownHTMLParser { let mut options = Options::empty(); options.insert(Options::ENABLE_STRIKETHROUGH); - let markdown = dbg!(markdown.to_string()); - let parser = Parser::new_ext(&markdown, options); + let markdown = markdown.to_string(); + let parser_events: Vec<_> = Parser::new_ext(&markdown, options) + .map(|event| match event { + Event::SoftBreak => Event::HardBreak, + _ => event, + }) + .collect(); let mut html = String::new(); - compile_to_html(&mut html, parser); + compile_to_html(&mut html, parser_events.into_iter()); // By default, there is a `

\n` around the HTML content. That's the // correct way to handle a text block in Markdown. But it breaks our @@ -45,9 +51,9 @@ impl MarkdownHTMLParser { let p = "

".len(); let ppnl = "

\n".len(); - &html[p..html.len() - ppnl] + html[p..html.len() - ppnl].to_string() } else { - &html[..] + html[..].to_string() } }; diff --git a/crates/wysiwyg/src/tests/test_set_content.rs b/crates/wysiwyg/src/tests/test_set_content.rs index 889612add..876ce23b2 100644 --- a/crates/wysiwyg/src/tests/test_set_content.rs +++ b/crates/wysiwyg/src/tests/test_set_content.rs @@ -149,6 +149,22 @@ fn set_content_from_html_moves_cursor_to_the_end() { assert_eq!(tx(&model), "content|"); } +#[test] +fn set_content_from_html_single_br() { + let mut model = cm("|"); + model.set_content_from_html(&utf16("test
test")).unwrap(); + assert_eq!(tx(&model), "

test

test|

"); +} + +#[test] +fn set_content_from_html_multiple_br() { + let mut model = cm("|"); + model + .set_content_from_html(&utf16("test

test")) + .unwrap(); + assert_eq!(tx(&model), "

test

 

test|

"); +} + #[test] fn clear() { let mut model = cm("|"); @@ -195,6 +211,15 @@ fn set_content_from_markdown_codeblock_with_newlines() { assert_eq!(tx(&model), "
I am a code block|
"); } +#[test] +fn set_content_from_markdown_codeblock_with_newlines_in_the_middle() { + let mut model = cm("|"); + model + .set_content_from_markdown(&utf16("```\nI am\na code block\n```")) + .unwrap(); + assert_eq!(tx(&model), "
I am\na code block|
"); +} + #[test] fn set_content_from_markdown_multiple_new_lines() { let mut model = cm("|"); @@ -203,3 +228,12 @@ fn set_content_from_markdown_multiple_new_lines() { .unwrap(); assert_eq!(tx(&model), "

test

test|

"); } + +#[test] +fn set_content_from_markdown_one_new_line() { + let mut model = cm("|"); + model + .set_content_from_markdown(&utf16("test\ntest")) + .unwrap(); + assert_eq!(tx(&model), "

test

test|

"); +}