Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-empty-lines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"streamdown": patch
---

Fix empty lines collapsing in highlighted code blocks
39 changes: 39 additions & 0 deletions packages/streamdown/__tests__/code-block-body.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,45 @@ describe("CodeBlockBody", () => {
expect(lineSpans.length).toBe(3);
});

it("should render newline for empty row (single empty-content token)", () => {
const result = {
tokens: [
[{ content: "line 1" }],
[{ content: "" }],
[{ content: "line 3" }],
],
};

const { container } = render(
<CodeBlockBody language="text" result={result} />
);

const lineSpans = container.querySelectorAll("code > span");
expect(lineSpans.length).toBe(3);

// Empty row should contain a newline, not a token span
const emptyLine = lineSpans[1];
expect(emptyLine?.querySelector("span")).toBeNull();
expect(emptyLine?.textContent).toBe("\n");
});

it("should render newline for empty row (zero tokens)", () => {
const result = {
tokens: [[{ content: "line 1" }], [], [{ content: "line 3" }]],
};

const { container } = render(
<CodeBlockBody language="text" result={result} />
);

const lineSpans = container.querySelectorAll("code > span");
expect(lineSpans.length).toBe(3);

const emptyLine = lineSpans[1];
expect(emptyLine?.querySelector("span")).toBeNull();
expect(emptyLine?.textContent).toBe("\n");
});

it("should handle token with color property", () => {
const result = {
tokens: [[{ content: "colored", color: "#abc123" }]],
Expand Down
94 changes: 49 additions & 45 deletions packages/streamdown/lib/code-block/body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,55 +89,59 @@ export const CodeBlockBody = memo(
// biome-ignore lint/suspicious/noArrayIndexKey: "This is a stable key."
key={index}
>
{/* biome-ignore lint/complexity/noExcessiveCognitiveComplexity: dual-theme token style mapping */}
{row.map((token, tokenIndex) => {
// Shiki dual-theme tokens put direct CSS properties (color,
// background-color) into htmlStyle alongside CSS custom
// properties (--shiki-dark, etc). Direct properties as inline
// styles override the Tailwind class-based dark mode approach,
// so we redirect them to CSS custom properties instead.
const tokenStyle: Record<string, string> = {};
let hasBg = Boolean(token.bgColor);
{row.length === 0 || (row.length === 1 && row[0].content === "")
? // Empty line: insert newline to preserve copy behavior
"\n"
: // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: dual-theme token style mapping
row.map((token, tokenIndex) => {
// Shiki dual-theme tokens put direct CSS properties (color,
// background-color) into htmlStyle alongside CSS custom
// properties (--shiki-dark, etc). Direct properties as inline
// styles override the Tailwind class-based dark mode approach,
// so we redirect them to CSS custom properties instead.
const tokenStyle: Record<string, string> = {};
let hasBg = Boolean(token.bgColor);

if (token.color) {
tokenStyle["--sdm-c"] = token.color;
}
if (token.bgColor) {
tokenStyle["--sdm-tbg"] = token.bgColor;
}
if (token.color) {
tokenStyle["--sdm-c"] = token.color;
}
if (token.bgColor) {
tokenStyle["--sdm-tbg"] = token.bgColor;
}

if (token.htmlStyle) {
for (const [key, value] of Object.entries(
token.htmlStyle
)) {
if (key === "color") {
tokenStyle["--sdm-c"] = value;
} else if (key === "background-color") {
tokenStyle["--sdm-tbg"] = value;
hasBg = true;
} else {
tokenStyle[key] = value;
if (token.htmlStyle) {
for (const [key, value] of Object.entries(
token.htmlStyle
)) {
if (key === "color") {
tokenStyle["--sdm-c"] = value;
} else if (key === "background-color") {
tokenStyle["--sdm-tbg"] = value;
hasBg = true;
} else {
tokenStyle[key] = value;
}
}
}
}
}

return (
<span
className={cn(
"text-[var(--sdm-c,inherit)]",
"dark:text-[var(--shiki-dark,var(--sdm-c,inherit))]",
hasBg && "bg-[var(--sdm-tbg)]",
hasBg && "dark:bg-[var(--shiki-dark-bg,var(--sdm-tbg))]"
)}
// biome-ignore lint/suspicious/noArrayIndexKey: "This is a stable key."
key={tokenIndex}
style={tokenStyle as CSSProperties}
{...token.htmlAttrs}
>
{token.content}
</span>
);
})}
return (
<span
className={cn(
"text-[var(--sdm-c,inherit)]",
"dark:text-[var(--shiki-dark,var(--sdm-c,inherit))]",
hasBg && "bg-[var(--sdm-tbg)]",
hasBg &&
"dark:bg-[var(--shiki-dark-bg,var(--sdm-tbg))]"
)}
// biome-ignore lint/suspicious/noArrayIndexKey: "This is a stable key."
key={tokenIndex}
style={tokenStyle as CSSProperties}
{...token.htmlAttrs}
>
{token.content}
</span>
);
})}
</span>
))}
</code>
Expand Down