Skip to content

Commit

Permalink
Fix to not add multiple line feeds at end of file
Browse files Browse the repository at this point in the history
Closes GH-440.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>

Co-authored-by: Evandro Oliveira <evandrofranco@gmail.com>
Co-authored-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
oliveiraev and wooorm committed Oct 3, 2019
1 parent 4e020e8 commit 81b24ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 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,11 @@ var lineFeed = '\n'
// Stringify a root.
// Adds a final newline to ensure valid POSIX files. */
function root(node) {
return this.block(node) + lineFeed
var doc = this.block(node)

if (doc.charAt(doc.length - 1) !== lineFeed) {
doc += lineFeed
}

return doc
}
12 changes: 12 additions & 0 deletions packages/remark-stringify/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ var pedantic = {pedantic: true}
var uncollapsable = {start: {line: 1, column: NaN}, end: {line: 1, column: NaN}}

test('remark().stringify(ast, file)', function(t) {
t.equal(
unified()
.use(stringify)
.stringify({
type: 'root',
children: [{type: 'html', value: '<!-- last line\n'}]
})
.toString(),
'<!-- last line\n',
'should not add more than one line feeds at the end'
)

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

0 comments on commit 81b24ec

Please sign in to comment.