Skip to main content

State Machines: Precise Markdown Processing

The Terminal-to-Document Bridge

Why state machines? Because regex cannot provide The Brick’s core property: predictable, trustable output. Regex backtracks, overlaps, and produces ambiguous results. State machines process linearly, deterministically, and atomically - the foundation of trust.

The Regex Problem

Traditional markdown parsers use regex patterns. This creates fundamental problems in interactive editors.

The State Machine Solution

A Finite State Machine (FSM) processes text character-by-character, maintaining explicit state about what construct is being parsed.

State Transition Diagram

The Critical Decision Points


Range Object: The Document Brick

When parsing completes, the state machine produces Range Objects - precise position markers for every formatting construct.

Execution Trace: Parsing “bold


Smart Backspace: Synchronized Marker Deletion

In a hybrid editor where markers are visible, users expect intuitive deletion behavior.

Naive vs Smart Approach

Why Delete Closing First?


Gradual Degradation Pattern


Copy-Paste Fidelity: VmdBox

When users paste ASCII art or box-drawing content, several problems can occur:

The VmdBox Solution

How VmdBox Works

Auto-Scaling Visualization


Parallel State Machines: Terminal and Document

This parallelism enables seamless terminal-to-document workflow and ensures that Human + AI see the same visual representation.

SMPC Discovery: Nested Italic = Bold

A key insight that simplifies the entire implementation:

Summary


Related: variablemd/hybrid-editor/architecture.mdx, variablemd/core-concepts/codexmono.mdx