mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2026-05-30 02:49:12 -04:00
fix: Improve parsing markdown
This commit is contained in:
@@ -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 == ' ') {
|
||||
|
||||
Reference in New Issue
Block a user