| 15 | } |
| 16 | |
| 17 | QString ChatUtils::getSafeMarkdownText(const QString &text) const |
| 18 | { |
| 19 | if (text.isEmpty()) { |
| 20 | return text; |
| 21 | } |
| 22 | |
| 23 | QString safeText; |
| 24 | safeText.reserve(text.size() + 16); |
| 25 | |
| 26 | bool inFenced = false; |
| 27 | bool inInline = false; |
| 28 | |
| 29 | for (int i = 0; i < text.size(); ++i) { |
| 30 | const QChar ch = text[i]; |
| 31 | |
| 32 | if (!inInline && i + 2 < text.size() |
| 33 | && text[i] == '`' && text[i + 1] == '`' && text[i + 2] == '`') { |
| 34 | safeText.append(QStringLiteral("```")); |
| 35 | inFenced = !inFenced; |
| 36 | i += 2; |
| 37 | continue; |
| 38 | } |
| 39 | |
| 40 | if (!inFenced && ch == '`') { |
| 41 | safeText.append(ch); |
| 42 | inInline = !inInline; |
| 43 | continue; |
| 44 | } |
| 45 | |
| 46 | if (!inFenced && !inInline && ch == '<') { |
| 47 | safeText.append(QStringLiteral("<")); |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | if (ch.isNull()) { |
| 52 | safeText.append(' '); |
| 53 | } else if (ch == '\n' || ch == '\t' || ch == '\r' || ch == ' ') { |
| 54 | safeText.append(ch); |
| 55 | } else if (ch.isPrint()) { |
| 56 | safeText.append(ch); |
| 57 | } else { |
| 58 | safeText.append(QChar(0xFFFD)); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return safeText; |
| 63 | } |
| 64 | |
| 65 | } // namespace QodeAssist::Chat |