Strip paragraph tags

fixes #438
This commit is contained in:
Konstantinos Sideris 2018-09-13 11:02:54 +03:00
parent e88cfa1b20
commit bf4d559523
4 changed files with 21 additions and 22 deletions

View File

@ -350,11 +350,11 @@ utils::markdownToHtml(const QString &text)
// The buffer is no longer needed.
free((char *)tmp_buf);
return QString::fromStdString(html).trimmed();
}
auto result = QString::fromStdString(html).trimmed();
std::string
utils::stripHtml(const QString &text)
{
return text.trimmed().remove(QRegExp("<[^>]*>")).toStdString();
// Strip paragraph tags.
result.replace("<p>", "");
result.replace("</p>", "");
return result;
}

View File

@ -204,10 +204,10 @@ QString
getMessageBody(const RoomMessageT &event)
{
if (event.content.format.empty())
return QString::fromStdString(event.content.body);
return QString::fromStdString(event.content.body).toHtmlEscaped();
if (event.content.format != common::FORMAT_MSG_TYPE)
return QString::fromStdString(event.content.body);
return QString::fromStdString(event.content.body).toHtmlEscaped();
return QString::fromStdString(event.content.formatted_body);
}
@ -219,8 +219,4 @@ linkifyMessage(const QString &body);
//! Convert the input markdown text to html.
QString
markdownToHtml(const QString &text);
//! Return the plain text version of an html document.
std::string
stripHtml(const QString &text);
}

View File

@ -310,11 +310,12 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty,
auto displayName = Cache::displayName(room_id_, userid);
auto timestamp = QDateTime::currentDateTime();
// Generate the html body to rendered.
// Generate the html body to be rendered.
auto formatted_body = utils::markdownToHtml(body);
// Extract the plain text version for the sidebar.
body = QString::fromStdString(utils::stripHtml(formatted_body));
// Escape html if the input is not formatted.
if (formatted_body == body.trimmed().toHtmlEscaped())
formatted_body = body.toHtmlEscaped();
if (ty == mtx::events::MessageType::Emote) {
formatted_body = QString("<em>%1</em>").arg(formatted_body);
@ -651,9 +652,7 @@ TimelineItem::markReceived(bool isEncrypted)
void
TimelineItem::generateBody(const QString &body)
{
QString content("<span>%1</span>");
body_ = new TextLabel(content.arg(replaceEmoji(body)), this);
body_ = new TextLabel(replaceEmoji(body), this);
body_->setFont(font_);
body_->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextBrowserInteraction);
}

View File

@ -1236,7 +1236,9 @@ toRoomMessage<mtx::events::msg::Emote>(const PendingMessage &m)
auto html = utils::markdownToHtml(m.body);
mtx::events::msg::Emote emote;
emote.body = utils::stripHtml(html);
emote.body = m.body.trimmed().toStdString();
if (html != m.body.trimmed().toHtmlEscaped())
emote.formatted_body = html.toStdString();
return emote;
@ -1261,7 +1263,9 @@ toRoomMessage<mtx::events::msg::Text>(const PendingMessage &m)
auto html = utils::markdownToHtml(m.body);
mtx::events::msg::Text text;
text.body = utils::stripHtml(html);
text.body = m.body.trimmed().toStdString();
if (html != m.body.trimmed().toHtmlEscaped())
text.formatted_body = html.toStdString();
return text;