# A CodeMirror Markdown editor inside an extension

> We replaced a hand-written contenteditable notes box with CodeMirror 6 and its Markdown grammar, bundled once for the app and once for a Chrome extension.

Source: https://mosaicbrowser.com/blog/notes-editor

## A text editor is not ours to write

We replaced a hand-written contenteditable notes box with CodeMirror 6 and its Markdown grammar, bundled once for the app and once for a Chrome extension.

![One editor, two hosts, beside a screenshot: the Mosaic companion inside a profile browser: notes, saved logins and two-factor codes for that profile](https://mosaicbrowser.com/covers/notes-editor.webp)

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](https://mosaicbrowser.com/features/logins-and-2fa). 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.

Mosaic is a desktop app

Email yourself the link and open it on your computer.

[How it works: Logins and 2FA codes](https://mosaicbrowser.com/features/logins-and-2fa)

## 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](https://mosaicbrowser.com/features/local-first) covers where all of it is stored, and [why we built Mosaic](https://mosaicbrowser.com/blog/why-we-built-mosaic) covers why it stays on your computer.

## Questions

### Can CodeMirror 6 run inside a Chrome extension popup?

Yes. Bundle your editor module into one IIFE file with esbuild, load it from the popup's HTML with a script tag, and it needs nothing else at runtime. Ours is a single minified file written beside popup.js.

### Why does inline code in CodeMirror Markdown show three separate backgrounds?

The Markdown grammar tags the backticks and the code between them separately, so a background set through syntax highlighting paints three pieces. Read the InlineCode node from the syntax tree and put one Decoration.mark over the whole range instead.

### Why is my CodeMirror editor only one line tall inside a flex layout?

A percentage height resolves against the parent's computed height, and a flex item sized by flex: 1 has none, so height: 100% quietly becomes auto. Make the editor a flex item too.

### Where are the notes kept?

On your computer, with the profile they belong to. The companion shows the notes for the profile whose browser it is in, and no other.

## Related

[2FA codes per browser profile, with a login vault each](https://mosaicbrowser.com/features/logins-and-2fa): Mosaic keeps 2FA codes per browser profile: each profile has its own login vault with TOTP codes, shown with a countdown and typed with real keystrokes.

[Local first: your profiles stay on your computer](https://mosaicbrowser.com/features/local-first): Mosaic has no cloud copy of your profiles. Profiles, cookies and proxies stay on your computer, encrypted on disk, with app lock, daily backups and a trash.

[Why we built Mosaic: one browser per account, kept local](https://mosaicbrowser.com/blog/why-we-built-mosaic): Mosaic is a local-first anti-detect browser: each profile's fingerprint, proxy and cookies live in one folder on your computer, with no cloud copy.

[Why the parts of a profile have to agree](https://mosaicbrowser.com/blog/parts-of-a-profile-agree): Fingerprint, proxy and cookies consistency: sites compare the three, and each can look fine alone while together they describe nobody.









[An invalid TOTP secret key, checked only where it was read](https://mosaicbrowser.com/blog/totp-key-checked-at-write): A TOTP secret only base32 can't spell got stored and showed as a row of dashes forever. The fix: validate where the value is written, not read.

[An authenticator inside the profile, and four faults](https://mosaicbrowser.com/blog/authenticator-in-the-profile): The companion extension in each Mosaic profile shows its own TOTP codes, read from a QR code or a pasted key. Four faults we found by driving it over CDP.

[A list rebuilt every second threw away what you revealed](https://mosaicbrowser.com/blog/timer-rebuild-discards-state): Our authenticator rebuilt every row each second, so a revealed setup key lasted under a second. A tick updates in place; only a new shape rebuilds.

## Mosaic, on your own computer

The browser this blog is about: its own Chromium, fingerprints from real machines and a proxy IP per profile. Free covers 3 profiles with no card, and Pro is $25 a month for any number of them.

Mosaic is a desktop app

Email yourself the link and open it on your computer.
