diff --git a/src/templates/__tests__/templateformatter.test.js b/src/templates/__tests__/templateformatter.test.js index 3ef4b27..5a8b655 100644 --- a/src/templates/__tests__/templateformatter.test.js +++ b/src/templates/__tests__/templateformatter.test.js @@ -49,4 +49,14 @@ describe('formatTemplateContent', () => { expect(result.content).toContain('<%# comment %>'); }); + + it('should not add spaces inside parentheses around EJS output', () => { + const input = + '<%= courierService.name %> ( <%= courier.name %> )'; + const result = formatTemplateContent(input); + + expect(result.content).toContain(' (<%= courier.name %>)'); + expect(result.content).not.toMatch(/\(\s+<%/); + expect(result.content).not.toMatch(/%>\s+\)/); + }); }); diff --git a/src/templates/templateformatter.js b/src/templates/templateformatter.js index 9dd84aa..6182ff9 100644 --- a/src/templates/templateformatter.js +++ b/src/templates/templateformatter.js @@ -65,6 +65,10 @@ function restoreEjsBlocks(content, ejsBlocks) { ); } +function tightenParenthesesAroundEjs(content) { + return content.replace(/\(\s+(<%)/g, '($1').replace(/(%>)\s+\)/g, '$1)'); +} + export function formatTemplateContent(content) { if (content == null || typeof content !== 'string') { return { error: 'Content is required and must be a string.', code: 400 }; @@ -78,6 +82,7 @@ export function formatTemplateContent(content) { const { masked, ejsBlocks } = maskEjsBlocks(content); let formatted = beautifyHtml(masked, HTML_BEAUTIFY_OPTIONS); formatted = restoreEjsBlocks(formatted, ejsBlocks); + formatted = tightenParenthesesAroundEjs(formatted); return { content: formatted.trimEnd() }; } catch (error) { return { error: error.message || 'Failed to format template content.', code: 400 };