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

Commit

Permalink
fix for markdown and newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
Velin92 committed Dec 13, 2023
1 parent 91fae40 commit 5136a8f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 14 deletions.
11 changes: 6 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 @@ -28,8 +28,7 @@ impl MarkdownHTMLParser {
let mut options = Options::empty();
options.insert(Options::ENABLE_STRIKETHROUGH);

let markdown = markdown.to_string();

let markdown = dbg!(markdown.to_string());
let parser = Parser::new_ext(&markdown, options);

let mut html = String::new();
Expand All @@ -39,14 +38,16 @@ impl MarkdownHTMLParser {
// 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.
// write me a function that gives me the number of substrings contained in a string:

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]
} else {
&html[..]
}
};

Expand Down
9 changes: 9 additions & 0 deletions crates/wysiwyg/src/tests/test_set_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,12 @@ 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_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>");
}
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

0 comments on commit 5136a8f

Please sign in to comment.