Skip to content

Commit dfe5438

Browse files
fix(mf2): Escape \ { | } when stringifying messages
1 parent 869d4cb commit dfe5438

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

packages/mf2-messageformat/src/data-model/stringify.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,18 @@ function stringifyPattern(pattern: Pattern, quoted: boolean) {
109109
quoted = true;
110110
}
111111
for (const el of pattern) {
112-
if (typeof el === 'string') res += el;
112+
if (typeof el === 'string') res += stringifyString(el, quoted);
113113
else if (el.type === 'markup') res += stringifyMarkup(el);
114114
else res += stringifyExpression(el);
115115
}
116116
return quoted ? `{{${res}}}` : res;
117117
}
118118

119+
function stringifyString(str: string, quoted: boolean) {
120+
const esc = quoted ? /[\\|]/g : /[\\{}]/g;
121+
return str.replace(esc, '\\$&');
122+
}
123+
119124
function stringifyExpression({ arg, annotation, attributes }: Expression) {
120125
let res: string;
121126
switch (arg?.type) {

0 commit comments

Comments
 (0)