markdown: Remove P tag that wraps the html

The Comrak lib that we're using wraps each parsed markdown with a
`<p>...</p>\n` so we need to remove to avoid the modification of the
real message.

See #210
This commit is contained in:
Daniel García Moreno 2018-04-29 21:07:15 +02:00
parent 8d5e98f259
commit 577703fc71
2 changed files with 19 additions and 2 deletions

7
Cargo.lock generated
View file

@ -369,6 +369,7 @@ dependencies = [
"gdk 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gdk-pixbuf 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glib 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"md5 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"pango 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pangocairo 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -831,6 +832,11 @@ name = "matches"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "md5"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "2.0.1"
@ -2018,6 +2024,7 @@ dependencies = [
"checksum maplit 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08cbb6b4fef96b6d77bfc40ec491b1690c779e77b05cd9f07f787ed376fd4c43"
"checksum markup5ever 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfedc97d5a503e96816d10fedcd5b42f760b2e525ce2f7ec71f6a41780548475"
"checksum matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "100aabe6b8ff4e4a7e32c1c13523379802df0772b82466207ac25b013f193376"
"checksum md5 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "daa1004633f76cdcd5a9d83ffcfe615e30ca7a2a638fcc8b8039a2dac21289d7"
"checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d"
"checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3"
"checksum mime 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e2e00e17be181010a91dbfefb01660b17311059dc8c7f48b9017677721e732bd"

View file

@ -1379,9 +1379,19 @@ impl AppOp {
// Riot does not properly show emotes with Markdown;
// Emotes with markdown have a newline after the username
if m.mtype != "m.emote" && self.md_enabled {
let md_parsed_msg = markdown_to_html(&msg, &ComrakOptions::default());
let mut md_parsed_msg = markdown_to_html(&msg, &ComrakOptions::default());
if md_parsed_msg != String::from("<p>") + &msg + &String::from("</p>\n") {
// Removing wrap tag: <p>..</p>\n
let limit = md_parsed_msg.len() - 5;
let trim = match (md_parsed_msg.get(0..3), md_parsed_msg.get(limit..)) {
(Some(open), Some(close)) if open == "<p>" && close == "</p>\n" => { true }
_ => { false }
};
if trim {
md_parsed_msg = md_parsed_msg.get(3..limit).unwrap_or(&md_parsed_msg).to_string();
}
if md_parsed_msg != msg {
m.formatted_body = Some(md_parsed_msg);
m.format = Some(String::from("org.matrix.custom.html"));
}