From 60454bb4dea3122c8b63ed9d00ef8c8ce5016281 Mon Sep 17 00:00:00 2001 From: Asaf Amrami Date: Sun, 7 Feb 2021 13:33:45 +0200 Subject: [PATCH] fix: json FM ending with object parse error (#4909) --- .../src/formats/__tests__/frontmatter.spec.js | 12 ++++++++++++ packages/netlify-cms-core/src/formats/frontmatter.js | 5 +---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/netlify-cms-core/src/formats/__tests__/frontmatter.spec.js b/packages/netlify-cms-core/src/formats/__tests__/frontmatter.spec.js index 3a5d9a58..2f14d732 100644 --- a/packages/netlify-cms-core/src/formats/__tests__/frontmatter.spec.js +++ b/packages/netlify-cms-core/src/formats/__tests__/frontmatter.spec.js @@ -351,6 +351,18 @@ describe('Frontmatter', () => { }); }); + it('should parse JSON with { } delimiters ending with a nested object', () => { + expect( + FrontmatterInfer.fromFile( + '{\n "title": "The Title",\n "nested": {\n "inside": "Inside prop"\n }\n}\nContent', + ), + ).toEqual({ + title: 'The Title', + nested: { inside: 'Inside prop' }, + body: 'Content', + }); + }); + it('should stringify JSON with { } delimiters when it is explicitly set as the format without a custom delimiter', () => { expect( frontmatterJSON().toFile({ diff --git a/packages/netlify-cms-core/src/formats/frontmatter.js b/packages/netlify-cms-core/src/formats/frontmatter.js index be052f08..e120bfa7 100644 --- a/packages/netlify-cms-core/src/formats/frontmatter.js +++ b/packages/netlify-cms-core/src/formats/frontmatter.js @@ -13,10 +13,7 @@ const parsers = { let JSONinput = input.trim(); // Fix JSON if leading and trailing brackets were trimmed. if (JSONinput.substr(0, 1) !== '{') { - JSONinput = '{' + JSONinput; - } - if (JSONinput.substr(-1) !== '}') { - JSONinput = JSONinput + '}'; + JSONinput = '{' + JSONinput + '}'; } return jsonFormatter.fromFile(JSONinput); },