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

Commit

Permalink
Allow markdown in message format (#740)
Browse files Browse the repository at this point in the history
* adds a new function get_content_as_message_markdown
* passes an as_message argument through calls to functions that render the model as markdown
  • Loading branch information
artcodespace authored Jul 14, 2023
1 parent 61cdc44 commit 69e0e48
Show file tree
Hide file tree
Showing 14 changed files with 343 additions and 130 deletions.
8 changes: 8 additions & 0 deletions bindings/wysiwyg-ffi/src/ffi_composer_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ impl ComposerModel {
.to_string()
}

pub fn get_content_as_message_markdown(self: &Arc<Self>) -> String {
self.inner
.lock()
.unwrap()
.get_content_as_message_markdown()
.to_string()
}

pub fn get_content_as_plain_text(self: &Arc<Self>) -> String {
self.inner
.lock()
Expand Down
1 change: 1 addition & 0 deletions bindings/wysiwyg-ffi/src/wysiwyg_composer.udl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface ComposerModel {
string get_content_as_html();
string get_content_as_message_html();
string get_content_as_markdown();
string get_content_as_message_markdown();
string get_content_as_plain_text();
ComposerUpdate clear();
ComposerUpdate select(u32 start_utf16_codeunit, u32 end_utf16_codeunit);
Expand Down
4 changes: 4 additions & 0 deletions bindings/wysiwyg-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ impl ComposerModel {
self.inner.get_content_as_markdown().to_string()
}

pub fn get_content_as_message_markdown(&self) -> String {
self.inner.get_content_as_message_markdown().to_string()
}

pub fn get_content_as_plain_text(&self) -> String {
self.inner.get_content_as_plain_text().to_string()
}
Expand Down
4 changes: 4 additions & 0 deletions crates/wysiwyg/src/composer_model/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ where
self.state.dom.to_markdown().unwrap()
}

pub fn get_content_as_message_markdown(&self) -> S {
self.state.dom.to_message_markdown().unwrap()
}

pub fn get_content_as_plain_text(&self) -> S {
self.state.dom.to_plain_text()
}
Expand Down
3 changes: 2 additions & 1 deletion crates/wysiwyg/src/dom/dom_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,9 @@ where
&self,
buffer: &mut S,
options: &MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>> {
self.document.fmt_markdown(buffer, options)
self.document.fmt_markdown(buffer, options, as_message)
}
}

Expand Down
65 changes: 41 additions & 24 deletions crates/wysiwyg/src/dom/nodes/container_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ where
&self,
buffer: &mut S,
options: &MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>> {
use ContainerNodeKind::*;
use InlineFormatType::*;
Expand All @@ -914,53 +915,53 @@ where

match self.kind() {
Generic => {
fmt_children(self, buffer, &options)?;
fmt_children(self, buffer, &options, as_message)?;
}

// Simple emphasis.
Formatting(Italic) => {
fmt_italic(self, buffer, &options)?;
fmt_italic(self, buffer, &options, as_message)?;
}

// Strong emphasis.
Formatting(Bold) => {
fmt_bold(self, buffer, &options)?;
fmt_bold(self, buffer, &options, as_message)?;
}

Formatting(StrikeThrough) => {
fmt_strikethrough(self, buffer, &options)?;
fmt_strikethrough(self, buffer, &options, as_message)?;
}

Formatting(Underline) => {
fmt_underline(self, buffer, &options)?;
fmt_underline(self, buffer, &options, as_message)?;
}

Formatting(InlineCode) => {
fmt_inline_code(self, buffer, &mut options)?;
fmt_inline_code(self, buffer, &mut options, as_message)?;
}

Link(url) => {
fmt_link(self, buffer, &options, url)?;
fmt_link(self, buffer, &options, url, as_message)?;
}

List(_) => {
fmt_list(self, buffer, &options)?;
fmt_list(self, buffer, &options, as_message)?;
}

ListItem => {
fmt_list_item(self, buffer, &options)?;
fmt_list_item(self, buffer, &options, as_message)?;
}

CodeBlock => {
fmt_code_block(self, buffer, &options)?;
fmt_code_block(self, buffer, &options, as_message)?;
}

Quote => {
fmt_quote(self, buffer, &options)?;
fmt_quote(self, buffer, &options, as_message)?;
}

Paragraph => {
fmt_paragraph(self, buffer, &options)?;
fmt_paragraph(self, buffer, &options, as_message)?;
}
};

Expand All @@ -974,6 +975,7 @@ where
this: &ContainerNode<S>,
buffer: &mut S,
options: &MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>>
where
S: UnicodeString,
Expand All @@ -983,7 +985,7 @@ where
buffer.push("\n");
}

child.fmt_markdown(buffer, options)?;
child.fmt_markdown(buffer, options, as_message)?;
}

Ok(())
Expand All @@ -994,6 +996,7 @@ where
this: &ContainerNode<S>,
buffer: &mut S,
options: &MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>>
where
S: UnicodeString,
Expand All @@ -1005,7 +1008,7 @@ where
// trend to avoid unexpected behaviours for our users.

buffer.push("*");
fmt_children(this, buffer, options)?;
fmt_children(this, buffer, options, as_message)?;
buffer.push("*");

Ok(())
Expand All @@ -1016,6 +1019,7 @@ where
this: &ContainerNode<S>,
buffer: &mut S,
options: &MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>>
where
S: UnicodeString,
Expand All @@ -1031,7 +1035,7 @@ where
// there. Instead, it will produce `*__…__*`.

buffer.push("__");
fmt_children(this, buffer, options)?;
fmt_children(this, buffer, options, as_message)?;
buffer.push("__");

Ok(())
Expand All @@ -1042,6 +1046,7 @@ where
this: &ContainerNode<S>,
buffer: &mut S,
options: &MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>>
where
S: UnicodeString,
Expand All @@ -1053,7 +1058,7 @@ where
// do not support this format extension.

buffer.push("~~");
fmt_children(this, buffer, options)?;
fmt_children(this, buffer, options, as_message)?;
buffer.push("~~");

Ok(())
Expand All @@ -1064,6 +1069,7 @@ where
this: &ContainerNode<S>,
buffer: &mut S,
options: &MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>>
where
S: UnicodeString,
Expand All @@ -1072,7 +1078,7 @@ where
// use raw HTML.

buffer.push("<u>");
fmt_children(this, buffer, options)?;
fmt_children(this, buffer, options, as_message)?;
buffer.push("</u>");

Ok(())
Expand All @@ -1083,6 +1089,7 @@ where
this: &ContainerNode<S>,
buffer: &mut S,
options: &mut MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>>
where
S: UnicodeString,
Expand All @@ -1102,7 +1109,7 @@ where
buffer.push("`` ");

options.insert(MarkdownOptions::IGNORE_LINE_BREAK);
fmt_children(this, buffer, options)?;
fmt_children(this, buffer, options, as_message)?;

buffer.push(" ``");

Expand All @@ -1115,13 +1122,14 @@ where
buffer: &mut S,
options: &MarkdownOptions,
url: &S,
as_message: bool,
) -> Result<(), MarkdownError<S>>
where
S: UnicodeString,
{
buffer.push('[');

fmt_children(this, buffer, options)?;
fmt_children(this, buffer, options, as_message)?;

// A link destination can be delimited by `<` and
// `>`.
Expand Down Expand Up @@ -1150,6 +1158,7 @@ where
this: &ContainerNode<S>,
buffer: &mut S,
options: &MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>>
where
S: UnicodeString,
Expand Down Expand Up @@ -1240,7 +1249,11 @@ where
{
// Let's create a new buffer for the child formatting.
let mut child_buffer = S::default();
child.fmt_markdown(&mut child_buffer, options)?;
child.fmt_markdown(
&mut child_buffer,
options,
as_message,
)?;

// Generate the indentation of form `\n` followed by
// $x$ spaces where $x$ is `indentation`.
Expand Down Expand Up @@ -1279,11 +1292,12 @@ where
this: &ContainerNode<S>,
buffer: &mut S,
options: &MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>>
where
S: UnicodeString,
{
fmt_children(this, buffer, options)?;
fmt_children(this, buffer, options, as_message)?;

Ok(())
}
Expand All @@ -1293,12 +1307,13 @@ where
this: &ContainerNode<S>,
buffer: &mut S,
options: &MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>>
where
S: UnicodeString,
{
buffer.push("```\n");
fmt_children(this, buffer, options)?;
fmt_children(this, buffer, options, as_message)?;
buffer.push("\n```\n");

Ok(())
Expand All @@ -1309,12 +1324,13 @@ where
this: &ContainerNode<S>,
buffer: &mut S,
options: &MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>>
where
S: UnicodeString,
{
buffer.push("> ");
fmt_children(this, buffer, options)?;
fmt_children(this, buffer, options, as_message)?;
buffer.push("\n");

Ok(())
Expand All @@ -1325,11 +1341,12 @@ where
this: &ContainerNode<S>,
buffer: &mut S,
options: &MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>>
where
S: UnicodeString,
{
fmt_children(this, buffer, options)?;
fmt_children(this, buffer, options, as_message)?;

Ok(())
}
Expand Down
15 changes: 11 additions & 4 deletions crates/wysiwyg/src/dom/nodes/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,21 @@ where
&self,
buffer: &mut S,
options: &MarkdownOptions,
as_message: bool,
) -> Result<(), MarkdownError<S>> {
match self {
DomNode::Container(container) => {
container.fmt_markdown(buffer, options)
container.fmt_markdown(buffer, options, as_message)
}
DomNode::Text(text) => {
text.fmt_markdown(buffer, options, as_message)
}
DomNode::LineBreak(node) => {
node.fmt_markdown(buffer, options, as_message)
}
DomNode::Mention(node) => {
node.fmt_markdown(buffer, options, as_message)
}
DomNode::Text(text) => text.fmt_markdown(buffer, options),
DomNode::LineBreak(node) => node.fmt_markdown(buffer, options),
DomNode::Mention(node) => node.fmt_markdown(buffer, options),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/wysiwyg/src/dom/nodes/line_break_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ where
&self,
buffer: &mut S,
options: &MarkdownOptions,
_as_message: bool,
) -> Result<(), MarkdownError<S>> {
if options.contains(MarkdownOptions::IGNORE_LINE_BREAK) {
// Replace the line break by a single space.
Expand Down
Loading

0 comments on commit 69e0e48

Please sign in to comment.