Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not add multiple line feeds at end of file #440

Closed
wants to merge 1 commit into from

Conversation

oliveiraev
Copy link
Contributor

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.

@wooorm
Copy link
Member

wooorm commented Oct 2, 2019

This shouldn’t happen 🤔 What code are you using that keeps on adding an extra line feed?

@oliveiraev
Copy link
Contributor Author

This shouldn’t happen What code are you using that keeps on adding an extra line feed?

I use VIM modelines at the end of my markdown files, and maybe the problem is that the comments are unclosed

https://asciinema.org/a/ec5mDFTHcvk09trCw8iKZFntL

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.
@wooorm
Copy link
Member

wooorm commented Oct 2, 2019

Hmm, it is expected that the initial file, without an EOF EOL, has an EOL. It is also expected that after adding the comment, another EOF EOL is added. But the other EOLs are superfluous 🤔

@oliveiraev
Copy link
Contributor Author

Hmm, it is expected that the initial file, without an EOF EOL, has an EOL. It is also expected that after adding the comment, another EOF EOL is added. But the other EOLs are superfluous

there are EOF EOL, they just doesn't show up. If they were missing, git diffs would report for the initial version...

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..b45ef6f
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+Hello, World!
\ No newline at end of file

...and for the comment version

diff --git a/README.md b/README.md
index 8ab686e..706a8d7 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,3 @@
 Hello, World!
+
+<!-- vim: set ai si sta et sw=4 sts=4 fenc=utf-8 nobomb eol ff=unix ft=markdown:
\ No newline at end of file

Also, after the very first run of remark, git would warn about changes due to the added EOF EOL.

@oliveiraev
Copy link
Contributor Author

I don't know typescript and can't make the build pass. Can you give me directions?

@wooorm
Copy link
Member

wooorm commented Oct 3, 2019

Ah, TS checking recently started failing: unifiedjs/unified@9786410. I’ll work on a fix

@wooorm
Copy link
Member

wooorm commented Oct 3, 2019

The problem is the final unclosed HTML:

var remark = require('./packages/remark')

var doc = `Hello, World!`

var index = 3

while (index--) {
  doc = remark()
    .processSync(doc)
    .toString()

  console.log([doc])
}

Yields:

$ node example.js 
[ 'Hello, World!\n' ]
[ 'Hello, World!\n' ]
[ 'Hello, World!\n' ]
var remark = require('./packages/remark')

var doc = `Hello, World!

<!-- asd -->`

var index = 3

while (index--) {
  doc = remark()
    .processSync(doc)
    .toString()

  console.log([doc])
}

Yields:

$ node example.js 
[ 'Hello, World!\n\n<!-- asd --->\n' ]
[ 'Hello, World!\n\n<!-- asd --->\n' ]
[ 'Hello, World!\n\n<!-- asd --->\n' ]
var remark = require('./packages/remark')

var doc = `Hello, World!

<!-- asd`

var index = 3

while (index--) {
  doc = remark()
    .processSync(doc)
    .toString()

  console.log([doc])
}

Yields:

$ node example.js 
[ 'Hello, World!\n\n<!-- asd\n' ]
[ 'Hello, World!\n\n<!-- asd\n\n' ]
[ 'Hello, World!\n\n<!-- asd\n\n\n' ]

@wooorm
Copy link
Member

wooorm commented Oct 3, 2019

Note btw, that VIM modelines, from the examples the doc you linked show, should be valid comments. <!-- so you need the final stuff as well: -->

@wooorm wooorm closed this in 81b24ec Oct 3, 2019
@wooorm
Copy link
Member

wooorm commented Oct 3, 2019

I refactor the code a bit; thanks for working on this!

@wooorm wooorm changed the title Stop appending linefeeds on consecutive runs Do not add multiple line feeds at end of file Oct 3, 2019
@wooorm wooorm added remark remark-cli remark-stringify 🐛 type/bug This is a problem 👶 semver/patch This is a backwards-compatible fix 🗄 area/interface This affects the public interface 💪 phase/solved Post is done labels Oct 3, 2019
@oliveiraev oliveiraev deleted the patch-1 branch October 3, 2019 10:33
@wooorm wooorm added the 💪 phase/solved Post is done label Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗄 area/interface This affects the public interface 💪 phase/solved Post is done remark remark-cli remark-stringify 👶 semver/patch This is a backwards-compatible fix 🐛 type/bug This is a problem
Development

Successfully merging this pull request may close these issues.

2 participants