summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-09-12 14:20:12 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-09-12 14:20:12 +0300
commitdd73a4b2786a0b934f338fbf9d07032a13ac6a94 (patch)
tree4054e9ef7d755187145f48a387e8045b8ef9af89 /src
parentEnable html attributes (diff)
downloadnheko-dd73a4b2786a0b934f338fbf9d07032a13ac6a94.tar.xz
Trim whitespace from text messages
Diffstat (limited to 'src')
-rw-r--r--src/Utils.cpp20
-rw-r--r--src/Utils.h7
-rw-r--r--src/timeline/TimelineItem.cpp2
-rw-r--r--src/timeline/TimelineView.cpp4
4 files changed, 13 insertions, 20 deletions
diff --git a/src/Utils.cpp b/src/Utils.cpp

index 84978bae..5a0558f4 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp
@@ -337,10 +337,12 @@ utils::linkifyMessage(const QString &body) return textString; } -std::string -utils::markdownToHtml(const std::string &text) +QString +utils::markdownToHtml(const QString &text) { - const char *tmp_buf = cmark_markdown_to_html(text.c_str(), text.size(), CMARK_OPT_DEFAULT); + const auto str = text.toUtf8(); + const char *tmp_buf = + cmark_markdown_to_html(str.constData(), str.size(), CMARK_OPT_DEFAULT); // Copy the null terminated output buffer. std::string html(tmp_buf); @@ -348,17 +350,11 @@ utils::markdownToHtml(const std::string &text) // The buffer is no longer needed. free((char *)tmp_buf); - return html; -} - -std::string -utils::markdownToHtml(const QString &text) -{ - return utils::markdownToHtml(text.toStdString()); + return QString::fromStdString(html).trimmed(); } std::string -utils::stripHtml(const std::string &text) +utils::stripHtml(const QString &text) { - return QString::fromStdString(text).remove(QRegExp("<[^>]*>")).toStdString(); + return text.trimmed().remove(QRegExp("<[^>]*>")).toStdString(); } diff --git a/src/Utils.h b/src/Utils.h
index 2ac6d41b..93484ca3 100644 --- a/src/Utils.h +++ b/src/Utils.h
@@ -217,13 +217,10 @@ QString linkifyMessage(const QString &body); //! Convert the input markdown text to html. -std::string +QString markdownToHtml(const QString &text); -std::string -markdownToHtml(const std::string &text); - //! Return the plain text version of an html document. std::string -stripHtml(const std::string &text); +stripHtml(const QString &text); } diff --git a/src/timeline/TimelineItem.cpp b/src/timeline/TimelineItem.cpp
index 5a339541..8f44b7e8 100644 --- a/src/timeline/TimelineItem.cpp +++ b/src/timeline/TimelineItem.cpp
@@ -272,7 +272,7 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty, "You: ", userid, body, utils::descriptiveTime(timestamp), timestamp}; } - body = QString::fromStdString(utils::markdownToHtml(body)); + body = utils::markdownToHtml(body); body = utils::linkifyMessage(body); generateTimestamp(timestamp); diff --git a/src/timeline/TimelineView.cpp b/src/timeline/TimelineView.cpp
index 87fa18db..e50bfa21 100644 --- a/src/timeline/TimelineView.cpp +++ b/src/timeline/TimelineView.cpp
@@ -1237,7 +1237,7 @@ toRoomMessage<mtx::events::msg::Emote>(const PendingMessage &m) mtx::events::msg::Emote emote; emote.body = utils::stripHtml(html); - emote.formatted_body = html; + emote.formatted_body = html.toStdString(); return emote; } @@ -1262,7 +1262,7 @@ toRoomMessage<mtx::events::msg::Text>(const PendingMessage &m) mtx::events::msg::Text text; text.body = utils::stripHtml(html); - text.formatted_body = html; + text.formatted_body = html.toStdString(); return text; }