fix: Improve parsing markdown

This commit is contained in:
Petr Mironychev
2026-05-28 10:50:25 +02:00
parent 8cbeb7132e
commit 282f48d9fb

View File

@@ -19,22 +19,34 @@ QString ChatUtils::getSafeMarkdownText(const QString &text) const
return text; 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; 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("&lt;"));
continue;
}
for (QChar ch : text) {
if (ch.isNull()) { if (ch.isNull()) {
safeText.append(' '); safeText.append(' ');
} else if (ch == '\n' || ch == '\t' || ch == '\r' || ch == ' ') { } else if (ch == '\n' || ch == '\t' || ch == '\r' || ch == ' ') {