Home / Forms and Input / Mini Rich Text Editor

Mini Rich Text Editor in JavaScript, Free with Live Demo

Free mini rich text editor in plain JavaScript. Bold, italic, headings, lists, quotes and links, keyboard shortcuts, clean paste, word count, autosave and HTML or Markdown export.

Open live demoDownload HTML fileView code on GitHub
Mini Rich Text Editor JavaScript project: a small editor with bold, headings, lists and links that exports clean HTML and Markdown

Runs on: contenteditable and Selection API. Works in all modern browsers. execCommand is marked as old in the specs but is still supported everywhere.

What is the Mini Rich Text Editor?

A small rich text editor with a toolbar for bold, italic, underline, headings, quotes, lists and links, plus the usual keyboard shortcuts.

Text pasted from web pages or Word is cleaned to simple HTML. The editor saves as you type, counts words and exports clean HTML or Markdown.

Good for

  • Comment and message boxes
  • Simple blog or news admin
  • Product descriptions in a shop admin
  • Notes and knowledge base tools

What this project does

How it works

  1. A div you can type incontenteditable turns a normal div into an editor. The toolbar runs editing commands like bold and formatBlock on the current selection.
  2. Clean every pastePasted HTML is parsed with DOMParser, and only safe tags like p, b, a and lists are kept. Styles, classes and scripts are removed.
  3. Export two waysThe cleaned HTML is shown as is, and a small walker turns the same tree into Markdown.

The key JavaScript

This is the heart of the project. The full file has the rest, including the screen layout and error handling.

editor.addEventListener("paste", (e) => {
  e.preventDefault();
  const html = e.clipboardData.getData("text/html");
  const doc = new DOMParser().parseFromString(html, "text/html");
  doc.querySelectorAll("*").forEach((el) => {
    if (!ALLOWED.has(el.tagName)) el.replaceWith(...el.childNodes);
    else [...el.attributes].forEach((a) => a.name !== "href" && el.removeAttribute(a.name));
  });
  document.execCommand("insertHTML", false, doc.body.innerHTML);
});

How to use it

  1. Click Download HTML file above.
  2. Open the file in a code editor, like VS Code.
  3. Run it from a local server with npx serve . so the camera, microphone and AI features are allowed.
  4. Change the text and colors, then upload it to GitHub Pages, Netlify or your own site. It is one file with no build step.

Questions people ask

Is execCommand safe to use?

It is still supported in every browser. For a very large editor you may want a dedicated library, but for simple formatting it works well.

Can people paste harmful code?

Pasted content is cleaned to a short list of safe tags, and link addresses must start with http, https or mailto. Clean it again on your server.

Where is the text saved?

In localStorage in your browser, half a second after you stop typing.

More Forms and Input projects