events: Improve room::message markdown constructors
Only return an html message if some markdown formatting was detected.
This commit is contained in:
parent
676548a2fe
commit
52d8ff1ffd
@ -427,13 +427,13 @@ impl EmoteMessageEventContent {
|
||||
}
|
||||
|
||||
/// A convenience constructor to create a markdown emote.
|
||||
///
|
||||
/// Returns an html emote message if some markdown formatting was detected, otherwise returns a
|
||||
/// plain-text emote.
|
||||
#[cfg(feature = "markdown")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "markdown")))]
|
||||
pub fn markdown(body: impl Into<String>) -> Self {
|
||||
let body = body.into();
|
||||
let mut html_body = String::new();
|
||||
pulldown_cmark::html::push_html(&mut html_body, pulldown_cmark::Parser::new(&body));
|
||||
Self::html(body, html_body)
|
||||
pub fn markdown(body: impl AsRef<str> + Into<String>) -> Self {
|
||||
Self { formatted: FormattedBody::markdown(&body), ..Self::plain(body) }
|
||||
}
|
||||
}
|
||||
|
||||
@ -629,6 +629,16 @@ impl NoticeMessageEventContent {
|
||||
pub fn html(body: impl Into<String>, html_body: impl Into<String>) -> Self {
|
||||
Self { formatted: Some(FormattedBody::html(html_body)), ..Self::plain(body) }
|
||||
}
|
||||
|
||||
/// A convenience constructor to create a markdown notice.
|
||||
///
|
||||
/// Returns an html notice if some markdown formatting was detected, otherwise returns a plain
|
||||
/// text notice.
|
||||
#[cfg(feature = "markdown")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "markdown")))]
|
||||
pub fn markdown(body: impl AsRef<str> + Into<String>) -> Self {
|
||||
Self { formatted: FormattedBody::markdown(&body), ..Self::plain(body) }
|
||||
}
|
||||
}
|
||||
|
||||
/// The payload for a server notice message.
|
||||
@ -725,6 +735,24 @@ impl FormattedBody {
|
||||
pub fn html(body: impl Into<String>) -> Self {
|
||||
Self { format: MessageFormat::Html, body: body.into() }
|
||||
}
|
||||
|
||||
/// Creates a new HTML-formatted message body by parsing the markdown in `body`.
|
||||
///
|
||||
/// Returns `None` if no markdown formatting was found.
|
||||
#[cfg(feature = "markdown")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "markdown")))]
|
||||
pub fn markdown(body: impl AsRef<str>) -> Option<Self> {
|
||||
let body = body.as_ref();
|
||||
let mut html_body = String::new();
|
||||
|
||||
pulldown_cmark::html::push_html(&mut html_body, pulldown_cmark::Parser::new(body));
|
||||
|
||||
if html_body == format!("<p>{}</p>\n", body) {
|
||||
None
|
||||
} else {
|
||||
Some(Self::html(html_body))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The payload for a text message.
|
||||
@ -752,13 +780,13 @@ impl TextMessageEventContent {
|
||||
}
|
||||
|
||||
/// A convenience constructor to create a markdown message.
|
||||
///
|
||||
/// Returns an html message if some markdown formatting was detected, otherwise returns a plain
|
||||
/// text message.
|
||||
#[cfg(feature = "markdown")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "markdown")))]
|
||||
pub fn markdown(body: impl Into<String>) -> Self {
|
||||
let body = body.into();
|
||||
let mut html_body = String::new();
|
||||
pulldown_cmark::html::push_html(&mut html_body, pulldown_cmark::Parser::new(&body));
|
||||
Self::html(body, html_body)
|
||||
pub fn markdown(body: impl AsRef<str> + Into<String>) -> Self {
|
||||
Self { formatted: FormattedBody::markdown(&body), ..Self::plain(body) }
|
||||
}
|
||||
|
||||
/// A convenience constructor to create a plain text message.
|
||||
|
@ -155,6 +155,50 @@ fn plain_text_content_serialization() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "markdown")]
|
||||
fn markdown_content_serialization() {
|
||||
let formatted_message = MessageEventContent::new(MessageType::Text(
|
||||
TextMessageEventContent::markdown("Testing **bold** and _italic_!"),
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&formatted_message).unwrap(),
|
||||
json!({
|
||||
"body": "Testing **bold** and _italic_!",
|
||||
"formatted_body": "<p>Testing <strong>bold</strong> and <em>italic</em>!</p>\n",
|
||||
"format": "org.matrix.custom.html",
|
||||
"msgtype": "m.text"
|
||||
})
|
||||
);
|
||||
|
||||
let plain_message_simple = MessageEventContent::new(MessageType::Text(
|
||||
TextMessageEventContent::markdown("Testing a simple phrase…"),
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&plain_message_simple).unwrap(),
|
||||
json!({
|
||||
"body": "Testing a simple phrase…",
|
||||
"msgtype": "m.text"
|
||||
})
|
||||
);
|
||||
|
||||
let plain_message_paragraphs = MessageEventContent::new(MessageType::Text(
|
||||
TextMessageEventContent::markdown("Testing\n\nSeveral\n\nParagraphs."),
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
to_json_value(&plain_message_paragraphs).unwrap(),
|
||||
json!({
|
||||
"body": "Testing\n\nSeveral\n\nParagraphs.",
|
||||
"formatted_body": "<p>Testing</p>\n<p>Several</p>\n<p>Paragraphs.</p>\n",
|
||||
"format": "org.matrix.custom.html",
|
||||
"msgtype": "m.text"
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relates_to_content_serialization() {
|
||||
let message_event_content =
|
||||
|
Loading…
x
Reference in New Issue
Block a user