diff --git a/gui/chatblockmodel.cpp b/gui/chatblockmodel.cpp index 69a46da..fc82600 100644 --- a/gui/chatblockmodel.cpp +++ b/gui/chatblockmodel.cpp @@ -538,20 +538,21 @@ void ChatBlockModel::incrementalAppend(int blockIndex, const QString &line) const QString defaultColor = getDefaultTextColor(); const QString escapedLine = line.toHtmlEscaped(); - if (!block.segments.isEmpty() && block.segments.last().type == BlockSegment::Text - && !rs.inFence) { - // Append to existing prose segment — strip closing , add newline + content + // Only extend the last segment if it is an in-progress prose segment + // (identified by its trailing ). A finalized code fence segment + // ends in and must not be overwritten. + bool canExtend = !block.segments.isEmpty() + && block.segments.last().type == BlockSegment::Text + && block.segments.last().html.endsWith(QStringLiteral("")); + + if (canExtend) { QString &segHtml = block.segments.last().html; - // Remove trailing to append - if (segHtml.endsWith(QStringLiteral(""))) { - segHtml.chop(6); - segHtml += '\n' + escapedLine + QStringLiteral(""); - } else { - // Shouldn't happen, but fall back to full rebuild - segHtml = escapeForHtml(rs.proseLines.join('\n')); - } + segHtml.chop(6); // remove trailing + segHtml += '\n' + escapedLine + QStringLiteral(""); } else { - // Start a new text segment + // Start a new prose segment + rs.proseLines.clear(); + rs.proseLines.append(line); BlockSegment seg; seg.type = BlockSegment::Text; seg.html = QStringLiteral("
")