@@ -188,37 +188,56 @@ export function parseMarkdownToReactEmail(
188188 return reactMailTemplate ;
189189}
190190
191- export function parseMarkdownToReactEmailJSX (
192- markdown : string ,
193- customStyles ?: StylesType
194- ) : string {
191+ interface ParseMarkdownToReactEmailJSXProps {
192+ markdown : string ;
193+ customStyles ?: StylesType ;
194+ withDataAttr ?: boolean ;
195+ }
196+
197+ export function parseMarkdownToReactEmailJSX ( {
198+ markdown,
199+ customStyles,
200+ withDataAttr = false ,
201+ } : ParseMarkdownToReactEmailJSXProps ) : string {
195202 const finalStyles = { ...styles , ...customStyles } ;
196203 let reactMailTemplate = "" ;
197204
198205 // Handle headings (e.g., # Heading)
199206 reactMailTemplate = markdown . replace (
200207 patterns . h1 ,
201- `<h1 style="${ parseCssInJsToInlineCss ( finalStyles . h1 ) } ">$1</h1>`
208+ `<h1${
209+ withDataAttr ? ' data-id="react-email-heading"' : ""
210+ } style="${ parseCssInJsToInlineCss ( finalStyles . h1 ) } ">$1</h1>`
202211 ) ;
203212 reactMailTemplate = reactMailTemplate . replace (
204213 patterns . h2 ,
205- `<h2 style="${ parseCssInJsToInlineCss ( finalStyles . h2 ) } ">$1</h2>`
214+ `<h2${
215+ withDataAttr ? ' data-id="react-email-heading"' : ""
216+ } style="${ parseCssInJsToInlineCss ( finalStyles . h2 ) } ">$1</h2>`
206217 ) ;
207218 reactMailTemplate = reactMailTemplate . replace (
208219 patterns . h3 ,
209- `<h3 style="${ parseCssInJsToInlineCss ( finalStyles . h3 ) } ">$1</h3>`
220+ `<h3${
221+ withDataAttr ? ' data-id="react-email-heading"' : ""
222+ } style="${ parseCssInJsToInlineCss ( finalStyles . h3 ) } ">$1</h3>`
210223 ) ;
211224 reactMailTemplate = reactMailTemplate . replace (
212225 patterns . h4 ,
213- `<h4 style="${ parseCssInJsToInlineCss ( finalStyles . h4 ) } ">$1</h4>`
226+ `<h4${
227+ withDataAttr ? ' data-id="react-email-heading"' : ""
228+ } style="${ parseCssInJsToInlineCss ( finalStyles . h4 ) } ">$1</h4>`
214229 ) ;
215230 reactMailTemplate = reactMailTemplate . replace (
216231 patterns . h5 ,
217- `<h5 style="${ parseCssInJsToInlineCss ( finalStyles . h5 ) } ">$1</h5>`
232+ `<h5${
233+ withDataAttr ? ' data-id="react-email-heading"' : ""
234+ } style="${ parseCssInJsToInlineCss ( finalStyles . h5 ) } ">$1</h5>`
218235 ) ;
219236 reactMailTemplate = reactMailTemplate . replace (
220237 patterns . h6 ,
221- `<h6 style="${ parseCssInJsToInlineCss ( finalStyles . h6 ) } ">$1</h6>`
238+ `<h6${
239+ withDataAttr ? ' data-id="react-email-heading"' : ""
240+ } style="${ parseCssInJsToInlineCss ( finalStyles . h6 ) } ">$1</h6>`
222241 ) ;
223242
224243 // Handle Tables from GFM
@@ -291,13 +310,17 @@ export function parseMarkdownToReactEmailJSX(
291310 // Handle bold text (e.g., **bold**)
292311 reactMailTemplate = reactMailTemplate . replace (
293312 patterns . bold ,
294- `<p style="${ parseCssInJsToInlineCss ( finalStyles . bold ) } ">$1</p>`
313+ `<p${
314+ withDataAttr ? ' data-id="react-email-text"' : ""
315+ } style="${ parseCssInJsToInlineCss ( finalStyles . bold ) } ">$1</p>`
295316 ) ;
296317
297318 // Handle italic text (e.g., *italic*)
298319 reactMailTemplate = reactMailTemplate . replace (
299320 patterns . italic ,
300- `<p style="${ parseCssInJsToInlineCss ( finalStyles . italic ) } ">$1</p>`
321+ `<p${
322+ withDataAttr ? ' data-id="react-email-text"' : ""
323+ } style="${ parseCssInJsToInlineCss ( finalStyles . italic ) } ">$1</p>`
301324 ) ;
302325
303326 // Handle lists (unordered and ordered)
@@ -321,29 +344,35 @@ export function parseMarkdownToReactEmailJSX(
321344 // Handle links (e.g., [link text](url))
322345 reactMailTemplate = reactMailTemplate . replace (
323346 patterns . link ,
324- `<a target="_blank" href="$2" style="${ parseCssInJsToInlineCss (
347+ `<a${
348+ withDataAttr ? ' data-id="react-email-link"' : ""
349+ } target="_blank" href="$2" style="${ parseCssInJsToInlineCss (
325350 finalStyles . link
326351 ) } ">$1</a>`
327352 ) ;
328353
329354 // Handle code blocks (e.g., ```code```)
330355 reactMailTemplate = reactMailTemplate . replace (
331356 patterns . codeBlocks ,
332- `<pre style="${ parseCssInJsToInlineCss (
333- finalStyles . codeBlock
334- ) } "><p >${ `{\`$1\`}` } </p></pre>`
357+ `<pre style="${ parseCssInJsToInlineCss ( finalStyles . codeBlock ) } "><p ${
358+ withDataAttr ? ' data-id="react-email-text"' : ""
359+ } >${ `{\`$1\`}` } </p></pre>`
335360 ) ;
336361
337362 // Handle inline code (e.g., `code`)
338363 reactMailTemplate = reactMailTemplate . replace (
339364 patterns . codeInline ,
340- `<p style="${ parseCssInJsToInlineCss ( finalStyles . codeInline ) } ">$1</p>`
365+ `<p${
366+ withDataAttr ? ' data-id="react-email-text"' : ""
367+ } style="${ parseCssInJsToInlineCss ( finalStyles . codeInline ) } ">$1</p>`
341368 ) ;
342369
343370 // Handle block quotes
344371 reactMailTemplate = reactMailTemplate . replace (
345372 / ^ > \s + ( .+ ) $ / gm,
346- `<p style="${ parseCssInJsToInlineCss ( finalStyles . blockQuote ) } ">$1</p>`
373+ `<p${
374+ withDataAttr ? ' data-id="react-email-text"' : ""
375+ } style="${ parseCssInJsToInlineCss ( finalStyles . blockQuote ) } ">$1</p>`
347376 ) ;
348377
349378 // Handle line breaks (e.g., <br />)
@@ -355,7 +384,9 @@ export function parseMarkdownToReactEmailJSX(
355384 // Handle horizontal rules (e.g., ---)
356385 reactMailTemplate = reactMailTemplate . replace (
357386 patterns . hr ,
358- `<hr style="${ parseCssInJsToInlineCss ( finalStyles . hr ) } " />`
387+ `<hr${
388+ withDataAttr ? ' data-id="react-email-hr"' : ""
389+ } style="${ parseCssInJsToInlineCss ( finalStyles . hr ) } " />`
359390 ) ;
360391
361392 return reactMailTemplate ;
0 commit comments