fix: json FM ending with object parse error (#4909)

This commit is contained in:
Asaf Amrami 2021-02-07 13:33:45 +02:00 committed by GitHub
parent e481e6e296
commit 60454bb4de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -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({

View File

@ -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);
},