Add function to tighten parentheses around EJS output and corresponding test
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good

This update introduces a new function, `tightenParenthesesAroundEjs`, which removes unnecessary spaces inside parentheses surrounding EJS output in formatted templates. Additionally, a test case is added to ensure that the formatting behaves as expected, enhancing the overall template content formatting functionality.
This commit is contained in:
Tom Butcher 2026-08-19 20:15:09 +01:00
parent f467b1be33
commit 5cbcdacd3b
2 changed files with 15 additions and 0 deletions

View File

@ -49,4 +49,14 @@ describe('formatTemplateContent', () => {
expect(result.content).toContain('<%# comment %>'); expect(result.content).toContain('<%# comment %>');
}); });
it('should not add spaces inside parentheses around EJS output', () => {
const input =
'<Text><Bold><%= courierService.name %></Bold> ( <%= courier.name %> )</Text>';
const result = formatTemplateContent(input);
expect(result.content).toContain('</Bold> (<%= courier.name %>)');
expect(result.content).not.toMatch(/\(\s+<%/);
expect(result.content).not.toMatch(/%>\s+\)/);
});
}); });

View File

@ -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) { export function formatTemplateContent(content) {
if (content == null || typeof content !== 'string') { if (content == null || typeof content !== 'string') {
return { error: 'Content is required and must be a string.', code: 400 }; 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); const { masked, ejsBlocks } = maskEjsBlocks(content);
let formatted = beautifyHtml(masked, HTML_BEAUTIFY_OPTIONS); let formatted = beautifyHtml(masked, HTML_BEAUTIFY_OPTIONS);
formatted = restoreEjsBlocks(formatted, ejsBlocks); formatted = restoreEjsBlocks(formatted, ejsBlocks);
formatted = tightenParenthesesAroundEjs(formatted);
return { content: formatted.trimEnd() }; return { content: formatted.trimEnd() };
} catch (error) { } catch (error) {
return { error: error.message || 'Failed to format template content.', code: 400 }; return { error: error.message || 'Failed to format template content.', code: 400 };