Trim whitespace from text messages

This commit is contained in:
Konstantinos Sideris 2018-09-12 14:20:12 +03:00
parent a0a76e352b
commit dd73a4b278
4 changed files with 13 additions and 20 deletions

View file

@ -337,10 +337,12 @@ utils::linkifyMessage(const QString &body)
return textString; return textString;
} }
std::string QString
utils::markdownToHtml(const std::string &text) 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. // Copy the null terminated output buffer.
std::string html(tmp_buf); std::string html(tmp_buf);
@ -348,17 +350,11 @@ utils::markdownToHtml(const std::string &text)
// The buffer is no longer needed. // The buffer is no longer needed.
free((char *)tmp_buf); free((char *)tmp_buf);
return html; return QString::fromStdString(html).trimmed();
} }
std::string std::string
utils::markdownToHtml(const QString &text) utils::stripHtml(const QString &text)
{ {
return utils::markdownToHtml(text.toStdString()); return text.trimmed().remove(QRegExp("<[^>]*>")).toStdString();
}
std::string
utils::stripHtml(const std::string &text)
{
return QString::fromStdString(text).remove(QRegExp("<[^>]*>")).toStdString();
} }

View file

@ -217,13 +217,10 @@ QString
linkifyMessage(const QString &body); linkifyMessage(const QString &body);
//! Convert the input markdown text to html. //! Convert the input markdown text to html.
std::string QString
markdownToHtml(const QString &text); markdownToHtml(const QString &text);
std::string
markdownToHtml(const std::string &text);
//! Return the plain text version of an html document. //! Return the plain text version of an html document.
std::string std::string
stripHtml(const std::string &text); stripHtml(const QString &text);
} }

View file

@ -272,7 +272,7 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty,
"You: ", userid, body, utils::descriptiveTime(timestamp), timestamp}; "You: ", userid, body, utils::descriptiveTime(timestamp), timestamp};
} }
body = QString::fromStdString(utils::markdownToHtml(body)); body = utils::markdownToHtml(body);
body = utils::linkifyMessage(body); body = utils::linkifyMessage(body);
generateTimestamp(timestamp); generateTimestamp(timestamp);

View file

@ -1237,7 +1237,7 @@ toRoomMessage<mtx::events::msg::Emote>(const PendingMessage &m)
mtx::events::msg::Emote emote; mtx::events::msg::Emote emote;
emote.body = utils::stripHtml(html); emote.body = utils::stripHtml(html);
emote.formatted_body = html; emote.formatted_body = html.toStdString();
return emote; return emote;
} }
@ -1262,7 +1262,7 @@ toRoomMessage<mtx::events::msg::Text>(const PendingMessage &m)
mtx::events::msg::Text text; mtx::events::msg::Text text;
text.body = utils::stripHtml(html); text.body = utils::stripHtml(html);
text.formatted_body = html; text.formatted_body = html.toStdString();
return text; return text;
} }