Skip to content

editor/language

Hosts the editor-class tokenizer in CodeMirror 6 as a Language, the way Obsidian’s editor hosts its mode.

CodeMirror 6’s StreamLanguage cannot run the tokenizer: it gives a mode no way to read the lines after the current one, and setext headings and tables need them. This host reads the document through the parser’s Input, so the mode’s lookAhead reads the real next lines.

The tree has the shape of the editor’s: a line with line classes is a node named by them, holding its tokens, and every token is a node named by its classes, sorted and joined by _. Lines are grouped into chunks, anonymous nodes a tree cursor steps through, and each chunk records the state after its last line and how far its lines read, lookahead included. A new parse after an edit resumes from the last chunk whose reads all lie before the edit, and once a line after the edit ends in the state the earlier parse had there, it reuses the earlier parse’s chunks. In an editor, the parse and the reuse stop at the end of the viewport.

Obsidian’s editor renders classes from two node props its copy of @codemirror/language adds, lineClassNodeProp and tokenClassNodeProp, which the public package does not have; a host inside Obsidian passes them as options.

Interface Description
CreateEditorClassLanguageOptions Options of createEditorClassLanguage. A host inside Obsidian passes the app’s apiVersion as obsidianVersion.
Function Description
createEditorClassLanguage Creates the editor-class tokenizer as a CodeMirror 6 language.
Variable Description
editorClassLanguage The editor-class tokenizer as a CodeMirror 6 language.
lineClassesProperty The classes of a line node’s type, rendered on .cm-line.
tokenClassesProperty The classes of a token node’s type.