summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-09-07 20:05:30 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-09-07 20:05:30 +0300
commit9e8f0b7409ff34b1235908b4c9d619b2a43d7a11 (patch)
tree558863c4a701f159492b0ce700db3b29c8969e1c /src
parentFix double href links on username pills (diff)
downloadnheko-9e8f0b7409ff34b1235908b4c9d619b2a43d7a11.tar.xz
Initial support for sending markdown formatted messages
fixes #283
Diffstat (limited to 'src')
-rw-r--r--src/Utils.cpp23
-rw-r--r--src/Utils.h11
-rw-r--r--src/timeline/TimelineItem.cpp10
-rw-r--r--src/timeline/TimelineView.cpp15
4 files changed, 52 insertions, 7 deletions
diff --git a/src/Utils.cpp b/src/Utils.cpp

index 40039179..5d5508b6 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp
@@ -3,10 +3,12 @@ #include <QApplication> #include <QDesktopWidget> #include <QSettings> +#include <QTextDocument> #include <QXmlStreamReader> #include <cmath> #include <boost/variant.hpp> +#include <maddy/parser.h> #include "Config.h" @@ -327,3 +329,24 @@ utils::linkifyMessage(const QString &body) return textString; } + +std::string +utils::markdownToHtml(const std::string &text) +{ + std::stringstream markdownInput(text); + auto parser = std::make_shared<maddy::Parser>(); + + return parser->Parse(markdownInput); +} + +std::string +utils::markdownToHtml(const QString &text) +{ + return markdownToHtml(text.toStdString()); +} + +std::string +utils::stripHtml(const std::string &text) +{ + return QString::fromStdString(text).remove(QRegExp("<[^>]*>")).toStdString(); +} diff --git a/src/Utils.h b/src/Utils.h
index 62af310b..2ac6d41b 100644 --- a/src/Utils.h +++ b/src/Utils.h
@@ -215,4 +215,15 @@ getMessageBody(const RoomMessageT &event) //! Replace raw URLs in text with HTML link tags. QString linkifyMessage(const QString &body); + +//! Convert the input markdown text to html. +std::string +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); } diff --git a/src/timeline/TimelineItem.cpp b/src/timeline/TimelineItem.cpp
index 267dea15..8e0401a8 100644 --- a/src/timeline/TimelineItem.cpp +++ b/src/timeline/TimelineItem.cpp
@@ -265,16 +265,16 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty, auto timestamp = QDateTime::currentDateTime(); if (ty == mtx::events::MessageType::Emote) { - body = QString("* %1 %2").arg(displayName).arg(body); + body = QString("%1 %2").arg(displayName).arg(body); descriptionMsg_ = {"", userid, body, utils::descriptiveTime(timestamp), timestamp}; } else { descriptionMsg_ = { "You: ", userid, body, utils::descriptiveTime(timestamp), timestamp}; } - body = body.toHtmlEscaped(); - body.replace(conf::strings::url_regex, conf::strings::url_html); - body.replace("\n", "<br/>"); + body = QString::fromStdString(utils::markdownToHtml(body)); + body = utils::linkifyMessage(body); + generateTimestamp(timestamp); if (withSender) { @@ -489,7 +489,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote> auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts); auto displayName = Cache::displayName(room_id_, sender); - auto emoteMsg = QString("* %1 %2").arg(displayName).arg(formatted_body); + auto emoteMsg = QString("%1 %2").arg(displayName).arg(formatted_body); descriptionMsg_ = {"", sender, emoteMsg, utils::descriptiveTime(timestamp), timestamp}; diff --git a/src/timeline/TimelineView.cpp b/src/timeline/TimelineView.cpp
index bf828a8e..d09f5d5f 100644 --- a/src/timeline/TimelineView.cpp +++ b/src/timeline/TimelineView.cpp
@@ -1233,8 +1233,12 @@ template<> mtx::events::msg::Emote toRoomMessage<mtx::events::msg::Emote>(const PendingMessage &m) { + auto html = utils::markdownToHtml(m.body); + mtx::events::msg::Emote emote; - emote.body = m.body.toStdString(); + emote.body = utils::stripHtml(html); + emote.formatted_body = html; + return emote; } @@ -1254,8 +1258,15 @@ template<> mtx::events::msg::Text toRoomMessage<mtx::events::msg::Text>(const PendingMessage &m) { + auto html = utils::markdownToHtml(m.body); + mtx::events::msg::Text text; - text.body = m.body.toStdString(); + text.body = utils::stripHtml(html); + text.formatted_body = html; + + std::cout << "body: " << text.body << std::endl; + std::cout << "formatted_body: " << text.formatted_body << std::endl; + return text; }