From 30bd4d3ee45522afef438a31217d6b3cccc2b415 Mon Sep 17 00:00:00 2001 From: saschabuehrle Date: Mon, 23 Mar 2026 19:50:24 +0100 Subject: [PATCH] fix: unwrap single paragraph child in list items --- .../__tests__/list-item-paragraphs.test.tsx | 23 +++++++++++++++++++ packages/streamdown/lib/components.tsx | 14 ++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 packages/streamdown/__tests__/list-item-paragraphs.test.tsx diff --git a/packages/streamdown/__tests__/list-item-paragraphs.test.tsx b/packages/streamdown/__tests__/list-item-paragraphs.test.tsx new file mode 100644 index 00000000..4ba9b5cf --- /dev/null +++ b/packages/streamdown/__tests__/list-item-paragraphs.test.tsx @@ -0,0 +1,23 @@ +import { render } from "@testing-library/react"; +import { describe, expect, it } from "vitest"; +import { Streamdown } from "../index"; + +describe("list item paragraph wrapping", () => { + it("does not keep paragraph wrappers for loose list items", () => { + const markdown = ` +## Test + +- hello + +- world +`; + const { container } = render({markdown}); + + const items = container.querySelectorAll('[data-streamdown="list-item"]'); + + expect(items).toHaveLength(2); + for (const item of items) { + expect(item.querySelector("p")).toBeNull(); + } + }); +}); diff --git a/packages/streamdown/lib/components.tsx b/packages/streamdown/lib/components.tsx index 2449ca3a..b27847f9 100644 --- a/packages/streamdown/lib/components.tsx +++ b/packages/streamdown/lib/components.tsx @@ -189,13 +189,25 @@ type LiProps = WithNode; const MemoLi = memo( ({ children, className, node, ...props }: LiProps) => { const cn = useCn(); + + const childArray = Array.isArray(children) + ? children.filter((child) => child !== "\n" && child !== "") + : [children]; + + const normalizedChildren = + childArray.length === 1 && + isValidElement(childArray[0]) && + childArray[0].type === "p" + ? childArray[0].props.children + : children; + return (
  • p]:inline", className)} data-streamdown="list-item" {...props} > - {children} + {normalizedChildren}
  • ); },