fix: 修复单个\n输入,渲染时不会换行的问题

This commit is contained in:
肖应宇 2026-04-24 10:27:35 +08:00 committed by MT-Fire
parent 6853ed71bc
commit 31f4bdb99a
2 changed files with 20 additions and 3 deletions

View File

@ -28,6 +28,7 @@ import { resolveArtifactURL } from "@/core/artifacts/utils";
import { useI18n } from "@/core/i18n/hooks";
import {
extractContentFromMessage,
normalizeHumanMessageDisplayText,
extractReasoningContentFromMessage,
parseUploadedFiles,
stripPriorityHintSuffix,
@ -167,9 +168,11 @@ function MessageContent_({
const contentToDisplay = useMemo(() => {
if (isHuman) {
return rawContent
? stripPriorityHintSuffix(stripUploadedFilesTag(rawContent))
: "";
if (!rawContent) {
return "";
}
const cleaned = stripPriorityHintSuffix(stripUploadedFilesTag(rawContent));
return normalizeHumanMessageDisplayText(cleaned);
}
return rawContent ?? "";
}, [rawContent, isHuman]);

View File

@ -364,6 +364,20 @@ export function stripPriorityHintSuffix(content: string): string {
.trim();
}
/**
* Normalize human-authored message text for markdown rendering.
* - Decode literal "\n" into real line breaks.
* - Split Chinese-numbered items (e.g. "1...") into separate paragraphs.
*/
export function normalizeHumanMessageDisplayText(content: string): string {
return content
.replace(/\\n/g, "\n")
.replace(/\r\n?/g, "\n")
.replace(/\n(?=\d+[)]\s*)/g, "\n\n")
.replace(/\n{3,}/g, "\n\n")
.trim();
}
export function parseUploadedFiles(content: string): FileInMessage[] {
// Match <uploaded_files>...</uploaded_files> tag
const uploadedFilesRegex = /<uploaded_files>([\s\S]*?)<\/uploaded_files>/;