CodeMirror

wiki

CM6 = CodeMirror 6

  • The text editor library Obsidian uses for its Edit mode (Source + Live Preview). Replaced CM5 in Obsidian 0.14+.
  • Modular, functional, immutable-state design. Fits your FP leanings.
  • Reading mode is not CM6. It’s rendered HTML (use post-processors there).

Core concepts

EditorState: immutable snapshot (doc text, selection, extensions’ state). Never mutated.

Transaction: describes a change (text edits, selection, effects). Dispatching one produces a new state.

EditorView: the DOM layer. Renders state; view.dispatch(tr) is the only write path.

Extension: everything is one. Keymaps, themes, decorations, listeners. Composable arrays.

StateField: custom state that lives inside EditorState, updated per transaction via a pure update(value, tr).

StateEffect: typed messages you attach to transactions to drive your fields (like actions in Redux).

Facet: a way for multiple extensions to contribute/combine config values.

ViewPlugin: class with view lifecycle (update, destroy). For DOM-coupled stuff and viewport-aware work.

Decoration: visual markup over the doc without changing text:

  • Decoration.mark: style a range (CSS class)
  • Decoration.widget: inject a DOM element at a position
  • Decoration.replace: swap a range for a widget (how Live Preview hides markdown syntax)
  • Decoration.line: style a whole line

Decorations must be sorted by position. Build with RangeSetBuilder (add in order) or Decoration.set(ranges, true).

Resources

  • codemirror.net/docs/guide (read the full guide, ~1 hr)
  • codemirror.net/examples (decoration, zebra stripes, tooltips)
  • Source: Obsidian Style Settings, Obsidian Vim, Dataview’s inline-field decorations