修复聊天代码节点属性泄漏

从 react-markdown code 组件属性中剔除 node 元数据。

新增回归测试,确保渲染后的代码节点不输出 node 属性。
This commit is contained in:
2026-09-04 12:54:03 +08:00
parent 08ea5962ac
commit da3033db21
2 changed files with 14 additions and 1 deletions
@@ -79,7 +79,7 @@ const markdownComponents: Components = {
{children}
</pre>
),
code: ({ className, children, ...props }) => {
code: ({ className, children, node: _node, ...props }) => {
const isBlock = Boolean(className?.includes('language-'));
return isBlock ? (
<code {...props} className="font-mono whitespace-pre">
@@ -21,6 +21,19 @@ describe('ChatMarkdownMessage', () => {
);
});
it('不会把 react-markdown 的 node 元数据泄漏到代码节点', () => {
const { container } = render(
<ChatMarkdownMessage
role="assistant"
text={'```ts\nconst answer = 42;\n```'}
/>,
);
expect(
container.querySelector('pre code')?.getAttribute('node'),
).toBeNull();
});
it('保留流式 assistant 文本并标记 streaming 状态', () => {
const { container } = render(
<ChatMarkdownMessage