Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(chat): code blocks in chat replies no longer get a blank line before each # comment (#17137)

Hazem Adel50e8698a26ad
diff --git a/packages/web/src/components/prompt-kit/markdown.tsx b/packages/web/src/components/prompt-kit/markdown.tsx
index 4ed5e505a19..81315e123fe 100644
--- a/packages/web/src/components/prompt-kit/markdown.tsx
+++ b/packages/web/src/components/prompt-kit/markdown.tsx
@@ -10,14 +10,8 @@ import { CodeBlock, CodeBlockCode, CodeBlockGroup } from './code-block';
import { Source } from './source';
function normalizeMarkdownSpacing(markdown: string): string {
- let text = markdown;
-
- // Ensure headings have a blank line before them (fixes "text\n## Heading" streaming artifact).
- // Uses multiline flag so ^ matches after every \n; only touches lines that start with #.
- text = text.replace(/^(#{1,6}\s)/gm, '\n$1');
-
// Ensure blank lines between non-empty content lines (except inside tables/code/lists)
- const lines = text.split('\n');
+ const lines = markdown.split('\n');
const result: string[] = [];
let inCodeBlock = false;
@@ -53,7 +47,14 @@ function normalizeMarkdownSpacing(markdown: string): string {
/^\s*[-*+]\s/.test(prevLine) ||
/^\s*\d+[.)]\s/.test(prevLine);
- if (i > 0 && prevLine !== '' && trimmed !== '' && !prevIsTableOrList) {
+ const isHeading = /^#{1,6}\s/.test(line);
+
+ if (
+ i > 0 &&
+ prevLine !== '' &&
+ trimmed !== '' &&
+ (!prevIsTableOrList || isHeading)
+ ) {
result.push('');
}
diff --git a/packages/web/test/components/prompt-kit/markdown-code-comments.test.tsx b/packages/web/test/components/prompt-kit/markdown-code-comments.test.tsx
new file mode 100644
index 00000000000..a101e4fdc86
--- /dev/null
+++ b/packages/web/test/components/prompt-kit/markdown-code-comments.test.tsx
@@ -0,0 +1,50 @@
+/**
+ * @vitest-environment jsdom
+ */
+import { cleanup, render, screen } from '@testing-library/react';
+import React from 'react';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+import { Markdown } from '@/components/prompt-kit/markdown';
+
+vi.mock('@/components/prompt-kit/code-block', () => ({
+ CodeBlock: ({ children }: { children: React.ReactNode }) => (
+ <div>{children}</div>
+ ),
+ CodeBlockGroup: ({ children }: { children: React.ReactNode }) => (
+ <div>{children}</div>
+ ),
+ CodeBlockCode: ({ code }: { code: string }) => (
+ <pre data-testid="code">{code}</pre>
+ ),
+}));
+
+afterEach(() => {
+ cleanup();
+});
+
+describe('Markdown code blocks', () => {
+ it('keeps # comment lines in a code block as written', () => {
+ render(<Markdown>{SCRIPT}</Markdown>);
+
+ expect(screen.getByTestId('code').textContent?.trimEnd()).toBe(
+ 'echo start\n# install deps\nnpm install',
+ );
+ });
+
+ it('still renders a heading written right under a line of text', () => {
+ render(<Markdown>{'Some intro\n## Setup'}</Markdown>);
+
+ expect(screen.getByRole('heading', { name: 'Setup' })).toBeTruthy();
+ });
+});
+
+const SCRIPT = [
+ 'Run this:',
+ '',
+ '```bash',
+ 'echo start',
+ '# install deps',
+ 'npm install',
+ '```',
+].join('\n');