maizzle / framework

Quickly build HTML emails with Tailwind CSS.
https://maizzle.com
MIT License
1.2k stars 48 forks source link

Allow capturing groups replacement in replaceStrings.js #1313

Closed Jordinateur closed 1 month ago

Jordinateur commented 1 month ago

Hi

This adjustement aims to let the replacement string make use of the capturing group in the regex.

In the version 4, replace strings transformers allows us to replace (1) by (1). Which is not the case in the version 5, i see it as a regression because i personnally used it in that way but maybe it was intended.

In my PR choosed to detect if the replacement string contains a capturing group reference in order to not have the overload of regex replacement if it's not wanted.

Thank for your time and this project !

cossssmin commented 1 month ago

Hey thanks for this, could you please provide some usage examples for it that (currently) no longer work in v5?

Jordinateur commented 1 month ago

Sure, in my mails, i wrap with <sup> text with this pattern (x).

I use the Maizzle API, just like this :

const html = await replaceStrings('<p>(1)</p>', {'(\\(\\d\\))': '<sup>$1</sup>'})

In V4, html was

 <p><sup>(1)</sup></p>

And in V5 html is

 <p><sup>$1</sup></p>

I also use this concept in order to add to wrap words inside french quotes like this : « Hi », and some other things on the same principle.

cossssmin commented 1 month ago

Cool, can you please also add a test for this case in test/transformers.test.js? Make sure to pull first, had to fix some linting errors.

It's this test:

image

I'll take care of the types and docs 👍

cossssmin commented 1 month ago

Sweet, thanks!

Jordinateur commented 1 month ago

Done!

And sorry for the linting, i didn't checked.