mirror of
https://git.jami.net/savoirfairelinux/jami-client-qt.git
synced 2025-03-28 14:56:19 +01:00
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
This commit is contained in:
parent
84ac5dba02
commit
65d3befad8
3 changed files with 71 additions and 0 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Reference in a new issue