Skip to content

Commit 07d3c71

Browse files
fix(markdown): support nested lists in markdown component (#2624)
Co-authored-by: Gabriel Miranda <gabrielmfern@outlook.com>
1 parent 7dcbd85 commit 07d3c71

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

.changeset/angry-lights-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-email/markdown": patch
3+
---
4+
5+
fix nested lists not working

.changeset/silent-maps-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-email/components": patch
3+
---
4+
5+
markdown: fix nested lists not working

packages/markdown/src/__snapshots__/markdown.spec.tsx.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ exports[`<Markdown> component renders correctly > renders lists in the correct f
1414
</div><!--/$-->"
1515
`;
1616
17+
exports[`<Markdown> component renders correctly > renders nested lists in the correct format for browsers 1`] = `
18+
"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><!--$--><div data-id="react-email-markdown"><ul>
19+
<li><p>parent list item</p>
20+
<ul>
21+
<li>nested list item 1</li>
22+
<li>nested list item 2</li>
23+
</ul>
24+
</li>
25+
<li><p>another parent item</p>
26+
<ol>
27+
<li>nested ordered item 1</li>
28+
<li>nested ordered item 2</li>
29+
</ol>
30+
</li>
31+
</ul>
32+
</div><!--/$-->"
33+
`;
34+
1735
exports[`<Markdown> component renders correctly > renders text in the correct format for browsers 1`] = `
1836
"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><!--$--><div data-id="react-email-markdown"><p><strong style="font:700 23px / 32px &#x27;Roobert PRO&#x27;, system-ui, sans-serif;background:url(&#x27;path/to/image&#x27;)">This is sample bold text in markdown</strong> and <em style="font-style:italic">this is italic text</em></p>
1937
</div><!--/$-->"

packages/markdown/src/markdown.spec.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,20 @@ console.log(\`Hello, $\{name}!\`);
111111
);
112112
expect(actualOutput).toMatchSnapshot();
113113
});
114+
115+
it('renders nested lists in the correct format for browsers', async () => {
116+
const actualOutput = await render(
117+
<Markdown>
118+
{`
119+
- parent list item
120+
- nested list item 1
121+
- nested list item 2
122+
- another parent item
123+
1. nested ordered item 1
124+
2. nested ordered item 2
125+
`}
126+
</Markdown>,
127+
);
128+
expect(actualOutput).toMatchSnapshot();
129+
});
114130
});

packages/markdown/src/markdown.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ export const Markdown = React.forwardRef<HTMLDivElement, MarkdownProps>(
119119
};
120120

121121
renderer.listitem = ({ tokens }) => {
122-
const text = renderer.parser.parseInline(tokens);
122+
const hasNestedList = tokens.some((token) => token.type === 'list');
123+
const text = hasNestedList
124+
? renderer.parser.parse(tokens)
125+
: renderer.parser.parseInline(tokens);
123126

124127
return `<li${
125128
parseCssInJsToInlineCss(finalStyles.li) !== ''

0 commit comments

Comments
 (0)