2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 06:26:18 +00:00

Apply markdown only when sending the message.

This commit is contained in:
John Preston
2018-05-31 21:28:37 +03:00
parent bfc748cd31
commit 43d19920e0
11 changed files with 360 additions and 174 deletions

View File

@@ -571,6 +571,7 @@ void MessageLinksParser::parse() {
const auto &textWithTags = _field->getTextWithTags();
const auto &text = textWithTags.text;
const auto &tags = textWithTags.tags;
const auto &markdownTags = _field->getMarkdownTags();
if (text.isEmpty()) {
_list = QStringList();
return;
@@ -578,9 +579,8 @@ void MessageLinksParser::parse() {
auto ranges = QVector<LinkRange>();
const auto tagsBegin = tags.begin();
auto tag = tags.begin();
const auto tagsEnd = tags.end();
auto tag = tagsBegin;
const auto processTag = [&] {
Expects(tag != tagsEnd);
@@ -605,6 +605,25 @@ void MessageLinksParser::parse() {
return true;
};
auto markdownTag = markdownTags.begin();
const auto markdownTagsEnd = markdownTags.end();
const auto markdownTagsAllow = [&](int from, int length) {
while (markdownTag != markdownTagsEnd
&& (markdownTag->start + markdownTag->length <= from
|| !markdownTag->closed)) {
++markdownTag;
continue;
}
if (markdownTag == markdownTagsEnd
|| markdownTag->start >= from + length) {
return true;
}
// Ignore http-links that are completely inside some tags.
// This will allow sending http://test.com/__test__/test correctly.
return (markdownTag->start > from
|| markdownTag->start + markdownTag->length < from + length);
};
const auto len = text.size();
const QChar *start = text.unicode(), *end = start + text.size();
for (auto offset = 0, matchOffset = offset; offset < len;) {
@@ -671,7 +690,9 @@ void MessageLinksParser::parse() {
};
processTagsBefore(domainOffset);
if (!hasTagsIntersection(range.start + range.length)) {
ranges.push_back(range);
if (markdownTagsAllow(range.start, range.length)) {
ranges.push_back(range);
}
}
offset = matchOffset = p - start;
}