Skip to content

Fix: Prevent files from being marked as modified immediately on open - #25

Merged
Tkaixiang merged 3 commits into
Tkaixiang:fix/mikejoseph23-no-auto-format-on-openfrom
mikejoseph23:fix/no-auto-format-on-open
Jan 15, 2026
Merged

Tkaixiang merged 3 commits into
Tkaixiang:fix/mikejoseph23-no-auto-format-on-openfrom
mikejoseph23:fix/no-auto-format-on-open

Conversation

@mikejoseph23

@mikejoseph23 mikejoseph23 commented Oct 23, 2025 •

Copy link
Copy Markdown

Fixes #31

Problem

MarkText currently marks files as modified immediately upon opening them, even when no user changes have been made. This causes unnecessary "unsaved changes" warnings when switching tabs or closing files, creating a frustrating user experience.

Root Cause

The issue has two contributing factors:

  1. Automatic line ending normalization on file open: When loadMarkdownFile() detects CRLF line endings or mixed line endings, it automatically converts them to LF and sets adjustLineEndingOnSave = true. This marks the file as modified even though the user hasn't made any changes.

  2. Muya editor content normalization: When content is loaded into the Muya editor via setMarkdown(), the editor performs its own internal normalization (whitespace adjustments, etc.). This triggers a LISTEN_FOR_CONTENT_CHANGE event that marks the file as unsaved, even when the file was just opened.

Solution

This PR implements a two-part fix:

1. Optional Line Ending Normalization

Added a new preference autoNormalizeMarkdownOnOpen (default: false) that controls whether files should be automatically normalized when opened:

  • When disabled (default): Files are opened as-is with their original line endings. The editor still converts to LF internally (required by MarkText), but preserves the original format via adjustLineEndingOnSave flag. The file is only marked as modified when the user actually makes changes.

  • When enabled: Maintains the old behavior where files with CRLF or mixed line endings are normalized immediately.

Files modified:

  • src/main/filesystem/markdown.js - Added conditional normalization logic
  • src/main/preferences/schema.json - Added preference definition
  • src/renderer/src/prefComponents/editor/index.vue - Added UI toggle
  • static/locales/en.json - Added localization string
  • src/main/filesystem/watcher.js - Propagated preference through file watching
  • src/main/windows/editor.js - Passed preference to loadMarkdownFile

2. Ignore First Content Change from Muya

Added isFirstContentChange flag to the file state that ignores the first content change event after a file is loaded. This prevents Muya's internal normalization from marking files as unsaved.

Files modified:

  • src/renderer/src/store/help.js - Added isFirstContentChange: true to defaultFileState
  • src/renderer/src/store/editor.js - Modified LISTEN_FOR_CONTENT_CHANGE to skip first change

Testing

Tested on macOS with files containing:

  • CRLF line endings
  • LF line endings
  • Mixed line endings

Result: Files are no longer marked as modified when opened. The unsaved indicator only appears after actual user edits.

Impact

This fix eliminates a significant UI annoyance where users were constantly prompted about unsaved changes when they hadn't modified anything. Files are now only marked as modified when actual changes are made by the user.

Fixed a bug where opening a file would immediately mark it as modified,
prompting a save dialog even when no changes were made. The issue had two parts:

1. Added autoNormalizeMarkdownOnOpen preference (defaults to false) to control
   whether MarkText automatically normalizes line endings and detects trailing
   newlines when files are opened. When disabled, files are preserved as-is.

2. Added isFirstContentChange flag to ignore the initial content change event
   triggered by Muya editor normalizing content during file load. This prevents
   marking files as unsaved when they haven't been actually edited.

Changes:
- Added autoNormalizeMarkdownOnOpen parameter to loadMarkdownFile
- Modified line ending and trailing newline detection to only run when auto-normalize is enabled
- Added isFirstContentChange flag to file state to track first content change
- Updated content change handler to skip marking file as unsaved on first change
- Added UI toggle in preferences for the new setting
Added localization strings for the new autoNormalizeMarkdownOnOpen
preference to all supported languages:
- German (de.json)
- Spanish (es.json)
- French (fr.json)
- Japanese (ja.json)
- Korean (ko.json)
- Portuguese (pt.json)
- Simplified Chinese (zh-CN.json)
- Traditional Chinese (zh-TW.json)

The localization describes that this preference controls whether files
are automatically normalized when opened or only when saved.
@ffgan

ffgan commented Dec 20, 2025

Copy link
Copy Markdown

cc @Tkaixiang

Some simple test feedback
I did a simple build on my Fedora. The current trunk branch does indeed cause the issue mentioned in this modification, and after switching to this PR, the problem was resolved. This fix should be effective.

@Tkaixiang
Tkaixiang self-requested a review January 14, 2026 05:13
@Tkaixiang Tkaixiang self-assigned this Jan 14, 2026
@Tkaixiang Tkaixiang added the bug Something isn't working label Jan 14, 2026

@Tkaixiang Tkaixiang left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Hey, thanks for this PR, just a few comments

Comment thread src/main/filesystem/markdown.js Resolved
Comment thread src/main/preferences/schema.json Resolved
}

// Ignore the first content change after file load (Muya normalizes content)
if (this.currentFile.isFirstContentChange) {

@Tkaixiang Tkaixiang Jan 14, 2026 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Consider not doing this since it silently suppreses file changes if there was no normalisation initially, instead, perhaps a notification could be sent if auto-normalisation of line endings/trailing new lines was carried out.

An alternative could be to modify the ground truth markdown directly instead of modifying the end lines through a change event

@Tkaixiang
Tkaixiang changed the base branch from trunk to fix/mikejoseph23-no-auto-format-on-open January 15, 2026 12:57
@Tkaixiang

Copy link
Copy Markdown
Owner

Thanks for the PR, since I have extra time right now, I will be bringing this branch in for works

@Tkaixiang
Tkaixiang merged commit 0fd39f0 into Tkaixiang:fix/mikejoseph23-no-auto-format-on-open Jan 15, 2026
@Tkaixiang
Tkaixiang requested a review from Copilot January 15, 2026 15:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug where files were incorrectly marked as modified immediately upon opening, even when no user changes were made. The fix addresses two root causes: automatic line ending normalization on file open and Muya editor's internal content normalization.

Changes:

  • Added a new preference autoNormalizeMarkdownOnOpen (default: false) to control automatic normalization behavior when opening files
  • Implemented logic to ignore the first content change event after loading a file to prevent Muya's internal normalization from triggering the unsaved state
  • Propagated the new preference through the file watching and loading pipeline

Reviewed changes

Copilot reviewed 16 out of 17 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/main/preferences/schema.json Added the new preference definition with default value false
src/main/filesystem/markdown.js Added conditional normalization logic based on the new preference
src/main/filesystem/watcher.js Propagated the new preference parameter through file watching functions
src/main/windows/editor.js Extracted and passed the new preference to loadMarkdownFile
src/renderer/src/prefComponents/editor/index.vue Added UI toggle for the preference
src/renderer/src/store/help.js Added isFirstContentChange flag to defaultFileState
src/renderer/src/store/editor.js Implemented logic to skip the first content change after file load
static/locales/*.json Added translations for the new preference in all supported languages
.gitignore Added local Claude settings file to ignore list
Comments suppressed due to low confidence (1)

src/renderer/src/store/help.js:5

  • Corrected spelling of 'internel' to 'internal'.
 * Default internel markdown document with editor options.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Tkaixiang

Tkaixiang commented Jan 15, 2026 •

Copy link
Copy Markdown
Owner

Upon further review, this PR does not fully solve the problem unfortunately, it was simply surpressing the first change which sometimes included normalisation. See my explanation in #31

@mikejoseph23
mikejoseph23 deleted the fix/no-auto-format-on-open branch January 15, 2026 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Files marked as modified immediately upon opening (auto-normalization issue)

4 participants