Skip to content

Conversation

@mikejoseph23
Copy link

@mikejoseph23 mikejoseph23 commented Oct 23, 2025

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.

@mikejoseph23 mikejoseph23 force-pushed the fix/no-auto-format-on-open branch from ed6126b to a5bcb25 Compare October 23, 2025 21:18
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.
@mikejoseph23 mikejoseph23 force-pushed the fix/no-auto-format-on-open branch from a5bcb25 to a0c87d8 Compare October 24, 2025 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant