Skip to content

Commit

Permalink
fix: strip line break added by gray matter
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed Feb 27, 2020
1 parent 5992b4a commit 8c6a78e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ describe('Frontmatter', () => {
'title: YAML',
'---',
'Some content',
'On another line\n',
'On another line',
].join('\n'),
);
});

it('should stringify YAML with missing body', () => {
expect(FrontmatterInfer.toFile({ tags: ['front matter', 'yaml'], title: 'YAML' })).toEqual(
['---', 'tags:', ' - front matter', ' - yaml', 'title: YAML', '---', '', ''].join('\n'),
['---', 'tags:', ' - front matter', ' - yaml', 'title: YAML', '---', ''].join('\n'),
);
});

Expand All @@ -206,7 +206,7 @@ describe('Frontmatter', () => {
'title: YAML',
'---',
'Some content',
'On another line\n',
'On another line',
].join('\n'),
);
});
Expand All @@ -227,7 +227,7 @@ describe('Frontmatter', () => {
'title: YAML',
'~~~',
'Some content',
'On another line\n',
'On another line',
].join('\n'),
);
});
Expand All @@ -248,7 +248,7 @@ describe('Frontmatter', () => {
'title: YAML',
'^^^',
'Some content',
'On another line\n',
'On another line',
].join('\n'),
);
});
Expand All @@ -267,7 +267,7 @@ describe('Frontmatter', () => {
'title = "TOML"',
'+++',
'Some content',
'On another line\n',
'On another line',
].join('\n'),
);
});
Expand All @@ -286,7 +286,7 @@ describe('Frontmatter', () => {
'title = "TOML"',
'~~~',
'Some content',
'On another line\n',
'On another line',
].join('\n'),
);
});
Expand All @@ -308,7 +308,7 @@ describe('Frontmatter', () => {
' "title": "JSON"',
'}',
'Some content',
'On another line\n',
'On another line',
].join('\n'),
);
});
Expand All @@ -330,8 +330,24 @@ describe('Frontmatter', () => {
' "title": "JSON"',
'~~~',
'Some content',
'On another line\n',
'On another line',
].join('\n'),
);
});

it('should trim last line break if added by grey-matter', () => {
expect(
frontmatterYAML().toFile({
body: 'noLineBreak',
}),
).toEqual('noLineBreak');
});

it('should not trim last line break if not added by grey-matter', () => {
expect(
frontmatterYAML().toFile({
body: 'withLineBreak\n',
}),
).toEqual('withLineBreak\n');
});
});
9 changes: 8 additions & 1 deletion packages/netlify-cms-core/src/formats/frontmatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class FrontmatterFormatter {
const format = this.format || inferFrontmatterFormat(content);
if (this.customDelimiter) this.format.delimiters = this.customDelimiter;
const result = matter(content, { engines: parsers, ...format });
// in the absent of a body when serializing an entry we use an empty one
// when calling `toFile`, so we don't want to add it when parsing.
return {
...result.data,
...(result.content.trim() && { body: result.content }),
Expand All @@ -83,8 +85,13 @@ class FrontmatterFormatter {
const format = this.format || getFormatOpts('yaml');
if (this.customDelimiter) this.format.delimiters = this.customDelimiter;

// gray-matter always adds a line break at the end which trips our
// change detection logic
// https://github.com/jonschlinkert/gray-matter/issues/96
const trimLastLineBreak = body.slice(-1) !== '\n' ? true : false;
// `sortedKeys` is not recognized by gray-matter, so it gets passed through to the parser
return matter.stringify(body, meta, { engines: parsers, sortedKeys, ...format });
const file = matter.stringify(body, meta, { engines: parsers, sortedKeys, ...format });
return trimLastLineBreak && file.slice(-1) === '\n' ? file.substring(0, file.length - 1) : file;
}
}

Expand Down

0 comments on commit 8c6a78e

Please sign in to comment.