Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
fix markdown content
Browse files Browse the repository at this point in the history
  • Loading branch information
Velin92 committed Dec 13, 2023
1 parent 5136a8f commit e168217
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
16 changes: 11 additions & 5 deletions crates/wysiwyg/src/dom/parser/markdown/markdown_html_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 `<p>…</p>\n` around the HTML content. That's the
// correct way to handle a text block in Markdown. But it breaks our
Expand All @@ -45,9 +51,9 @@ impl MarkdownHTMLParser {
let p = "<p>".len();
let ppnl = "</p>\n".len();

&html[p..html.len() - ppnl]
html[p..html.len() - ppnl].to_string()
} else {
&html[..]
html[..].to_string()
}
};

Expand Down
34 changes: 34 additions & 0 deletions crates/wysiwyg/src/tests/test_set_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<br>test")).unwrap();
assert_eq!(tx(&model), "<p>test</p><p>test|</p>");
}

#[test]
fn set_content_from_html_multiple_br() {
let mut model = cm("|");
model
.set_content_from_html(&utf16("test<br><br>test"))
.unwrap();
assert_eq!(tx(&model), "<p>test</p><p>&nbsp;</p><p>test|</p>");
}

#[test]
fn clear() {
let mut model = cm("|");
Expand Down Expand Up @@ -195,6 +211,15 @@ fn set_content_from_markdown_codeblock_with_newlines() {
assert_eq!(tx(&model), "<pre><code>I am a code block|</code></pre>");
}

#[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), "<pre><code>I am\na code block|</code></pre>");
}

#[test]
fn set_content_from_markdown_multiple_new_lines() {
let mut model = cm("|");
Expand All @@ -203,3 +228,12 @@ fn set_content_from_markdown_multiple_new_lines() {
.unwrap();
assert_eq!(tx(&model), "<p>test</p><p>test|</p>");
}

#[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), "<p>test</p><p>test|</p>");
}

0 comments on commit e168217

Please sign in to comment.