Building my own editor

An editor I built into this site that only exists on my laptop. Try its card menu and typewriter mode here, in the page.

9 min read

I’ve been using iA Writer a lot for writing my drafts, and it served me well. But since I made this site, there has been an extra step. Whenever a post needed to link to another of my posts, I wrote the draft in iA Writer and left myself TODO comments along the way: a wikilink (a link to one of my own posts) goes here, a component goes there, something else after that.

Here is roughly what that looked like.

The way it used to work
A draft in iA Writer, with TODO notes for links and components.
Playing by itself Step 1 of 3
The draft is done. Move it across to VS Code.
The old way: draft in iA Writer, leave TODOs, then move it into VS Code and type the frontmatter from memory.

Then I moved it across to VS Code and worked through the TODOs. New file, .mdx (Markdown that can hold components), and then a pause at the top of an empty document while I tried to remember what goes in the frontmatter this time. The block at the top that says what the post is. A note wants one set of keys, a live note wants another, a work post wants fourteen.

This is what starting a post looks like in the editor now. Pick a collection, give it a slug, and the frontmatter is written for you, with a field for everything that collection asks for.

Start a new post
Local editor
Posts
Building QuoteIt playground
The series master card playground
AI tools as Creative Partners notes
The editor’s list of posts.
Playing by itself Step 1 of 5
Click New post.
Pick a collection and a slug. The settings come from that collection’s schema, and the frontmatter is written as you fill them in.

Then the components. The site has eighteen you can drop into a post, and nothing in VS Code knew about any of them. ImagesLeft takes imageSrc, Image takes src, and if you get that backwards nothing shouts at you.

So I built this editor, combining the experience I had with iA Writer and the experience I wanted.

What I built

An editor for this site that only exists while the dev server is running (the copy of the site that runs on my laptop and nowhere else). No login, no dashboard, nothing hosted.

It takes the writing from iA Writer and the quick menu from Ghost (another publishing tool), and puts my components inside the place I write. Having them right there made thinking about the story easier.

The part I’m quietly pleased about is the cards. When I insert a Callout or a Quote, it isn’t a grey box labelled “Callout”. It is the real component, the one the site ships, rendering as itself inside the document I’m typing in. Change a prop in the settings panel and the thing in front of me changes, because it is the same thing.

How I built it

Over nine days in September, in twenty one commits. I didn’t write most of the code by hand. I wrote plans, Codex (an AI coding tool) implemented them, and I reviewed what came back. That part did not go the way the marketing suggests, and most of the interesting moments are where something pushed back.

The first thing I built wasn’t the editor. It was a throwaway script that opened all forty posts on this site and saved them again without changing anything. Thirty five came back the same. Five didn’t: a blank line before the opening --- in four of my live notes, a list that got rewritten in a different shape, and one excerpt that made the parser give up completely. So the rule became that a save only renders again the paragraph I actually touched, and everything else is written back exactly as it was.

Then the question of where it lives. A hosted editor needs a login, and a login needs users, sessions and a way to reset a password I would forget anyway, all for one person. So the editor is simply not there in production. Its pages are never created when the site is built, so they aren’t protected, they aren’t there. I didn’t build a lock, I built absence, and a test builds the site and searches it to prove nothing leaked.

I was wrong about that being enough. Four days later I put four checks on it, because the dev server on my laptop is still a real server, and any page in the browser tab you already have open can talk to it. The first post I tried to write in the editor was about the editor, and it told me “invalid slug”. It was right and it was useless, so now the message shows you the shape it wants.

Remembering the components

That is the problem I actually started with. Eighteen components, each with props I had to remember, and an editor that couldn’t tell me any of it.

The first card menu was a flat list of nineteen bare labels. It worked, in the sense that the thing you clicked appeared. So I rebuilt it after Ghost’s quick menu: searchable, grouped into Primary, Embeds, Structure and Showcase, with an icon and a one line description on every card.

Try it. Type / on an empty line and the menu opens.

Type / for a card
Building my own editor
Every post here used to start as an empty file and a guess at which components existed.
Now the cards come to you.
Saved as MDX
An empty line in a draft.
Playing by itself Step 1 of 4
Click the empty line and type /.
Type / on an empty line. The menu filters as you type, and Enter puts the card in.

There was a trap underneath. The keyboard walks a single flat list, so arrow down goes from one to two to three. If you group the rows for display and the order changes even slightly, the arrow keys land somewhere you didn’t expect. The grouped menu has to flatten back to exactly the same list in exactly the same order. You would never see that in a screenshot, and you would hit it within ten seconds of using the menu.

Typewriter mode

This is the iA Writer half, and the part I actually wanted. The line you are writing stays in the middle of the screen. The paragraph you are in stays at full strength and everything else dims, so you only ever look at the sentence you are on.

In my editor it is on by default, with a toggle, because it gets obstructive when you are editing the middle of a post or clicking around.

The way it got built is my favourite story from the whole project. ProseMirror (the library the editor is made of) already has the machinery to centre the caret, and two lines of config would have done it. The arithmetic even worked out exactly. It was the most attractive wrong answer I hit on this project, and it died when I read the source. Typing asks the page to scroll to the caret, so it would have centred while you type. A plain click doesn’t ask, so the caret would sit wherever you clicked. Half a typewriter mode, and specifically the half that feels broken. So it is a small plugin that reacts to every selection change instead.

Type in the draft below, then click somewhere else and keep going.

Write anywhere in this draft
Draft
I wrote this paragraph first, and it is already fading.
Typewriter mode keeps your eyes in one place. The page moves, you do not.
Everything you are not writing steps back, so the sentence in front of you is the only loud thing on the screen.
This is the paragraph I am writing now, one word at a time.
The paragraphs below wait their turn, faint until the caret gets there.
A draft in typewriter mode.
Playing by itself Step 1 of 4
Click into the fourth paragraph.
Type anywhere. The line you are on holds the middle of the window, and everything else steps back.

One honest note. The real editor switches typewriter mode off on narrow screens, though this demo works on a phone.

Three small decisions

The caret stays the browser’s own. iA Writer draws a thick bar, and I wanted that. But CSS can’t change how wide a caret is, so a thick one means hiding the real one and drawing your own. Then you have to track blinking, focus, selection, and every card boundary by hand, and it breaks for anyone typing with an input method editor. So it is one line of CSS that colours the browser’s own caret in the site’s blue, and the browser keeps doing the hard part.

The dim is 0.35. I first asked for 90% opacity on the paragraphs you aren’t in. That is a 10% dim, and on this site’s dark teal over white it is nearly invisible. I had said a number that sounded like “slightly faded” and specified something you couldn’t see. It lives in one value now, so retuning it is one edit.

The lit unit is the whole top level block. If you are typing inside a card, the whole card stays lit, label and frame included. Dimming a card’s own label while you type in it reads as broken, not focused. My first test for this passed anyway, by accident. The card was last in the document, so the wrong position fell off the end and landed back on the card. Right answer, wrong reason, and it would have kept passing until someone put a paragraph after a card.

What testing kept finding

Two things stuck with me.

The first was creating a post. On the second day I wrote a test that drives the real editor in a real browser: make a post, type, insert a card, wait for it to say Saved, then read the file off disk. Creating a post turned out to be broken. A brand new post came back as an empty document, which the editor refuses, so you couldn’t write in it at all. Every earlier check had missed it, because by hand I always opened a post that already existed.

Then it got worse. Whenever the editor saved, the site told the page to reload itself, which throws away the paragraph you are in the middle of. I had already stopped that, but only once the editor had loaded, and the reload could arrive first. Chromium won that race every time. WebKit (Safari’s engine) lost it. Had I only tested in the browser I reach for first, it would have shipped, and eaten a paragraph now and then on a slow load.

The second was an error I wrote down on the first morning and worked around. Nine days later it was still there, and it had never worked at all. The plugin that turns [[wikilinks]] into links cannot work with the parser this site uses, so any wikilink in any post would have failed the build. The editor had a shortcut for typing exactly that syntax. I had built a feature that quietly made broken posts, and nothing noticed, because nothing I had written used the syntax yet.

What’s still rough

It only runs on my laptop, so I can’t write from my phone, which is the price of having no login. Typewriter mode is desktop only. And the demos above are copies built to play in a page, not the editor itself: the menu and the maths are the real ones, the rest is a stand in.

An empty file

Every post still starts the same way, an empty .mdx file and a blinking cursor. I just don’t have to remember anything first. I type /, and the menu remembers for me.

seed maturity Seed — this note will change as the strip does