From 8ca3a8b6078ee92aa39e7533d90c5a9e20521aae Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Mon, 15 Mar 2021 20:59:18 +0100 Subject: [PATCH] Don't send markdown links in body fixes #422 --- src/Config.h | 4 ++++ src/timeline/InputBar.cpp | 6 ++++++ src/timeline/TimelineModel.cpp | 30 ++++++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Config.h b/src/Config.h index d3fee836..97669822 100644 --- a/src/Config.h +++ b/src/Config.h @@ -60,6 +60,10 @@ const QRegularExpression url_regex( // vvvvvv match quote via negative lookahead/lookbehind vv // vvvv atomic match url -> fail if there is a " before or after vvv R"((?((www\.(?!\.)|[a-z][a-z0-9+.-]*://)[^\s<>'"]+[^!,\.\s<>'"\]\)\:]))(?!["']))"); +// match any markdown matrix.to link. Capture group 1 is the link name, group 2 is the target. +static const QRegularExpression matrixToMarkdownLink( + R"(\[(.*?)(?(.*?))"); } // Window geometry. diff --git a/src/timeline/InputBar.cpp b/src/timeline/InputBar.cpp index d5a6a1dd..20a3fe10 100644 --- a/src/timeline/InputBar.cpp +++ b/src/timeline/InputBar.cpp @@ -264,6 +264,9 @@ InputBar::message(QString msg, MarkdownOverride useMarkdown) useMarkdown == MarkdownOverride::NOT_SPECIFIED) || useMarkdown == MarkdownOverride::ON) { text.formatted_body = utils::markdownToHtml(msg).toStdString(); + // Remove markdown links by completer + text.body = + msg.trimmed().replace(conf::strings::matrixToMarkdownLink, "\\1").toStdString(); // Don't send formatted_body, when we don't need to if (text.formatted_body.find("<") == std::string::npos) @@ -326,6 +329,9 @@ InputBar::emote(QString msg) ChatPage::instance()->userSettings()->markdown()) { emote.formatted_body = html.toStdString(); emote.format = "org.matrix.custom.html"; + // Remove markdown links by completer + emote.body = + msg.trimmed().replace(conf::strings::matrixToMarkdownLink, "\\1").toStdString(); } if (!room->reply().isEmpty()) { diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index 6134262e..004cf26a 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -17,6 +17,7 @@ #include #include "ChatPage.h" +#include "Config.h" #include "EventAccessors.h" #include "Logging.h" #include "MainWindow.h" @@ -1586,10 +1587,31 @@ TimelineModel::setEdit(QString newEdit) auto msgType = mtx::accessors::msg_type(e); if (msgType == mtx::events::MessageType::Text || - msgType == mtx::events::MessageType::Notice) { - input()->setText(relatedInfo(newEdit).quoted_body); - } else if (msgType == mtx::events::MessageType::Emote) { - input()->setText("/me " + relatedInfo(newEdit).quoted_body); + msgType == mtx::events::MessageType::Notice || + msgType == mtx::events::MessageType::Emote) { + auto relInfo = relatedInfo(newEdit); + auto editText = relInfo.quoted_body; + + if (!relInfo.quoted_formatted_body.isEmpty()) { + auto matches = conf::strings::matrixToLink.globalMatch( + relInfo.quoted_formatted_body); + std::map reverseNameMapping; + while (matches.hasNext()) { + auto m = matches.next(); + reverseNameMapping[m.captured(2)] = m.captured(1); + } + + for (const auto &[user, link] : reverseNameMapping) { + // TODO(Nico): html unescape the user name + editText.replace( + user, QStringLiteral("[%1](%2)").arg(user, link)); + } + } + + if (msgType == mtx::events::MessageType::Emote) + input()->setText("/me " + editText); + else + input()->setText(editText); } else { input()->setText(""); }