Skip to content

obsidian/editor

This module provides utility functions for working with markdown editors in Obsidian

FunctionDescription
syncOpenEditorBuffersForPathOverwrites the buffer of every open MarkdownView currently showing the given path so that it reflects content, in any window or popout. This is the fix for a subtle data-loss window: when a note is open in an editor with a dirty buffer (a pending, not-yet-saved edit) and its file content is rewritten on disk out from under the editor, the editor’s next autosave re-writes its stale buffer and clobbers the on-disk content. A caller that restores a file’s content on disk (e.g. rolling back a transaction) must also reset the open editor’s buffer to the same content, so its next save is a no-op instead of a clobber. Only the view whose MarkdownView.file still matches pathOrFile is touched — if the editor has since navigated to another note, it is left alone. The buffer is replaced only when it actually differs from content, so a clean editor already in sync is not disturbed (no cursor reset).
toggleEditorReadOnlyToggles the read-only state of a single CodeMirror instance — the low-level primitive behind editor locking. When read-only, the editor stays focusable and interactive (selection, copy, and app hotkeys such as the command palette keep working) — only document edits are prevented, via the CodeMirror readOnly state plus a change filter that drops any user-originated change (see readOnlyExtension). This toggles ONE CodeMirror instance, not “a file”: an Obsidian Editor is reused as its leaf navigates between notes, so this primitive does not follow a note around. To lock a note across every editor that shows it (current and future, in any window), use lockResourceForPath from ./resource-lock.ts.