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

[Rust] fix multiple lines in markdown mode #907

Merged
merged 6 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 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 @@ -29,24 +30,28 @@ impl MarkdownHTMLParser {
options.insert(Options::ENABLE_STRIKETHROUGH);

let markdown = markdown.to_string();

let parser = Parser::new_ext(&markdown, options);
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
// assumption regarding the HTML markup. So let's remove it.
let html = {
if !html.starts_with("<p>") {
&html[..]
} else {
if html.starts_with("<p>") && html.matches("<p>").count() == 1 {
let p = "<p>".len();
let ppnl = "</p>\n".len();

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

Expand Down
43 changes: 43 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 @@ -194,3 +210,30 @@ fn set_content_from_markdown_codeblock_with_newlines() {
.unwrap();
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("|");
model
.set_content_from_markdown(&utf16("test\n\ntest"))
.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>");
}
3 changes: 1 addition & 2 deletions platforms/ios/example/Wysiwyg/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ struct ContentView: View {
switch keyCommand {
case .enter:
sentMessage = viewModel.content

viewModel.clearContent()
return true
case .shiftEnter:
return false
}
},
pasteHandler: { _ in })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ struct UITextViewWrapper: UIViewRepresentable {

private func processDefault(for keyCommand: WysiwygKeyCommand) {
switch keyCommand {
case .enter, .shiftEnter:
case .enter:
enter()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protocol WysiwygTextViewDelegate: AnyObject {
/// - textView: Composer text view.
/// - keyCommand: Key command received.
func textViewDidReceiveKeyCommand(_ textView: UITextView, keyCommand: WysiwygKeyCommand)

/// Notify the delegate that a paste event has beeb received by the text view.
///
/// - Parameters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ public enum WysiwygKeyCommand: CaseIterable {
/// User pressed `enter`. Default behaviour: a line feed is created.
/// Note: in the context of a messaging app, this is usually used to send a message.
case enter
/// User pressed `shift` + `enter`. Default behaviour: a line feed is created.
case shiftEnter

var input: String {
switch self {
case .enter, .shiftEnter:
case .enter:
return "\r"
}
}
Expand All @@ -37,8 +35,6 @@ public enum WysiwygKeyCommand: CaseIterable {
switch self {
case .enter:
return []
case .shiftEnter:
return .shift
}
}

Expand Down
Loading