Skip to content

Commit

Permalink
Stop appending linefeeds on consecutive runs
Browse files Browse the repository at this point in the history
If I run `remark-cli` several times on a file, each run will insert a final newline to the end of the checked target file.

This commit adds a verification to check if the file already ends with a linefeed and doesn't include additional ones when not needed.
  • Loading branch information
oliveiraev committed Oct 2, 2019
1 parent 66cd49f commit 0f5fcbd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/remark-stringify/lib/visitors/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ var lineFeed = '\n'
// Stringify a root.
// Adds a final newline to ensure valid POSIX files. */
function root(node) {
return this.block(node) + lineFeed
var block = this.block(node)
if (block.substr(lineFeed.length * -1) === lineFeed) {
return block
}
return block + lineFeed
}
8 changes: 8 additions & 0 deletions packages/remark/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ test('remark().processSync(value)', function(t) {
'should accept stringify options'
)

t.equal(
remark()
.processSync('<!-- last line\n')
.toString(),
'<!-- last line\n',
'should not add more than one linefeed at the end'
)

t.throws(
function() {
remark()
Expand Down

0 comments on commit 0f5fcbd

Please sign in to comment.