diff --git a/ChatView/ChatUtils.cpp b/ChatView/ChatUtils.cpp index 57d22b0..d6d0409 100644 --- a/ChatView/ChatUtils.cpp +++ b/ChatView/ChatUtils.cpp @@ -29,4 +29,25 @@ void ChatUtils::copyToClipboard(const QString &text) QGuiApplication::clipboard()->setText(text); } +QString ChatUtils::getSafeMarkdownText(const QString &text) const +{ + // TODO replace to QTextMarkdownImporter after QtC on Qt6.9.0 + QString safeText; + safeText.reserve(text.size()); + + for (QChar ch : text) { + if (ch.isNull()) { + safeText.append(' '); + } else if (ch == '\n' || ch == '\t' || ch == '\r' || ch == ' ') { + safeText.append(ch); + } else if (ch.isPrint()) { + safeText.append(ch); + } else { + safeText.append(QChar(0xFFFD)); + } + } + + return safeText; +} + } // namespace QodeAssist::Chat diff --git a/ChatView/ChatUtils.h b/ChatView/ChatUtils.h index e8ec539..9f5946d 100644 --- a/ChatView/ChatUtils.h +++ b/ChatView/ChatUtils.h @@ -34,6 +34,7 @@ public: : QObject(parent) {}; Q_INVOKABLE void copyToClipboard(const QString &text); + Q_INVOKABLE QString getSafeMarkdownText(const QString &text) const; }; } // namespace QodeAssist::Chat diff --git a/ChatView/qml/ChatItem.qml b/ChatView/qml/ChatItem.qml index 03af679..d39af9d 100644 --- a/ChatView/qml/ChatItem.qml +++ b/ChatView/qml/ChatItem.qml @@ -158,9 +158,12 @@ Rectangle { height: implicitHeight + 10 verticalAlignment: Text.AlignVCenter leftPadding: 10 - text: itemData.text - } + text: utils.getSafeMarkdownText(itemData.text) + ChatUtils { + id: utils + } + } component CodeBlockComponent : CodeBlock { id: codeblock