Plain Text Output

The editor stores HTML, but getPlainText()returns just the visible text — formatting dropped, block structure surviving as line breaks. That is what you want for search indexing, previews, notification snippets, and any count a user would recognise.

Quarterly summary

Revenue grew 18% against a flat headcount.

  • EMEA ahead of plan
  • APAC recovering
Formatting disappears; the reading order does not.

getPlainText()

0 words · 0 characters — counted on the text, not the markup (0 characters of HTML).

Loading…

Example code

// Visible text, with block structure surviving as line breaks.
const text = editor.getPlainText();     // editor.getText() is an alias

// Character and word counts should measure TEXT, not markup:
const characters = text.length;
const words = text.trim() ? text.trim().split(/\s+/).length : 0;

// And the reverse — replace the document with plain text:
editor.setPlainText("Line one\nLine two");

Counting getHTMLCode().length instead is the common mistake: a short sentence wrapped in a few tags reads as hundreds of characters, and the number a user sees stops matching the one you enforce.

Why not just read the text content? Because textContent concatenates without regard for structure: three list items come back as AlphaBravoCharlie, and a search index then tokenises the last word of one item and the first of the next as a single term. getPlainText() is layout-aware, so the same list comes back as Alpha\nBravo\nCharlie— blocks separated by a blank line, list items by a single one.

getPlainText() reference