Handle NBSP in tiptap parsing (#8148)

Tiptap uses non breaking spaces between nodes (like variables). Those
html characters are not properly handles in emails. Replacing by regular
spaces during parsing.

I tried to fix it in settings but looks like this is only for preserving
those nbsp and not for removal (see
https://github.com/ueberdosis/tiptap/pull/254)
This commit is contained in:
Thomas Trompette
2024-10-28 15:50:14 +01:00
committed by GitHub
parent fc8c9d9167
commit 2ba98ddadd
2 changed files with 31 additions and 1 deletions

View File

@@ -267,4 +267,33 @@ describe('parseEditorContent', () => {
expect(parseEditorContent(input)).toBe('First line\nSecond line');
});
it('should handle spaces between variables correctly', () => {
const input: JSONContent = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'variableTag',
attrs: { variable: '{{user.firstName}}' },
},
{
type: 'text',
text: '\u00A0', // NBSP character
},
{
type: 'variableTag',
attrs: { variable: '{{user.lastName}}' },
},
],
},
],
};
expect(parseEditorContent(input)).toBe(
'{{user.firstName}} {{user.lastName}}',
);
});
});

View File

@@ -11,7 +11,8 @@ export const parseEditorContent = (json: JSONContent): string => {
}
if (node.type === 'text') {
return node.text || '';
// Replace   with regular space
return node?.text?.replace(/\u00A0/g, ' ') ?? '';
}
if (node.type === 'hardBreak') {