Skip to content

fix: keep the final newline of markdown files on save - #4220

Merged
JammingBen merged 2 commits into
mainfrom
fix/markdown-final-newline
Nov 16, 2026
Merged

JammingBen merged 2 commits into
mainfrom
fix/markdown-final-newline

Conversation

@JammingBen

@JammingBen JammingBen commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Description

Saving a Markdown file in the text editor dropped its final newline, because the editor document can't hold it and the Markdown serializer doesn't reliably write one.

  • The markdown strategy now remembers whether the loaded file ended with a newline and restores it on save. New and empty files get one by default.
  • In collaborative (Yjs) mode, only the peer that loads the file sees it, so the flag is stored in the shared Y.Doc. Every peer then saves the same final newline. Resetting the doc clears the flag.
  • Edits in the source view now keep or drop the final newline as typed. This also applies in local mode, where the source view is editable. The new setFinalNewline() on TextEditorInstance makes this possible.

Related Issue

@JammingBen JammingBen self-assigned this Nov 3, 2026
@JammingBen
JammingBen force-pushed the fix/markdown-final-newline branch from 110370d to 4c17acc Compare November 3, 2026 10:27
@JammingBen
JammingBen marked this pull request as ready for review November 3, 2026 10:28
@JammingBen
JammingBen marked this pull request as draft November 3, 2026 10:38
@JammingBen
JammingBen marked this pull request as ready for review November 3, 2026 12:51
@AlexAndBear

Copy link
Copy Markdown
Member

Works great, but I wonder if all that extra hassle is really worth it just to remember a new line 🤔

we could also think about keeping the behaviour as known issue, what do you think?

@JammingBen

Copy link
Copy Markdown
Member Author

Works great, but I wonder if all that extra hassle is really worth it just to remember a new line 🤔

we could also think about keeping the behaviour as known issue, what do you think?

Yes I agree it's quite the edge case, although it would be nice for our editor to preserve the original file. @kulmann I'd leave it up for you to decide if it's worth the added logic. There is also a simpler solution that doesn't take the source view into account (bullet point 3 above), but that feels like a half baked solution.

@kulmann

kulmann commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

I agree that it's quite the change for a seemingly small improvement. But anything about newlines is a PITA from a users perspective, so I think preserving the original newline behaviour at the end of the file is a good thing.

@JammingBen
JammingBen force-pushed the fix/markdown-final-newline branch from c2a2d80 to ea1c0bd Compare November 10, 2026 10:16

@kulmann kulmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to strip all trailing newlines and then only add exactly one \n if a newline at EOF is supposed to be set?

@JammingBen

Copy link
Copy Markdown
Member Author

would it make sense to strip all trailing newlines and then only add exactly one \n if a newline at EOF is supposed to be set?

As briefly discussed in chat: let's preserve the user's input, since this is not a code editor that profits from the 1-newline-rule.

@JammingBen
JammingBen requested a review from kulmann November 10, 2026 13:21
@JammingBen
JammingBen force-pushed the fix/markdown-final-newline branch from ea1c0bd to 2c7bdb0 Compare November 16, 2026 07:16
@JammingBen
JammingBen merged commit b590299 into main Nov 16, 2026
28 checks passed
@JammingBen
JammingBen deleted the fix/markdown-final-newline branch November 16, 2026 08:40
@openclouders openclouders mentioned this pull request Nov 16, 2026
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Saving a Markdown file removes its final newline

3 participants