From 8c109d3c0a7ec66b352dc82677d30db7cb0723eb Mon Sep 17 00:00:00 2001 From: Tilo Spannagel Date: Thu, 21 Jan 2021 15:53:03 +0100 Subject: [PATCH] Add constructor for markdown formatted messages behind a feature flag Signed-off-by: Tilo Spannagel --- ruma-events/Cargo.toml | 2 ++ ruma-events/src/room/message.rs | 9 +++++++++ ruma/Cargo.toml | 1 + 3 files changed, 12 insertions(+) diff --git a/ruma-events/Cargo.toml b/ruma-events/Cargo.toml index 8d06ffb1..28ded211 100644 --- a/ruma-events/Cargo.toml +++ b/ruma-events/Cargo.toml @@ -20,6 +20,7 @@ ruma-identifiers = { version = "=0.18.0-alpha.1", path = "../ruma-identifiers" } ruma-serde = { version = "0.3.0", path = "../ruma-serde" } serde = { version = "1.0.118", features = ["derive"] } serde_json = { version = "1.0.60", features = ["raw_value"] } +pulldown-cmark = { version = "0.8", default-features = false, optional = true } [dev-dependencies] maplit = "1.0.2" @@ -31,6 +32,7 @@ trybuild = "1.0.38" unstable-exhaustive-types = [] unstable-pre-spec = [] unstable-synapse-quirks = [] +markdown = ["pulldown-cmark"] [[bench]] name = "event_deserialize" diff --git a/ruma-events/src/room/message.rs b/ruma-events/src/room/message.rs index cc304233..8ef149ec 100644 --- a/ruma-events/src/room/message.rs +++ b/ruma-events/src/room/message.rs @@ -479,6 +479,15 @@ impl TextMessageEventContent { Self { formatted: Some(FormattedBody::html(html_body)), ..Self::plain(body) } } + /// A convenience constructor to create a markdown message. + #[cfg(feature = "markdown")] + pub fn markdown(body: impl Into) -> 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) + } + /// A convenience constructor to create a plain text message. #[deprecated = "Renamed to plain"] pub fn new_plain(body: impl Into) -> Self { diff --git a/ruma/Cargo.toml b/ruma/Cargo.toml index 46f3c0e1..2a5c94f5 100644 --- a/ruma/Cargo.toml +++ b/ruma/Cargo.toml @@ -63,3 +63,4 @@ unstable-synapse-quirks = [ "ruma-common/unstable-synapse-quirks", "ruma-events/unstable-synapse-quirks", ] +markdown = ["ruma-events/markdown"]