Skip to content

Commit 7d722a2

Browse files
authored
Merge pull request #25 from strapi/fix/empty-paragraphs
Fix: render empty paragraph as br elements
2 parents 740bcbb + 6c3e6e3 commit 7d722a2

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/Block.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ const Block = ({ content }: BlockProps) => {
6565
return <BlockComponent {...props} />;
6666
}
6767

68+
// Handle empty paragraphs separately as they should render a <br> tag
69+
if (
70+
type === 'paragraph' &&
71+
childrenNodes.length === 1 &&
72+
childrenNodes[0].type === 'text' &&
73+
childrenNodes[0].text === ''
74+
) {
75+
return <br />;
76+
}
77+
6878
const augmentedProps = augmentProps(content);
6979

7080
return (

tests/BlocksRenderer.test.tsx

+27
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,33 @@ describe('BlocksRenderer', () => {
8888
expect(paragraph).toHaveTextContent('A paragraph with bold');
8989
});
9090

91+
it('renders a br when there is an empty paragraph', () => {
92+
render(
93+
<BlocksRenderer
94+
content={[
95+
{
96+
type: 'paragraph',
97+
children: [{ type: 'text', text: 'First paragraph' }],
98+
},
99+
// empty paragraph
100+
{
101+
type: 'paragraph',
102+
children: [{ type: 'text', text: '' }],
103+
},
104+
{
105+
type: 'paragraph',
106+
children: [{ type: 'text', text: 'Second paragraph' }],
107+
},
108+
]}
109+
/>
110+
);
111+
112+
// eslint-disable-next-line testing-library/no-node-access
113+
const brElement = screen.getByText('First paragraph').nextElementSibling;
114+
expect(brElement).toBeInTheDocument();
115+
expect(brElement?.tagName).toBe('BR');
116+
});
117+
91118
it('renders quotes', () => {
92119
render(
93120
<BlocksRenderer

0 commit comments

Comments
 (0)