Skip to content

fix(react-core): preserve markdown code block newlines - #5387

Closed
westkite1201 wants to merge 1 commit into
CopilotKit:mainfrom
westkite1201:fix/3330-preserve-code-newlines
Closed

westkite1201 wants to merge 1 commit into
CopilotKit:mainfrom
westkite1201:fix/3330-preserve-code-newlines

Conversation

@westkite1201

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes markdown code blocks in the v2 React chat UI so newlines are preserved when Streamdown renders fenced code blocks.

This adds cpk:whitespace-pre to the existing [data-streamdown="code-block"] > pre rule and includes a small regression test that keeps the CSS rule from dropping the whitespace preservation utility again.

I also checked for existing open and closed PRs by issue number and root-cause terms (#3330, code block, newlines, whitespace-pre, Streamdown) before opening this PR and did not find an overlapping PR.

Testing:

  • corepack pnpm -C packages/react-core exec vitest run src/v2/styles/__tests__/globals-css.test.ts
  • corepack pnpm -C packages/react-core run build:css
  • corepack pnpm exec oxfmt --check packages/react-core/src/v2/styles/__tests__/globals-css.test.ts packages/react-core/src/v2/styles/globals.css
  • git diff --check

Related PRs and Issues

Fixes #3330

Checklist

  • I have read the Contribution Guide
  • If the PR changes or adds functionality, I have updated the relevant documentation (not applicable: scoped visual bug fix)
  • "Allow edits by maintainers" is checked (lets us help iterate on your PR directly — faster turnaround for everyone)

@vercel

vercel Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview, Comment Jun 11, 2026 3:08pm

Request Review

@vercel

vercel Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

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

A member of the Team first needs to authorize it.

@BenTaylorDev BenTaylorDev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — this is on the right, live selector (div[data-streamdown="code-block"] > pre still matches in streamdown 1.6.11), it merges cleanly, it's strictly additive (can't regress prose whitespace), and it fits the pattern maintainers have been using to re-declare streamdown elements under the cpk: prefix. My concern is whether it actually fixes the reported symptom:

  • Streamdown renders each code line as a display:block span with no newline characters between the spans — the line breaks come from that block utility, not from \n. CopilotKit's build is @import "tailwindcss" prefix(cpk) scanning react-core source only (not node_modules/streamdown, which ships no CSS), so streamdown's unprefixed block class isn't emitted and the per-line spans have no data-streamdown attribute to re-declare. In that state white-space: pre on the <pre> preserves intra-line indentation but can't restore inter-line breaks (there are no newline chars to preserve).
  • The test only readFileSyncs globals.css and asserts it contains cpk:whitespace-pre — it never renders Streamdown, so green CI isn't evidence the newlines are actually preserved.

Could you render a multi-line fenced code block in the v2 chat with this applied (in a consumer relying on CopilotKit's packaged CSS, not one whose own Tailwind scans node_modules/streamdown) and confirm both line breaks and indentation survive? If lines still collapse, the fix is broader than this one-liner (supplying streamdown's line-span styles under the prefix). Also note commenters report the code-block header is broken too — separate from this change.

@BenTaylorDev

Copy link
Copy Markdown
Contributor

Re-reviewed this with a live render rather than by inspection, and the result is conclusive: the change has no effect.

I built the packaged CSS from both main and this branch and rendered streamdown 1.6.11's actual code-block DOM in a browser. Computed white-space on the <pre>, the line-span display, and the number of rendered line rows are identical before and after — a 3-line fenced block still collapses onto one row in both.

Two independent reasons:

  1. The declaration is redundant. The UA stylesheet already applies white-space: pre to <pre>, and nothing in CopilotKit's packaged CSS overrides it — Tailwind v4's preflight only sets font properties on pre (tailwindcss/preflight.css:109-127), and the built dist/v2/index.css has no competing rule. The compiled delta is exactly +white-space:pre, which changes no computed value.

  2. There are no newlines to preserve. Rendering Streamdown through the repo's own test harness, pre.textContent comes back as "function greet(name) { return `hi ${name}`;}" — no \n anywhere. Streamdown splits the source on \n and emits one <span class="block …"> per line; the line breaks come entirely from that unprefixed block class, which CopilotKit's @import "tailwindcss" prefix(cpk) build never emits (the compiled CSS contains .cpk\:block only).

The rule that does fix it, verified in the same harness (3 rows, indentation intact):

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

whitespace-pre isn't needed alongside it.

Also worth knowing: main has since gained a scoped streamdown style block covering code-block, code-block-header and code-block-body, and streamdown-styles.test.ts is now the established home for assertions of this kind — so the new globals-css.test.ts would be a third file doing the same string matching.

Closing this, since the one-line change doesn't move the behavior and the branch has drifted a long way from current main. #3330 stays open. Thanks for the careful write-up, and for checking for overlapping PRs before opening — that part was exactly right.

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 -->

This branch was successfully deployed

1 active deployment
Preview – docs — 76f20e97 Deployed Jun 11, 2026 by vercel[bot]
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

2 participants