feat: strip prefixes and suffixes in links (#5486)
This commit is contained in:
@@ -94,10 +94,10 @@ MessageBuilder::MessageBuilder(SystemMessageTag, const QString &text,
|
||||
text.split(QRegularExpression("\\s"), Qt::SkipEmptyParts);
|
||||
for (const auto &word : textFragments)
|
||||
{
|
||||
LinkParser parser(word);
|
||||
if (parser.result())
|
||||
auto link = linkparser::parse(word);
|
||||
if (link)
|
||||
{
|
||||
this->addLink(*parser.result());
|
||||
this->addLink(*link, word);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -637,30 +637,45 @@ std::unique_ptr<MessageElement> MessageBuilder::releaseBack()
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void MessageBuilder::addLink(const ParsedLink &parsedLink)
|
||||
void MessageBuilder::addLink(const linkparser::Parsed &parsedLink,
|
||||
const QString &source)
|
||||
{
|
||||
QString lowercaseLinkString;
|
||||
QString origLink = parsedLink.source;
|
||||
QString origLink = parsedLink.link.toString();
|
||||
QString fullUrl;
|
||||
|
||||
if (parsedLink.protocol.isNull())
|
||||
{
|
||||
fullUrl = QStringLiteral("http://") + parsedLink.source;
|
||||
fullUrl = QStringLiteral("http://") + origLink;
|
||||
}
|
||||
else
|
||||
{
|
||||
lowercaseLinkString += parsedLink.protocol;
|
||||
fullUrl = parsedLink.source;
|
||||
fullUrl = origLink;
|
||||
}
|
||||
|
||||
lowercaseLinkString += parsedLink.host.toString().toLower();
|
||||
lowercaseLinkString += parsedLink.rest;
|
||||
|
||||
auto textColor = MessageColor(MessageColor::Link);
|
||||
|
||||
if (parsedLink.hasPrefix(source))
|
||||
{
|
||||
this->emplace<TextElement>(parsedLink.prefix(source).toString(),
|
||||
MessageElementFlag::Text, this->textColor_)
|
||||
->setTrailingSpace(false);
|
||||
}
|
||||
auto *el = this->emplace<LinkElement>(
|
||||
LinkElement::Parsed{.lowercase = lowercaseLinkString,
|
||||
.original = origLink},
|
||||
fullUrl, MessageElementFlag::Text, textColor);
|
||||
if (parsedLink.hasSuffix(source))
|
||||
{
|
||||
el->setTrailingSpace(false);
|
||||
this->emplace<TextElement>(parsedLink.suffix(source).toString(),
|
||||
MessageElementFlag::Text, this->textColor_);
|
||||
}
|
||||
|
||||
getIApp()->getLinkResolver()->resolve(el->linkInfo());
|
||||
}
|
||||
|
||||
@@ -676,20 +691,18 @@ void MessageBuilder::addIrcMessageText(const QString &text)
|
||||
int fg = -1;
|
||||
int bg = -1;
|
||||
|
||||
for (const auto &word : words)
|
||||
for (const auto &string : words)
|
||||
{
|
||||
if (word.isEmpty())
|
||||
if (string.isEmpty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
auto string = QString(word);
|
||||
|
||||
// Actually just text
|
||||
LinkParser parser(string);
|
||||
if (parser.result())
|
||||
auto link = linkparser::parse(string);
|
||||
if (link)
|
||||
{
|
||||
this->addLink(*parser.result());
|
||||
this->addLink(*link, string);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -772,15 +785,13 @@ void MessageBuilder::addTextOrEmoji(EmotePtr emote)
|
||||
this->emplace<EmoteElement>(emote, MessageElementFlag::EmojiAll);
|
||||
}
|
||||
|
||||
void MessageBuilder::addTextOrEmoji(const QString &string_)
|
||||
void MessageBuilder::addTextOrEmoji(const QString &string)
|
||||
{
|
||||
auto string = QString(string_);
|
||||
|
||||
// Actually just text
|
||||
LinkParser linkParser(string);
|
||||
if (linkParser.result())
|
||||
auto link = linkparser::parse(string);
|
||||
if (link)
|
||||
{
|
||||
this->addLink(*linkParser.result());
|
||||
this->addLink(*link, string);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user