From 81b24ecd71d3845ea8c0bdd8da3fdab3922f6edf Mon Sep 17 00:00:00 2001 From: Evandro Oliveira Date: Wed, 2 Oct 2019 16:23:45 -0300 Subject: [PATCH] Fix to not add multiple line feeds at end of file Closes GH-440. Reviewed-by: Titus Wormer Co-authored-by: Evandro Oliveira Co-authored-by: Titus Wormer --- packages/remark-stringify/lib/visitors/root.js | 8 +++++++- packages/remark-stringify/test.js | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/remark-stringify/lib/visitors/root.js b/packages/remark-stringify/lib/visitors/root.js index 33f30feb0..88e3c45f7 100644 --- a/packages/remark-stringify/lib/visitors/root.js +++ b/packages/remark-stringify/lib/visitors/root.js @@ -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 } diff --git a/packages/remark-stringify/test.js b/packages/remark-stringify/test.js index 377a17454..79ae009dd 100644 --- a/packages/remark-stringify/test.js +++ b/packages/remark-stringify/test.js @@ -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: '