Skip to content
Snippets Groups Projects
Commit 65d3befa authored by Page Magnier-Slimani's avatar Page Magnier-Slimani Committed by Page Magnier-Slimani
Browse files

messages display: fix the display of links

The links are now postprocessed to compensate for the malfunctions of
md4c. It now displays properly even with a + or - in it. It's also
checked by the unittests.

GitLab: #1903
Change-Id: I0d7f520a7a3fe06fb89107fe6b08f83325218cd4
parent 84ac5dba
No related branches found
No related tags found
No related merge requests found
......@@ -167,6 +167,38 @@ MessageParser::preprocessMarkdown(QString& markdown)
markdown += block.second;
}
}
const QRegularExpression linkRegex(R"((http[s]?://[^\s]*[\+\-_][^\s]*|www\.[^\s]*[\+\-_][^\s]*))");
const QString anchorTagTemplate("<a href=\"%1\">%1</a>");
QByteArray
MessageParser::postprocessHTML(const QByteArray& html)
{
// HACK : https://github.com/mity/md4c/issues/251
// Check for any links that haven't been properly detected by md4c.
// We can do this by checking for any text that starts with "http" or "www" and contains a + or
// a -. If we find any, we insert a <a> tag around the link.
// This function could become obsolete if the md4c library fixes the issue it's addressing.
QByteArray processedHtml = html;
QRegularExpressionMatchIterator it = linkRegex.globalMatch(html);
int offset = 0;
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
QString link = match.captured(0);
int pTagIndex = link.indexOf("</p>");
QString anchorTag = anchorTagTemplate.arg(pTagIndex != -1 ? link.left(pTagIndex) : link);
if (pTagIndex != -1) {
anchorTag += "</p>";
}
processedHtml.replace(match.capturedStart(0) + offset,
match.capturedLength(0),
anchorTag.toUtf8());
offset += anchorTag.toUtf8().length() - match.capturedLength(0);
}
return processedHtml;
}
QString
MessageParser::markdownToHtml(const char* markdown)
......@@ -179,5 +211,6 @@ MessageParser::markdownToHtml(const char* markdown)
}
QByteArray array;
const int result = md_html(markdown, MD_SIZE(data_len), &htmlChunkCb, &array, md_flags, 0);
array = postprocessHTML(array);
return result == 0 ? QString::fromUtf8(array) : QString();
}
......@@ -60,6 +60,9 @@ private:
// Preprocess the markdown message (e.g. handle line breaks).
void preprocessMarkdown(QString& markdown);
// Postprocess HTML to account for issues in md4c
QByteArray postprocessHTML(const QByteArray& html);
// Transform markdown syntax into HTML.
QString markdownToHtml(const char* markdown);
......
......@@ -107,6 +107,41 @@ TEST_F(MessageParserFixture, ALinkIsParsedCorrectly)
// The rest of the link info is not tested here.
}
/*!
* WHEN We parse a text body with a link and a + or a -.
* THEN The HTML body should be returned correctly including the link even if MD4C doesn't detect it.
*/
TEST_F(MessageParserFixture, AComplexLinkIsParsedCorrectly)
{
auto linkColor = QColor::fromRgb(0, 0, 255);
auto backgroundColor = QColor::fromRgb(0, 0, 255);
QSignalSpy messageParsedSpy(globalEnv.messageParser.data(), &MessageParser::messageParsed);
QSignalSpy linkInfoReadySpy(globalEnv.messageParser.data(), &MessageParser::linkInfoReady);
// Parse a message with a link containing a + or a -.
globalEnv.messageParser->parseMessage("msgId_03",
"https://review.jami.net/q/status:open+-is:wip",
false,
linkColor,
backgroundColor);
// Wait for the messageParsed signal which should be emitted once.
messageParsedSpy.wait();
EXPECT_EQ(messageParsedSpy.count(), 1);
QList<QVariant> messageParserArguments = messageParsedSpy.takeFirst();
EXPECT_TRUE(messageParserArguments.at(0).typeId() == qMetaTypeId<QString>());
EXPECT_EQ(messageParserArguments.at(0).toString(), "msgId_03");
EXPECT_TRUE(messageParserArguments.at(1).typeId() == qMetaTypeId<QString>());
EXPECT_EQ(messageParserArguments.at(1).toString(),
"<style>a{color:#0000ff;}</style><p><a "
"href=\"https://review.jami.net/q/status:open+-is:wip\">https://review.jami.net/q/"
"status:open+-is:wip</a></p>\n");
// The rest of the link info is not tested here.
}
/*!
* WHEN We parse a text body with end of line characters.
* THEN The HTML body should be returned correctly with the end of line characters.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment