From f09991de96a51b4704cd584fa6d4dc67f5a4f86a Mon Sep 17 00:00:00 2001 From: Alex Komoroske Date: Sat, 22 Jul 2023 16:32:02 -0700 Subject: [PATCH] Made the JSON extractor more forgiving. Change the regular expression to just match everything between curly braces. This makes a template of `{{data|json}}` to match across multi-line JSON work (test added). Part of #17. Part of #45. --- src/template.ts | 2 +- test/template/test.ts | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/template.ts b/src/template.ts index af438cd..50a1107 100644 --- a/src/template.ts +++ b/src/template.ts @@ -75,7 +75,7 @@ const VALUE_PATTERNS : {[t in TemplateVarType]: string} = { 'float': '-?\\d+?(\\.\\d+?)?', 'boolean': [...Object.keys(TRUE_LITERALS), ...Object.keys(FALSE_LITERALS)].join('|'), 'whitespace': '\\s+', - 'json': String.raw`((\[[^\}]{3,})?\{s*[^\}\{]{3,}?:.*\}([^\{]+\])?)` + 'json': '\\{([^}]*)\\}' }; //templates with a var of IGNORE_VAR should be ignored for rendering and diff --git a/test/template/test.ts b/test/template/test.ts index 435cda2..f4edf25 100644 --- a/test/template/test.ts +++ b/test/template/test.ts @@ -668,6 +668,29 @@ describe('template.extract', () => { assert.deepStrictEqual(actual, golden); }); + it('json modifier full text multi-line', () => { + const template = '{{foo|json}}'; + const input = `{ + "a": 1, + "b":[ + 2, + 3 + ] +}`; + const t = new Template(template); + const actual = t.extract(input); + const golden = { + foo: { + a: 1, + b: [ + 2, + 3 + ] + } + }; + assert.deepStrictEqual(actual, golden); + }); + it('json modifier', () => { const template = 'foo {{foo|json}} bar'; const input = 'foo {"a": 1, "b":[2,3]} bar';