Add support for replacing written Personalization Tags with attributes
[MAILPOET-6376]
This commit is contained in:
committed by
Rostislav Wolný
parent
0e13133499
commit
e56fe9a95e
@ -195,17 +195,26 @@ const replacePersonalizationTagsWithHTMLComments = (
|
|||||||
tags: PersonalizationTag[]
|
tags: PersonalizationTag[]
|
||||||
) => {
|
) => {
|
||||||
tags.forEach( ( tag ) => {
|
tags.forEach( ( tag ) => {
|
||||||
if ( ! content.includes( tag.token ) ) {
|
// Skip if the token is not in the content
|
||||||
// Skip if the token is not in the content
|
if (
|
||||||
|
! content.includes( tag.token.slice( 0, tag.token.length - 1 ) )
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const escapedRegExp = tag.token.replace(
|
// Match the token with optional attributes like [mailpoet/subscriber-firstname default="user"]
|
||||||
/[.*+?^${}()|[\]\\]/g,
|
const baseToken = tag.token
|
||||||
'\\$&'
|
.substring( 1, tag.token.length - 1 )
|
||||||
); // Escape special characters
|
.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); // Escape base token and remove brackets
|
||||||
const regex = new RegExp( `(?<!<!--)${ escapedRegExp }(?!-->)`, 'g' ); // Match token not inside HTML comments
|
const regex = new RegExp(
|
||||||
content = content.replace( regex, `<!--${ tag.token }-->` );
|
`(?<!<!--)\\[(${ baseToken }(\\s[^\\]]*)?)\\](?!-->)`, // Match full token with optional attributes
|
||||||
|
'g'
|
||||||
|
);
|
||||||
|
|
||||||
|
content = content.replace( regex, ( match ) => {
|
||||||
|
// Use the exact text inside the brackets for the replacement
|
||||||
|
return `<!--${ match }-->`;
|
||||||
|
} );
|
||||||
} );
|
} );
|
||||||
return content;
|
return content;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user