Skip to content

Commit fe03e32

Browse files
committed
fix: major bug fix for parseMarkdownToReactEmailJSX
1 parent 7b3db09 commit fe03e32

File tree

5 files changed

+52
-38
lines changed

5 files changed

+52
-38
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
### [2.0.0](https://github.com/codeskills-dev/md-to-react-email/compare/v2.0.0...v1.2.0) (2023-06-20)
7+
8+
### Features
9+
10+
- Major Bug fix for `parseMarkdownToReactEmailJSX`
11+
- Major Bug fix for `reactEmailMarkdown` Component
12+
613
### [1.2.0](https://github.com/codeskills-dev/md-to-react-email/compare/v1.2.0...v1.0.2) (2023-06-15)
714

815
### Features

__tests__/parseMarkdownToReactEmailJSX.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,41 @@ import {
77
describe("Markdown to React Mail JSX Parser", () => {
88
it("converts header one correctly", () => {
99
const markdown = "# Hello, World!";
10-
const expected = `<Section><Heading as="h1" style="${parseCssInJsToInlineCss(
10+
const expected = `<h1 data-id="react-email-heading" style="${parseCssInJsToInlineCss(
1111
styles.h1
12-
)}">Hello, World!</Heading></Section>`;
12+
)}">Hello, World!</h1>`;
1313

1414
const rendered = parseMarkdownToReactEmailJSX(markdown);
1515
expect(rendered).toBe(expected);
1616
});
1717

1818
it("converts images correctly", () => {
1919
const markdown = "![alt text](image.jpg)";
20-
const expected = `<Section><Img style="${parseCssInJsToInlineCss(
20+
const expected = `<img style="${parseCssInJsToInlineCss(
2121
styles.image
22-
)}" alt="alt text" src="image.jpg" /></Section>`;
22+
)}" alt="alt text" src="image.jpg" />`;
2323

2424
const rendered = parseMarkdownToReactEmailJSX(markdown);
2525
expect(rendered).toBe(expected);
2626
});
2727

2828
it("converts links correctly", () => {
2929
const markdown = "[Codeskills](https://codeskills.dev)";
30-
const expected = `<Section><Link href="https://codeskills.dev" style="${parseCssInJsToInlineCss(
30+
const expected = `<a data-id=\"react-email-link\" target=\"_blank\" href="https://codeskills.dev" style="${parseCssInJsToInlineCss(
3131
styles.link
32-
)}">Codeskills</Link></Section>`;
32+
)}">Codeskills</a>`;
3333

3434
const rendered = parseMarkdownToReactEmailJSX(markdown);
3535
expect(rendered).toBe(expected);
3636
});
3737

3838
it("converts code blocks correctly", () => {
3939
const markdown = '```javascript\nconsole.log("Hello, World!");\n```';
40-
const expected = `<Section><pre style="${parseCssInJsToInlineCss(
40+
const expected = `<pre style="${parseCssInJsToInlineCss(
4141
styles.codeBlock
42-
)}"><Text>{\`javascript
42+
)}"><p data-id="react-email-text">{\`javascript
4343
console.log("Hello, World!");
44-
\`}</Text></pre></Section>`;
44+
\`}</p></pre>`;
4545

4646
const rendered = parseMarkdownToReactEmailJSX(markdown);
4747
expect(rendered).toBe(expected);
@@ -55,7 +55,7 @@ console.log("Hello, World!");
5555
| Cell 3 | Cell 4 |
5656
`;
5757
const expected =
58-
'<Section><table style=""><thead style=""><tr style="color:red"><th style="" align="center">Header 1</th><th style="" align="center">Header 2</th></tr></thead><tbody style=""><tr style="color:red"><td style="" align="center">Cell 1</td><td style="" align="center">Cell 2</td></tr><tr style="color:red"><td style="" align="center">Cell 3</td><td style="" align="center">Cell 4</td></tr></tbody></table></Section>';
58+
'<table style=""><thead style=""><tr style="color:red"><th style="" align="center">Header 1</th><th style="" align="center">Header 2</th></tr></thead><tbody style=""><tr style="color:red"><td style="" align="center">Cell 1</td><td style="" align="center">Cell 2</td></tr><tr style="color:red"><td style="" align="center">Cell 3</td><td style="" align="center">Cell 4</td></tr></tbody></table>';
5959

6060
const rendered = parseMarkdownToReactEmailJSX(markdown, {
6161
tr: { color: "red" },
@@ -65,9 +65,9 @@ console.log("Hello, World!");
6565

6666
it("converts strikethrough blocks correctly", () => {
6767
const markdown = "~~This is a paragraph.~~";
68-
const expected = `<Section><del style="${parseCssInJsToInlineCss(
68+
const expected = `<del style="${parseCssInJsToInlineCss(
6969
styles.strikethrough
70-
)}">This is a paragraph.</del></Section>`;
70+
)}">This is a paragraph.</del>`;
7171

7272
const rendered = parseMarkdownToReactEmailJSX(markdown);
7373
expect(rendered).toBe(expected);

__tests__/reactEmailMarkdown.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("ReactEmailMarkdown component renders correctly", () => {
88
<ReactEmailMarkdown markdown={`# Hello, World!`} />
99
);
1010
expect(actualOutput).toMatchInlineSnapshot(
11-
`"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><div><Section><Heading as="h1" style="font-weight:500;padding-top:20;font-size:2.5rem">Hello, World!</Heading></Section></div>"`
11+
`"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><div><h1 data-id="react-email-heading" style="font-weight:500;padding-top:20;font-size:2.5rem">Hello, World!</h1></div>"`
1212
);
1313
});
1414
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "md-to-react-email",
3-
"version": "1.2.0",
3+
"version": "2.0.0",
44
"description": "A simple Markdown parser for React-email written in typescript.",
55
"keywords": [
66
"markdown",

src/utils.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -198,39 +198,39 @@ export function parseMarkdownToReactEmailJSX(
198198
// Handle headings (e.g., # Heading)
199199
reactMailTemplate = markdown.replace(
200200
patterns.h1,
201-
`<Heading as="h1" style="${parseCssInJsToInlineCss(
201+
`<h1 data-id="react-email-heading" style="${parseCssInJsToInlineCss(
202202
finalStyles.h1
203-
)}">$1</Heading>`
203+
)}">$1</h1>`
204204
);
205205
reactMailTemplate = reactMailTemplate.replace(
206206
patterns.h2,
207-
`<Heading as="h2" style="${parseCssInJsToInlineCss(
207+
`<h2 data-id="react-email-heading" style="${parseCssInJsToInlineCss(
208208
finalStyles.h2
209-
)}">$1</Heading>`
209+
)}">$1</h2>`
210210
);
211211
reactMailTemplate = reactMailTemplate.replace(
212212
patterns.h3,
213-
`<Heading as="h3" style="${parseCssInJsToInlineCss(
213+
`<h3 data-id="react-email-heading" style="${parseCssInJsToInlineCss(
214214
finalStyles.h3
215-
)}">$1</Heading>`
215+
)}">$1</h3>`
216216
);
217217
reactMailTemplate = reactMailTemplate.replace(
218218
patterns.h4,
219-
`<Heading as="h4" style="${parseCssInJsToInlineCss(
219+
`<h4 data-id="react-email-heading" style="${parseCssInJsToInlineCss(
220220
finalStyles.h4
221-
)}">$1</Heading>`
221+
)}">$1</h4>`
222222
);
223223
reactMailTemplate = reactMailTemplate.replace(
224224
patterns.h5,
225-
`<Heading as="h5" style="${parseCssInJsToInlineCss(
225+
`<h5 data-id="react-email-heading" style="${parseCssInJsToInlineCss(
226226
finalStyles.h5
227-
)}">$1</Heading>`
227+
)}">$1</h5>`
228228
);
229229
reactMailTemplate = reactMailTemplate.replace(
230230
patterns.h6,
231-
`<Heading as="h6" style="${parseCssInJsToInlineCss(
231+
`<h6 data-id="react-email-heading" style="${parseCssInJsToInlineCss(
232232
finalStyles.h6
233-
)}">$1</Heading>`
233+
)}">$1</h6>`
234234
);
235235

236236
// Handle Tables from GFM
@@ -303,13 +303,17 @@ export function parseMarkdownToReactEmailJSX(
303303
// Handle bold text (e.g., **bold**)
304304
reactMailTemplate = reactMailTemplate.replace(
305305
patterns.bold,
306-
`<Text style="${parseCssInJsToInlineCss(finalStyles.bold)}">$1</Text>`
306+
`<p data-id="react-email-text" style="${parseCssInJsToInlineCss(
307+
finalStyles.bold
308+
)}">$1</p>`
307309
);
308310

309311
// Handle italic text (e.g., *italic*)
310312
reactMailTemplate = reactMailTemplate.replace(
311313
patterns.italic,
312-
`<Text style="${parseCssInJsToInlineCss(finalStyles.italic)}">$1</Text>`
314+
`<p data-id="react-email-text" style="${parseCssInJsToInlineCss(
315+
finalStyles.italic
316+
)}">$1</p>`
313317
);
314318

315319
// Handle lists (unordered and ordered)
@@ -325,37 +329,41 @@ export function parseMarkdownToReactEmailJSX(
325329
// Handle images (e.g., ![alt text](url))
326330
reactMailTemplate = reactMailTemplate.replace(
327331
patterns.image,
328-
`<Img style="${parseCssInJsToInlineCss(
332+
`<img style="${parseCssInJsToInlineCss(
329333
finalStyles.image
330334
)}" alt="$1" src="$2" />`
331335
);
332336

333337
// Handle links (e.g., [link text](url))
334338
reactMailTemplate = reactMailTemplate.replace(
335339
patterns.link,
336-
`<Link href="$2" style="${parseCssInJsToInlineCss(
340+
`<a data-id="react-email-link" target="_blank" href="$2" style="${parseCssInJsToInlineCss(
337341
finalStyles.link
338-
)}">$1</Link>`
342+
)}">$1</a>`
339343
);
340344

341345
// Handle code blocks (e.g., ```code```)
342346
reactMailTemplate = reactMailTemplate.replace(
343347
patterns.codeBlocks,
344348
`<pre style="${parseCssInJsToInlineCss(
345349
finalStyles.codeBlock
346-
)}"><Text>${`{\`$1\`}`}</Text></pre>`
350+
)}"><p data-id="react-email-text">${`{\`$1\`}`}</p></pre>`
347351
);
348352

349353
// Handle inline code (e.g., `code`)
350354
reactMailTemplate = reactMailTemplate.replace(
351355
patterns.codeInline,
352-
`<Text style="${parseCssInJsToInlineCss(finalStyles.codeInline)}">$1</Text>`
356+
`<p data-id="react-email-text" style="${parseCssInJsToInlineCss(
357+
finalStyles.codeInline
358+
)}">$1</p>`
353359
);
354360

355361
// Handle block quotes
356362
reactMailTemplate = reactMailTemplate.replace(
357363
/^>\s+(.+)$/gm,
358-
`<Text style="${parseCssInJsToInlineCss(finalStyles.blockQuote)}">$1</Text>`
364+
`<p data-id="react-email-text" style="${parseCssInJsToInlineCss(
365+
finalStyles.blockQuote
366+
)}">$1</p>`
359367
);
360368

361369
// Handle line breaks (e.g., <br />)
@@ -367,11 +375,10 @@ export function parseMarkdownToReactEmailJSX(
367375
// Handle horizontal rules (e.g., ---)
368376
reactMailTemplate = reactMailTemplate.replace(
369377
patterns.hr,
370-
`<Hr style="${parseCssInJsToInlineCss(finalStyles.hr)}" />`
378+
`<hr data-id="react-email-hr" style="${parseCssInJsToInlineCss(
379+
finalStyles.hr
380+
)}" />`
371381
);
372382

373-
// Wrap content in a section tag
374-
reactMailTemplate = `<Section>${reactMailTemplate}</Section>`;
375-
376383
return reactMailTemplate;
377384
}

0 commit comments

Comments
 (0)