Black Friday: 50% off everythingBlack Friday: 50% off every plan and credit packEnds in --d --h --m
Dashboard

How to Add a Rich Text Editor to an AI App

The storage format decides whether you can change editors later, whether search works, and whether you just opened a stored cross-site scripting hole.

Steve Jefferson
Steve Jefferson
Developer Advocate
8 October 20265 min readMake us preferred on Google

Before you ask AI to add a rich text editor to your app, decide how the text will be stored. That one decision determines whether you can change editors in a year, whether your search works, and whether you have just opened a cross-site scripting hole. AI will happily skip it and give you a working toolbar in ninety seconds, which is the problem.

Pick a storage format first

A rich text editor has to persist formatting somehow. There are three realistic choices and they are not equivalent.

Format

Stored as

Good at

Bad at

HTML string

<p>Hello <b>world</b></p>

Rendering anywhere, simple to start

Sanitising, querying, migrating, diffing

Structured JSON

A document tree of typed nodes

Validation, migration, programmatic edits

Needs a renderer, more code upfront

Markdown

Hello **world**

Portable, diffable, human-readable

Tables, embeds, anything beyond basic formatting

For most apps, structured JSON is the right answer. It is what ProseMirror and the editors built on it use, and the reason is that a typed document tree can be validated before it is stored. You can reject a node type you do not support rather than discovering it when the page renders.

Markdown is the right answer when the content is genuinely simple and users may want it elsewhere. HTML strings are the right answer almost never, but they are what an AI agent produces by default because the fastest path from "add an editor" to "it works" goes through innerHTML.

Tell the model the format before it starts:

Add a rich text editor to the notes feature.
Store content as ProseMirror-style JSON in a jsonb column, not as an HTML string.
Allowed nodes: paragraph, heading (levels 2 and 3), bulletList, orderedList,
listItem, codeBlock, blockquote. Allowed marks: bold, italic, code, link.
Reject anything else on the server before insert.

That paragraph is the difference between a feature you can maintain and one you rewrite next year.

Sanitise on the server, always

This is the step AI skips most reliably, and it is the one that matters.

If you store HTML and render it with something like dangerouslySetInnerHTML, any user who can save content can run JavaScript in another user's browser. That is stored cross-site scripting, and in a multi-user app it is a serious bug, not a hardening task for later.

Two rules:

  1. Sanitise on the server, on the way in. Client-side sanitising is a user experience nicety. Anyone can post directly to your API and skip it entirely.

  2. Allowlist, never blocklist. Enumerate the node types, marks and attributes you permit and drop everything else. Blocklists lose, because there is always another vector.

With structured JSON, sanitising is pleasant: walk the tree, drop unknown node types, drop unknown marks, and validate that every link href starts with https://, http:// or /. A javascript: URL in a link mark is the one that gets missed, because it survives a naive node-type check.

js
const ALLOWED_NODES = new Set(['doc','paragraph','heading','bulletList',
  'orderedList','listItem','codeBlock','blockquote','text']);
const ALLOWED_MARKS = new Set(['bold','italic','code','link']);

function clean(node) {
  if (!ALLOWED_NODES.has(node.type)) return null;
  if (node.marks) {
    node.marks = node.marks.filter(m => {
      if (!ALLOWED_MARKS.has(m.type)) return false;
      if (m.type === 'link') return /^(https?:\/\/|\/)/.test(m.attrs?.href ?? '');
      return true;
    });
  }
  if (node.content) node.content = node.content.map(clean).filter(Boolean);
  return node;
}

The four things that break later

Once it works, these are what go wrong in order of how often.

Paste. Users paste from word processors, and that paste carries fonts, colours, nested tables and inline styles. If you have not told the editor how to handle pasted content, the first real user will paste a document and break the layout. Configure paste to strip to your allowed node set.

Images. An editor that accepts images will accept them as base64 data URLs by default, which means a 4MB photo lands inside your database row. Route images through your normal upload path and store a reference instead. Our guide on adding file uploads covers that path.

Search. Structured JSON is not searchable with a LIKE query. Store a plain-text copy of the content in a separate column at save time and index that. It is a few lines and it saves a painful retrofit.

Concurrent editing. Two people in the same document is a different and much larger problem. If you do not need it, make sure your save does not silently overwrite the other person's work.

How to add a rich text editor, in order

  1. Add the column as jsonb, plus a content_text column for search.

  2. Wire the editor with a minimal node set: paragraphs, bold, italic, links, lists. Ship that.

  3. Add server-side validation and sanitising before you let anyone but yourself write to it.

  4. Add the plain-text extraction on save.

  5. Only then add headings, code blocks, quotes and images, one at a time.

Resist the urge to let the model add every feature at once. A toolbar with fourteen buttons generated in one pass is fourteen untested paths through your sanitiser.

The storage decision here is the same class of choice as planning your data model before building. If users will edit the same document repeatedly, version history pairs naturally with structured JSON. For user-generated content that other people will see, content moderation is the next piece, and checking whether your app exposes user data is worth running once the editor is live. The wider walkthrough is how to build an app with AI.

Questions people ask

Should I store rich text as HTML or JSON?

JSON for most apps. It can be validated before storage and migrated later. HTML strings are simplest to start and hardest to secure or change.

Do I need to sanitise if only my team can write content?

Yes. Internal tools get exported, embedded and eventually opened up, and an unsanitised store is a bug waiting for the day the audience widens.

How do I make rich text searchable?

Extract a plain-text version at save time into its own column and index that. Do not try to query the document tree directly.

Why does pasted content break my layout?

Because word processors paste inline styles and nested markup. Configure the editor to strip pasted content down to your allowed nodes rather than accepting it as-is.

Can AI build this correctly on its own?

It will build something that works. It will usually store HTML, render it unsanitised, and inline images as base64. Specify the storage format, the allowed nodes and the sanitiser in the prompt and the output changes completely.

How did this land?

About the author

Steve Jefferson
Steve Jefferson

Developer Advocate

Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.

Share

Get the next post in your inbox

One email a month. Product updates, engineering posts, and the best of Built with Swarmz.

I agree to receive emails about AI building tips and Swarmz product news. Unsubscribe any time.