fix(template-function-json): escape control characters in json.escape (#607)

This commit is contained in:
Ngo Quoc Viet
2026-08-24 21:14:14 -07:00
committed by GitHub
parent 9a7bcf73bb
commit cef1129d4b
3 changed files with 57 additions and 2 deletions
+5 -1
View File
@@ -85,7 +85,11 @@ export const plugin: PluginDefinition = {
],
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
const input = String(args.values.input ?? "");
return input.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
// JSON.stringify produces a spec-correct string literal: it escapes
// the backslash and quote this used to handle, and also the control
// characters it did not. Slicing off the surrounding quotes leaves
// the escaped inner text this function is meant to emit.
return JSON.stringify(input).slice(1, -1);
},
},
{