diff --git a/ChatView/ChatUtils.cpp b/ChatView/ChatUtils.cpp index 2368992..fc08307 100644 --- a/ChatView/ChatUtils.cpp +++ b/ChatView/ChatUtils.cpp @@ -19,22 +19,34 @@ QString ChatUtils::getSafeMarkdownText(const QString &text) const return text; } - bool needsSanitization = false; - for (const QChar &ch : text) { - if (ch.isNull() || (!ch.isPrint() && ch != '\n' && ch != '\t' && ch != '\r' && ch != ' ')) { - needsSanitization = true; - break; - } - } - - if (!needsSanitization) { - return text; - } - QString safeText; - safeText.reserve(text.size()); + safeText.reserve(text.size() + 16); + + bool inFenced = false; + bool inInline = false; + + for (int i = 0; i < text.size(); ++i) { + const QChar ch = text[i]; + + if (!inInline && i + 2 < text.size() + && text[i] == '`' && text[i + 1] == '`' && text[i + 2] == '`') { + safeText.append(QStringLiteral("```")); + inFenced = !inFenced; + i += 2; + continue; + } + + if (!inFenced && ch == '`') { + safeText.append(ch); + inInline = !inInline; + continue; + } + + if (!inFenced && !inInline && ch == '<') { + safeText.append(QStringLiteral("<")); + continue; + } - for (QChar ch : text) { if (ch.isNull()) { safeText.append(' '); } else if (ch == '\n' || ch == '\t' || ch == '\r' || ch == ' ') {