Opening a file shouldn't change it

Open a page in a visual editor, save it without typing, and run git diff. Here's why ours prints nothing, and the bug that taught us how.

Sahil Gadimbayli

Founder & CTO

Engineering5 min read

Open a Markdown file in a visual editor, change nothing, and save it. Then run git diff. If the editor parses the file and prints it back out, you'll find changes you didn't make: quotes gone, bullets swapped, a blank line that wasn't there before.

In a word processor nobody would notice. In a Git repository it's a commit full of lines nobody wrote, and a reviewer reading every one of them to find the typo fix hidden in the middle.

In April I wrote that your docs belong in a Git repository, and that our editor isn't allowed to do this. The same files get edited in VS Code, on GitHub and from a terminal. This post is the how: what the obvious approach breaks, what we do instead, and the bug that showed us we'd got it wrong.

What the obvious editor does

A visual editor can't work on raw text. It parses the file into a tree, you edit the tree, and on save it prints the tree back out as Markdown. That's where the trouble starts.

Here's a page like the ones we see in imported docs:

---
title: 'Rotate your API keys'
description: "Replace a key without downtime"
icon: key # Lucide name
sidebarTitle: Rotate keys
---

Rotate a key when someone leaves the team, or every 90 days.

<Note>
  Old keys keep working for **24 hours** after you rotate them.
</Note>

<Steps>
  <Step title="Create a new key">
    Open __Settings → API keys__ and choose *New key*.
  </Step>
  <Step title="Deploy it">
    Swap the key in your environment, then redeploy.
  </Step>
</Steps>

* Keys are shown once
* Store them in a secret manager

I ran it through remark, the standard Markdown toolkit, with a YAML library for the frontmatter. Parse, print, no edit. Here's the diff:

 ---
-title: 'Rotate your API keys'
-description: "Replace a key without downtime"
-icon: key # Lucide name
+title: Rotate your API keys
+description: Replace a key without downtime
+icon: key
 sidebarTitle: Rotate keys
 ---
@@
 <Steps>
   <Step title="Create a new key">
-    Open __Settings → API keys__ and choose *New key*.
+    Open **Settings → API keys** and choose *New key*.
   </Step>
+
   <Step title="Deploy it">
@@
-* Keys are shown once
-* Store them in a secret manager
+- Keys are shown once
+- Store them in a secret manager

Four changes and a lost comment, and nobody typed anything.

That's not remark's fault. A printer's job is to write one tidy version of whatever it's given, which is what you want from a formatter when you choose to run it. It's not what you want from an editor working on someone else's files. The tree never recorded that you used single quotes, so there's no way to print them back.

Don't print what nobody touched

So we stopped printing the parts nobody touched.

When a page opens, our parser notes two things for every block: where in the file it came from, and a fingerprint of how the block looks in the editor. On save, we fingerprint each block again. Same fingerprint means nobody touched it, so we copy its original bytes out of the file. Only the blocks that changed get printed.

That's most of the idea, and the core of it fits in a few lines:

function tryVerbatim(node, ctx) {
  const range = node.attrs?.originalRange
  const expectedHash = node.attrs?.sourceHash
  if (!range || !expectedHash) return null
  if (range.start < ctx.bodyOffset || range.end > ctx.source.length) return null

  if (hashPmNode(node) !== expectedHash) return null

  return ctx.source.slice(range.start, range.end)
}

Same page again. Save it without an edit and the diff is empty. Change "90 days" to "30 days" and you get this:

-Rotate a key when someone leaves the team, or every 90 days.
+Rotate a key when someone leaves the team, or every 30 days.

That's the diff a reviewer should see.

A few smaller rules cover the rest of the file:

  • The gaps. Whitespace between two untouched blocks is copied too, so a </Card> sitting right above a <Card> stays that way. So does the end of the file, final newline or not.
  • The frontmatter. We keep it as raw text and write it back as it was unless a value changed. Renaming a page rewrites the title: line and nothing else.
  • Unknown components. If the editor doesn't know a component, it keeps it as raw text and saves it exactly as written. A <Note> opens as a callout and saves as <Note>.

The bug that proved it

We shipped this in April. In November we found out it wasn't doing its job.

Opening a page didn't stop at parsing. The editor then cleaned the document up to fit its own schema: it added default values for attributes the file didn't set, and dropped ones it didn't know. We'd taken the fingerprints before that cleanup. By save time every block looked different from its fingerprint, so every block counted as edited and got printed fresh. The tidy-printer diff was back.

The same gap hid something worse. A page with a block type the editor didn't support loaded as an empty document, and autosave wrote that empty document over the file.

The fix was to fingerprint after the cleanup, not before. Pages now load through one function that parses the file, checks it against the editor's real schema, and only then takes fingerprints. A page that doesn't fit opens in the source editor and never autosaves. Raw MDX on screen is a much better failure than an empty page saved over a real one.

Keeping it fixed

One test page uses every block the editor supports. The test loads it in a real editor, saves it without an edit and checks that every byte matches. Every new block gets a line on that page, so it's round-tripped before it ships.

A second test generates 200 random documents per property. Saving a page three times in a row has to give the same bytes every time, and a paragraph nobody touched has to come back exactly as it was.

The test

You can run this on any tool that writes to your repository. Commit everything, open each file in the tool, save without typing, and run git diff.

If it prints anything, the tool is rewriting files nobody edited, and your history is paying for it.

Engineering4 min read

We never run your page's code

MDX lets a docs page run JavaScript. On a platform that compiles thousands of strangers' pages, we decided the safest sandbox is the one you never need.

Sahil Gadimbayli

Engineering4 min read

Your docs belong in a Git repository

A docs database is convenient until you need history, review or a way out. Why we keep every page as a file in Git, and what that asks of the editor.

Sahil Gadimbayli