Skip to content

fix(react): preserve newlines in markdown code blocks - #3441

Closed
giulio-leone wants to merge 2 commits into
CopilotKit:mainfrom
giulio-leone:fix/markdown-code-block-newlines
Closed

giulio-leone wants to merge 2 commits into
CopilotKit:mainfrom
giulio-leone:fix/markdown-code-block-newlines

Conversation

@giulio-leone

Copy link
Copy Markdown
Contributor

Problem

After adding the Tailwind 4 prefix(cpk) configuration in commit 5f941db, the preflight CSS reset no longer applies white-space: pre to <pre> elements inside the [data-copilotkit] scope. This causes code blocks rendered by Streamdown to collapse newlines, making them illegible.

Root Cause

Tailwind 4 with prefix(cpk) scopes the preflight reset under the prefix namespace. The browser default white-space: pre for <pre> elements gets overridden by the scoped reset, and the existing CSS rule for code blocks only set margin resets:

[data-copilotkit] div[data-streamdown="code-block"] > pre {
  @apply cpk:mt-0 cpk:mb-0;  /* margin only, no whitespace preservation */
}

Fix

Add explicit white-space: pre declarations to both <pre> and <code> elements within Streamdown code blocks, along with overflow-x: auto for horizontal scrolling on long lines:

[data-copilotkit] div[data-streamdown="code-block"] > pre {
  @apply cpk:mt-0 cpk:mb-0;
  overflow-x: auto;
}

[data-copilotkit] div[data-streamdown="code-block"] > pre code {
}

Verification

  • Pattern is consistent with existing cpk:whitespace-pre-wrap usage in WildcardToolCallRender.tsx
  • CSS-only fix — no component logic changes, no risk of runtime regressions

Closes #3330

After adding the Tailwind 4 `prefix(cpk)` configuration, the preflight
CSS reset no longer applies `white-space: pre` to `<pre>` elements
inside the `[data-copilotkit]` scope. This causes code blocks rendered
by Streamdown to lose their newline/whitespace formatting, making them
illegible.

Add explicit `white-space: pre` declarations to both `<pre>` and
`<code>` elements within Streamdown code blocks to ensure whitespace
preservation regardless of the Tailwind prefix scoping.

Closes #3330

Co-authored-by: Copilot <[email protected]>
@vercel

vercel Bot commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

Someone is attempting to deploy a commit to the CopilotKit Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Mar 13, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5a3c894

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 23 packages
Name Type
@copilotkitnext/react Patch
@copilotkit/react-core Patch
@copilotkitnext/next-pages-router Patch
@copilotkit/react-textarea Patch
@copilotkit/react-ui Patch
@copilotkit/a2ui-renderer Patch
@copilotkit/runtime-client-gql Patch
@copilotkit/runtime Patch
@copilotkit/sdk-js Patch
@copilotkit/shared Patch
@copilotkit/voice Patch
@copilotkitnext/agent Patch
@copilotkitnext/angular Patch
@copilotkitnext/core Patch
@copilotkitnext/demo-agents Patch
@copilotkitnext/eslint-config Patch
@copilotkitnext/runtime Patch
@copilotkitnext/shared Patch
@copilotkitnext/sqlite-runner Patch
@copilotkitnext/typescript-config Patch
@copilotkitnext/web-inspector Patch
@copilotkitnext/node-express Patch
@copilotkit-storybook/angular Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@nx-cloud

nx-cloud Bot commented Mar 13, 2026 •

Copy link
Copy Markdown

🤖 Nx Cloud AI Fix

Ensure the fix-ci command is configured to always run in your CI pipeline to get automatic fixes in future runs. For more information, please see https://nx.dev/ci/features/self-healing-ci


View your CI Pipeline Execution ↗ for commit 5a3c894

Command Status Duration Result
nx run-many -t build --projects=packages/** ✅ Succeeded 1s View ↗

☁️ Nx Cloud last updated this comment at 2026-03-14 18:48:22 UTC

@giulio-leone

Copy link
Copy Markdown
Contributor Author

Closing to reduce PR volume. The fix is available in the branch if useful.

BenTaylorDev added a commit that referenced this pull request Sep 4, 2026
… (#6902)

## What does this PR do?

Fixes #3330 — fenced markdown code blocks render as one collapsed line
in the packaged v2 React UI.

### Root cause

streamdown renders one `<span>` per source line inside
`pre[data-streamdown="code-block-body"] > code`, and leaves **no newline
characters** in the text. The line break comes entirely from the raw
Tailwind utility `block` on that span:

```js
// streamdown 1.6.11, dist/code-block-*.js
var v = cn("block", "before:content-[counter(line)]", ...);
```

CopilotKit builds Tailwind with `@import "tailwindcss" prefix(cpk)`, so
`.block` is never emitted into `dist/v2/index.css` — only `.cpk\:block`
is. Every line therefore renders inline and the block collapses onto one
row.

The line spans carry no `data-streamdown` attribute, so the rule has to
be scoped structurally, the same way the table action controls were in
#5944:

```css
[data-copilotkit] [data-streamdown="code-block-body"] > code > span {
  @apply cpk:block;
}
```

### Why the earlier attempts did not work

Three previous PRs (#3441, #3615, #5387) added `whitespace-pre` to the
`<pre>`. That is a no-op: the UA stylesheet already applies
`white-space: pre` to `<pre>`, nothing in the packaged CSS overrides it,
and there are no newlines in the text for it to preserve.

### Knowingly not fixed here

- **Line-number gutter.** streamdown's `before:content-[counter(line)]
before:w-4 before:mr-4 …` utilities are unprefixed too, so the gutter
never renders. That is cosmetic, and the repo's existing scoped rules do
not port it either.
- **The pre-highlight loading skeleton** (`space-y-4`, `divide-y`,
`animate-spin`) is unprefixed as well — a brief flash of unstyled
skeleton before shiki resolves.
- **The broader class of bug.** Every unprefixed streamdown utility has
to be hand-ported like this. streamdown 2.x adds a `prefix` prop that
would fix the whole surface at once, and #5147 proposes removing the
bundled renderer entirely. Both are larger calls than this bug fix.

## Testing

**1. Live browser verification.** Built `dist/v2/index.css` from
`origin/main` and from this branch, rendered streamdown 1.6.11's actual
code-block DOM against each, and measured layout in Chromium:

| | `white-space` on `<pre>` | line-span `display` | distinct rendered
rows | `<pre>` height |
|---|---|---|---|---|
| main | `pre` | `inline` | **1** | 52px |
| this PR | `pre` | `block` | **5** | 112px |

Indentation is preserved after the fix (`spans[1].textContent` starts
with two spaces).


**2. Compiled CSS.** `tailwindcss -i src/v2/styles/globals.css -o … -m`
emits exactly:

```css
[data-copilotkit] [data-streamdown=code-block-body]>code>span{display:block}
```

**3. Tests** — `pnpm -C packages/react-core exec vitest run
src/v2/styles`

```
 ✓ src/v2/styles/__tests__/streamdown-styles.test.ts (3 tests) 2ms
 ✓ src/v2/styles/__tests__/streamdown-table-controls.test.tsx (1 test) 37ms
 ✓ src/v2/styles/__tests__/streamdown-code-block-lines.test.tsx (1 test) 430ms

 Test Files  3 passed (3)
      Tests  5 passed (5)
```

Two tests, following the split established by #5944 — a source-string
test that the selector exists, and a DOM test that streamdown still
renders the structure that selector assumes (so a streamdown markup
change fails loudly instead of silently un-fixing this).

**4. Mutation-checked both tests.** Removing the CSS rule fails the
string test:

```
   × Streamdown styles > ships a scoped display rule for code block lines (#3330) 3ms
      Tests  1 failed | 2 passed (3)
```

Pointing the DOM test at a selector streamdown does not render fails it:

```
   × Streamdown code block lines DOM (#3330) > renders one line span per source line 428ms
      Tests  1 failed (1)
```

**5. Formatting** — `oxfmt --check` clean on all three files; `git diff
--check` clean.

`tsc --noEmit` in this worktree reports 58 pre-existing errors, all from
a stale cross-package `@copilotkit/core` dist; none are in the changed
files (which are CSS plus tests).

## Related PRs and Issues

Fixes #3330
Supersedes #3441, #3615, #5387, #5996 (all added a no-op
`whitespace-pre`)

## Checklist

- [x] I have read the [Contribution
Guide](https://github.com/copilotkit/copilotkit/blob/master/CONTRIBUTING.md)
- [x] If the PR changes or adds functionality, I have updated the
relevant documentation (not applicable: scoped visual bug fix)
- [x] "Allow edits by maintainers" is checked


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Bug Fixes**
- Fixed fenced code blocks collapsing into a single line in the packaged
UI.
- Code lines now render vertically as separate rows with the correct
layout styling.

- **Tests**
- Added regression coverage to verify code-line rendering and scoped
styles for code blocks.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug: Markdown code block rendering doesn't preserve newlines

1 participant