去除 Markdown 多余空行
保留两个换行表示的正常空行,仅压缩普通正文中的连续多余换行
This commit is contained in:
@@ -15,6 +15,16 @@ export type ChatMarkdownMessageProps = {
|
||||
streaming?: boolean;
|
||||
};
|
||||
|
||||
/** 只压缩普通 Markdown 正文里多余的空行;代码块中的换行必须原样保留。 */
|
||||
function normalizeMarkdownBlankLines(text: string) {
|
||||
return text
|
||||
.split(/(```[\s\S]*?```)/g)
|
||||
.map((part, index) =>
|
||||
index % 2 === 1 ? part : part.replace(/\n{3,}/g, '\n\n'),
|
||||
)
|
||||
.join('');
|
||||
}
|
||||
|
||||
type MarkdownErrorBoundaryProps = {
|
||||
fallbackText: string;
|
||||
children: ReactNode;
|
||||
@@ -76,7 +86,7 @@ function MarkdownUnorderedList({ children }: { children?: ReactNode }) {
|
||||
<ListDepthContext.Provider value={depth + 1}>
|
||||
{/* 用真正的列表标记(`list-disc`)而不是手写 `'- '` 文本:手写前缀既没有悬挂缩进
|
||||
(换行后的第二行会顶回最左边),也不算列表语义(读屏读成普通文本)。 */}
|
||||
<ul className="m-0 mt-1 list-disc space-y-0.5 pl-4 first:mt-0">
|
||||
<ul className="m-0 mt-2 list-disc space-y-1 pl-4 first:mt-0">
|
||||
{children}
|
||||
</ul>
|
||||
</ListDepthContext.Provider>
|
||||
@@ -95,7 +105,7 @@ function MarkdownOrderedList({
|
||||
<ListDepthContext.Provider value={depth + 1}>
|
||||
<ol
|
||||
start={start}
|
||||
className="m-0 mt-1 list-decimal space-y-0.5 pl-5 first:mt-0"
|
||||
className="m-0 mt-2 list-decimal space-y-1 pl-5 first:mt-0"
|
||||
>
|
||||
{children}
|
||||
</ol>
|
||||
@@ -111,8 +121,8 @@ function MarkdownParagraph({ children }: { children?: ReactNode }) {
|
||||
paragraphPosition === 'first'
|
||||
? 'inline'
|
||||
: paragraphPosition === 'continuation'
|
||||
? 'mt-1'
|
||||
: 'mt-1 first:mt-0'
|
||||
? 'mt-2'
|
||||
: 'mt-2 first:mt-0'
|
||||
}`}
|
||||
>
|
||||
{children}
|
||||
@@ -128,8 +138,8 @@ function StreamingMarkdownParagraph({ children }: { children?: ReactNode }) {
|
||||
paragraphPosition === 'first'
|
||||
? 'inline'
|
||||
: paragraphPosition === 'continuation'
|
||||
? 'mt-1'
|
||||
: 'mt-1 first:mt-0'
|
||||
? 'mt-2'
|
||||
: 'mt-2 first:mt-0'
|
||||
}`}
|
||||
>
|
||||
{children}
|
||||
@@ -265,7 +275,7 @@ export function ChatMarkdownMessage({
|
||||
streaming ? streamingMarkdownComponents : markdownComponents
|
||||
}
|
||||
>
|
||||
{text}
|
||||
{normalizeMarkdownBlankLines(text)}
|
||||
</ReactMarkdown>
|
||||
</MarkdownErrorBoundary>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user