DEV Community

Cover image for Building MD Doctor: a Markdown editor on a plain textarea
Muhammad Sheharyar Butt
Muhammad Sheharyar Butt

Posted on Originally published at msyb.dev AI-assisted

Building MD Doctor: a Markdown editor on a plain textarea

I write a lot of Markdown: READMEs, changelogs, release notes, every post on this blog.
MD Doctor is the editor I built for all of it. Live preview,
tables, LaTeX math, Mermaid diagrams and highlighted code, with export to Markdown, HTML or
PDF. It is free, MIT licensed and runs entirely in your browser. There is no account,
because there is no server to keep one on.

It first shipped in July. This week it got a complete redesign and the features I kept
wishing it had. This post covers both, and the one decision everything else rests on:
there is no editor engine.

Open the editor ·
Source on GitHub

The MD Doctor home page in the light theme: plain-text Markdown on the left, the rendered result on the right

No Monaco, no CodeMirror, no TipTap

The obvious way to build an editor in the browser is to install one. Monaco is the editor
inside VS Code. CodeMirror 6 is excellent. TipTap and ProseMirror are great if you want
rich text.

MD Doctor uses none of them. The writing surface is a <textarea>.

That sounds like a shortcut. It is closer to the opposite. A textarea already gets the
hard parts right, because browsers have spent twenty-five years on them: IME composition
for Chinese and Japanese input, spellcheck, mobile selection handles, screen readers and
right-to-left text. Markdown is plain text. I needed a syntax tree to render it, never
to edit it.

The part that makes this workable is undo. Every formatting action, including bold, wrap
in a link, insert a table and continue a list, goes through one function:

function insertText(ta: HTMLTextAreaElement, text: string): boolean {
  ta.focus();
  const ok = document.execCommand("insertText", false, text);
  if (!ok) {
    // Fallback: set the value directly and fire an input event.
    // ...
  }
  return ok;
}
Enter fullscreen mode Exit fullscreen mode

execCommand is deprecated on paper, but insertText into a focused textarea still
works in every engine, and it has one property nothing else gives you: the edit lands on
the browser's native undo stack. Ctrl+Z undoes a toolbar action
exactly like it undoes typing. There is no custom history to keep in sync, because there
is no custom history.

What a textarea does not give you

Three things had to be built by hand.

Line numbers that survive wrapping. A gutter is trivial until a long line wraps onto
three visual rows and every number below it drifts. The editor mirrors the content into a
hidden element with the textarea's exact font, padding and tab size, one <div> per
logical line, and reads each one's offsetHeight. The gutter rows take those heights. A
ResizeObserver re-measures when the pane changes width, and on phones, where the gutter
is hidden, the measuring is skipped entirely.

Knowing where the caret is, in pixels. Press / on an empty line and an
insert menu opens at the cursor. Select text and a format bar floats above it. A textarea
will tell you the caret's index, never its position. The fix is the classic mirror
technique: copy around thirty computed style properties onto an invisible element, fill it
with the text up to the caret, append a marker <span> and read the span's offset.

div.textContent = element.value.slice(0, position);

const span = document.createElement("span");
// Non-empty, so it occupies the caret's line box.
span.textContent = element.value.slice(position) || ".";
div.appendChild(span);

return {
  top: span.offsetTop + parseInt(computed.borderTopWidth, 10),
  left: span.offsetLeft + parseInt(computed.borderLeftWidth, 10),
};
Enter fullscreen mode Exit fullscreen mode

Getting out of the way on touch screens. On a phone, the operating system draws its
own selection handles and copy-paste callout, and my floating format bar fought it for the
same few pixels. The bar now only appears for fine pointers, behind a single
(pointer: coarse) media query. The slash menu works everywhere.

The MD Doctor editor in dark mode: Markdown source with line numbers on the left, live preview on the right

One rendering pipeline, two places

The live preview is react-markdown with GitHub Flavored Markdown, KaTeX for math,
highlight.js for code and Mermaid for diagrams. Mermaid is large, so it is imported the
first time a document actually contains a diagram, not before.

The redesign added a Markdown cheat sheet
that shows every piece of syntax MD Doctor understands, with the source beside the result.
Those examples go through the same pipeline, just on the server. A Server Component runs
react-markdown at build time, so the page ships finished HTML and none of the Markdown,
KaTeX or highlight.js code reaches the browser.

One detail that is easy to miss: a page of Markdown examples is full of # Heading 1.
Rendered literally, the cheat sheet would contain dozens of <h1> elements, which wrecks
the document outline for screen readers and search engines alike. Example headings render
as styled <div>s instead, so the page keeps exactly one <h1> and real sections.

The Markdown cheat sheet: each example shows the Markdown source next to the rendered result, with Copy and Try it buttons

Every example has a Try it button that opens the snippet in the editor. The homepage
has an Open a .md file button that does the same with a file from your disk. Both hand
the document across with sessionStorage, which the editor reads exactly once on mount.

I deliberately did not put the document in the URL. A document can be long, and URLs end
up in browser history, server logs and analytics. sessionStorage stays in that one tab
and is never sent anywhere. The file importer, which converts HTML to Markdown with
turndown, is also loaded only once a file has been chosen, so the homepage does not pay
for it.

Private by default, and asking first

Documents can be saved to IndexedDB, but only after you agree to it. Until then nothing is
written: the editor still works, you just cannot reopen the document later. "It stays on
your device" is not the same as "you expected it to be stored", and shared computers are a
real case. Clearing everything is one button.

Exports avoid canvas tricks entirely. Markdown is a plain file download. HTML is a
standalone file with its stylesheet embedded. PDF opens a clean, print-optimised window
and hands it to the browser's own Save as PDF, so the result has selectable text,
working links and proper fonts.

That PDF path also taught me something I did not know. My first version opened the print
window with "noopener" for good security hygiene. With that flag, window.open()
returns null, so the code had nothing to write into. Users got an empty tab and no PDF.
It is now a comment in the source so nobody "fixes" it back.

The redesign

The first version looked like a lot of 2026 side projects: purple gradients, glowing
cards and two trendy display fonts. It worked, but it did not look like a tool for
writing.

The new version is paper and ink. Newsreader for headings and rendered documents,
Instrument Sans for the interface, JetBrains Mono for source, and one accent colour, a
deep teal. Exported HTML uses serif headings too, so what you export looks like what you
wrote.

The less visible changes matter more:

  • Self-hosted fonts. next/font downloads them at build time, so there is no third-party request and the fallback fonts are size-adjusted to avoid layout shift. The mono and Arabic faces are not preloaded; Arabic glyphs are only fetched when a page actually contains Arabic script.
  • Theme before first paint. The colour scheme is resolved in the browser by an inline script, using your stored choice or the OS setting. No cookies are read on the server, which means every page can be statically prerendered.
  • Four languages with full RTL. English, Turkish, Arabic and Urdu. Layout uses CSS logical properties such as paddingInlineStart and borderInlineEnd, so one stylesheet serves both directions.
  • Search and answer engines. Per-page canonical URLs and hreflang, JSON-LD for the app, FAQ and articles, and an /llms.txt: a short Markdown summary of the site for AI assistants.

The marketing pages are also Server Components now. They read their copy on the server,
and the layout only sends the browser the translation namespaces that client components
actually use:

const CLIENT_NAMESPACES = ["nav", "common", "editor", "storage", "contact", "notFound"];

const clientMessages = Object.fromEntries(
  CLIENT_NAMESPACES.filter((ns) => ns in messages).map((ns) => [ns, messages[ns]]),
);
Enter fullscreen mode Exit fullscreen mode

Before this, every page shipped its language's entire message catalogue, FAQ, cheat sheet
and legal copy included, whether it rendered them or not.

Where it still has weight

I measured the live site while writing this. The homepage references about 330 KB of
gzipped JavaScript and the editor about 640 KB. The editor is where weight belongs.
Rendering math, diagrams and highlighted code live in the browser is the product. The
homepage is a different story: most of its JavaScript is the component library's runtime,
pulled in by a header with a theme toggle and a language menu. That is the next thing to
cut.

Try it

MD Doctor is at mddoctor.msyb.dev. Open the editor, press
/ on an empty line, and export something. The source is on
GitHub under the MIT licence. Issues and
pull requests are welcome, and the cheat sheet
is worth a bookmark even if you write Markdown somewhere else.

Top comments (0)