Our notes box is CodeMirror 6 with @codemirror/lang-markdown, written as one module and bundled twice: once by Vite for the Mosaic window, and once by esbuild into a single file that a Chrome extension popup loads with a script tag. That is the whole answer if you are building a CodeMirror Markdown editor for an extension. The rest of this post is how we got there, which was by writing our own editor first and watching it fail.

What the notes box is for

Every Mosaic profile has notes: which client it belongs to, what the account is for, what not to change. They are read in two places. One is the profile editor in the Mosaic window. The other is the companion, a small extension inside each profile's own browser that also holds that profile's logins and 2FA codes. So the same note is typed in an Electron window on one day and in a browser popup the next.

We wanted one box that is always editable and styles its own Markdown as you type. A heading still starts with #, but it is drawn at heading size. We did not want a Write/Preview toggle, because then you read your notes in one place and edit them in another. That decision was right and it is still how the box works.

The editor we wrote ourselves

The first version was a contenteditable div with a decorator we wrote by hand. The note was a string. Every keystroke spliced the string, rebuilt every node in the box, and then put the caret back by counting characters. Three reports came back from inside running browsers:

  • Enter did not reliably make a new line. It went through the splice-and-repaint path, and at a line boundary the restored caret landed in the wrong place. The report said it worked "only on the second line or something".
  • Inline code was drawn as three chips: the opening backtick, the word and the closing backtick, each its own span with its own background.
  • The box could not grow. It had a fixed minimum height inside a 548-pixel popup, with a third of the window empty below it.

execCommand('insertText') made it worse, because it drops newlines, so a pasted paragraph of three lines arrived as one.

None of those was a bug we could go and find. Selection, composition, undo, input methods for other scripts and right-to-left text are what a text editor is. After the third report we stopped fixing ours and took one that already handles all of it.

Each profile keeps its own passwords and its own 2FA codesIn Mosaic: Logins and 2FA codes. A password vault and TOTP codes for each profile. Free covers 3 profiles with no card, and Pro is $25 a month for any number of them.

How it works: Logins and 2FA codes

One module, two hosts

src/shared/notes-editor.ts is about 290 lines: CodeMirror's view and state, its default keymap and history, the Markdown language, and our own decorations. The window imports it like any other module.

The companion cannot import anything. Mosaic writes it to the profile's folder as an unpacked extension every time the browser starts, so it needs the editor as one file of plain JavaScript with nothing left to resolve. A build script runs esbuild over the same source with format: 'iife' and minify: true, and writes the result into a TypeScript file as a JSON string. The companion writes that string to disk beside popup.js. The generated file is about 500 KB of source, and it is committed, because the app has to produce the extension on a machine that has never run a build step.

A committed, generated file goes stale. So the same script has a --check mode, and our build fails when the bundle no longer matches the module the window imports. We store it as a JSON string, not a template literal, for a reason we learned the hard way: minified JavaScript is full of backticks and ${, and either one ends a template literal early. The companion's popup is itself generated from template literals, and a backtick in a code comment had already broken its script several times while we worked on it, each time caught by a parse check before it shipped.

Colours come from the host. Every colour in the editor's theme is a CSS variable such as --md-text or --md-code-bg. The window declares them in its stylesheet and the popup declares the same names in its own, so one editor is dark in the window and follows the browser's theme in the popup without knowing which it is in.

Syntax highlighting cannot draw a code block

This is the part most CodeMirror Markdown examples skip. Put a background on the monospace tag through HighlightStyle and you get our old bug back by a new route: the grammar tags the code and its two backticks separately, so the background paints three chips with bare backticks between them.

The fix is to read the node, not the tag. A view plugin walks syntaxTree(view.state) over the visible ranges. An InlineCode node gets one Decoration.mark from the first backtick to the last. A FencedCode or CodeBlock node gets a Decoration.line on every line it covers, with its own class on the first and the last line for the block's top and bottom edges, so the block is drawn as one rectangle and not a ragged set of runs.

A full-height editor that was 39 pixels tall

In the popup the bordered box measured 418 pixels high and the editor inside it measured 39. It looked like a working full-height box until you clicked the empty part and nothing took focus.

The cause is CSS, not CodeMirror. A percentage height resolves against the parent's computed height, and a flex item sized by flex: 1 has an auto height, so height: 100% on the child silently becomes auto. The child has to be a flex item as well.

A counter nobody asked for

The old box showed "Line 3 of 12" under the text, recalculated on every caret move. It was absent until you clicked and then changed under your eye while you read what you had typed. We took it out.

How we know Enter works

A check called check:notes-typing opens an Electron window, loads the exact bundle the companion ships, types into it with real key events and reads the document back. A synthetic dispatchEvent would skip the browser's own input handling, which is where the old fault lived. With Enter unbound on purpose, the check reads back "onetwothree" and fails, which is how we know it can.

Notes are one small part of keeping each account's context with its profile; the local-first page covers where all of it is stored, and why we built Mosaic covers why it stays on your computer.