diff --git a/client/src/components/GiftTemplate/templates/TextTypeTemplate.ts b/client/src/components/GiftTemplate/templates/TextTypeTemplate.ts index 154f59f..8a3e24b 100644 --- a/client/src/components/GiftTemplate/templates/TextTypeTemplate.ts +++ b/client/src/components/GiftTemplate/templates/TextTypeTemplate.ts @@ -35,7 +35,7 @@ export function FormattedTextTemplate(formattedText: TextFormat): string { case 'moodle': case 'plain': // Replace newlines with
tags - result = formatText.replace(/(?:\r\n|\r|\n)/g, '
'); + result = replaceNewlinesOutsideSVG(formatText); break; case 'html': // Strip outer paragraph tags (not a great approach with regex) @@ -50,3 +50,25 @@ export function FormattedTextTemplate(formattedText: TextFormat): string { } return DOMPurify.sanitize(result); } + +// Function to replace \n outside of SVG paths +function replaceNewlinesOutsideSVG(text: string): string { + const svgPathRegex = /]*d="([^"]*)"[^>]*>/g; + let result = ''; + let lastIndex = 0; + + // Iterate over all SVG paths + text.replace(svgPathRegex, (match, _p1, offset) => { + // Append text before the SVG path, replacing \n with
+ result += text.slice(lastIndex, offset).replace(/(?:\r\n|\r|\n)/g, '
'); + // Append the SVG path without replacing \n + result += match; + // Update the last index + lastIndex = offset + match.length; + return match; + }); + + // Append the remaining text, replacing \n with
+ result += text.slice(lastIndex).replace(/(?:\r\n|\r|\n)/g, '
'); + return result; +}