Use html-to-text to Extract Text from HTML

A practical review of the html-to-text library: a basic example, the options that shape output, what the conversion changes, and when the plain-text result stays readable and usable.

html-to-text is a Node.js library that turns an HTML string into plain text. It removes tags, decodes entities, and maps structural elements such as headings, paragraphs, lists, links and tables to text conventions like blank lines, bullet prefixes and bracketed link targets. This review walks through a working example, explains what the conversion does to your content, and covers where the output holds up and where it does not. No independent test results or measurements were available for this review. Behavior described here reflects the library's documented design, and option names and defaults can differ between major versions, so check them against the documentation for the version you install.

Is html-to-text suitable for your use?

html-to-text is a reasonable choice when you need readable plain text from HTML you already have as a string and you want control over how each element becomes text. Its main benefit is per-element configuration: you can skip navigation, drop link targets, keep or flatten tables, and turn line wrapping on or off. The main tradeoff is that it reads markup, not a rendered page. It does not apply CSS or run scripts, so its output can differ from what a browser shows.

  • Good fits:
  • Plain-text alternatives for HTML emails
  • Text for search indexing, previews, snippets or summaries
  • Cleaning HTML before passing it to text-only pipelines such as logs, diffing or language processing
  • Converting content you control, where the markup is predictable

Keep the HTML when layout, emphasis, embedded media or link destinations carry meaning that readers need. That includes formatted documentation, tables that depend on merged cells, and anything a screen reader user would navigate by headings or landmarks. If you need exactly the text a browser displays, including the effects of CSS and JavaScript, a headless browser that reads rendered text is a better match. Plain text is the better output when the destination cannot render HTML, when markup would be noise (as in search indexes and text-only email clients), or when the text will be edited or reflowed by someone else.

A basic html-to-text example

Install the package from npm, then call its convert function with an HTML string and an optional options object. Recent versions export convert. Older releases used a different function name, which is another reason to check the documentation for your version.

npm install html-to-text
const { convert } = require('html-to-text');
// or, in an ES module: import { convert } from 'html-to-text';

const html = `
<h1>Release notes</h1>
<p>Version two adds <strong>batch export</strong> and fixes &amp; escaping.</p>
<ul>
  <li>Faster import</li>
  <li>New settings page</li>
</ul>
`;

const text = convert(html, { wordwrap: 80 });
console.log(text);

Under the documented defaults, you should expect something close to the output below. Heading text is uppercased, inline emphasis is dropped, the entity is decoded to a literal ampersand, and list items get a bullet prefix. This is illustrative and was not produced by a recorded test run. Exact spacing and prefixes depend on your version and settings.

RELEASE NOTES

Version two adds batch export and fixes & escaping.

 * Faster import
 * New settings page

Shaping the output with options

Most practical work with html-to-text happens in the options. The snippet below shows a configuration aimed at text that people will read or paste elsewhere, not text bound for a fixed-width terminal.

const text = convert(html, {
  wordwrap: false,
  baseElements: { selectors: ['article'] },
  selectors: [
    { selector: 'h1', options: { uppercase: false } },
    { selector: 'a', options: { ignoreHref: true } },
    { selector: 'img', format: 'skip' },
    { selector: 'nav', format: 'skip' },
    { selector: 'table', format: 'dataTable' }
  ]
});
  • wordwrap: sets the column at which lines are hard-wrapped. false disables wrapping, so the destination can reflow the text.
  • baseElements: limits extraction to the matched part of the page, such as the main article, so headers and footers stay out of the result.
  • uppercase on headings: turning it off keeps the original capitalization, which is easier to read and less likely to be misread as acronyms.
  • ignoreHref on links: by default, link text is followed by its target in square brackets. Ignoring the target gives cleaner prose but loses the destination.
  • format 'skip': drops matched elements entirely, which is useful for navigation, images or boilerplate.
  • format 'dataTable': lays out a table as aligned columns. Without it, table content may be flattened into running text.

What the conversion actually changes

html-to-text changes the stored characters, not just how they look. The output is a new string in which tags are gone, entities are replaced by the characters they represent, and structure is expressed through newlines, spaces, prefixes and, optionally, case changes. It does not reverse or mirror text, and it does not swap letters for decorative Unicode look-alikes. The words in the output are the same characters as in the source. In this context, 'extract' means stripping markup and mapping structure. It does not mean altering the text itself.

Because the library works from markup, CSS presentation is not applied. Content hidden with a style rule can still appear in the output, and text whose order or case comes from CSS will appear in source order and source case. If your HTML relies on CSS to hide, reorder or restyle text, add skip selectors or clean the markup first. Also check whether script, style and other non-content elements are excluded in your version, and skip them explicitly if they are not.

Semantic information is reduced to visual conventions. A heading becomes a line of text with surrounding blank lines, a list becomes prefixed lines, and a link becomes text with an optional bracketed target. Anyone reading the result can usually infer the structure, but software can no longer tell a heading from an ordinary line.

Bidirectional text needs a separate check. HTML can set text direction with the dir attribute or with bdi and bdo elements. Once the markup is removed, that direction information is gone. The library is not documented as inserting Unicode directional control characters in its place. Mixed right-to-left and left-to-right content may therefore display in a different order after conversion. If your content includes Arabic, Hebrew or similar scripts alongside Latin text, test the output in the actual destination.

Will the result stay readable and usable after copying or sharing?

Hard line wrapping is the most common copy-and-paste problem. With wrapping on, the library inserts real newline characters at the wrap column. When that text is pasted into an editor, a form field or a chat message, those breaks stay put, and you get short ragged lines that no longer reflow. If the text will be read anywhere other than a fixed-width display, set wordwrap to false and let the destination handle line length.

For accessibility, plain text loses the heading levels, list semantics, table headers and link roles that assistive technology uses for navigation. A screen reader can still read the words, but the user cannot jump between headings or move through a table by cell. Uppercased headings can also be harder to read, and screen readers may pronounce them differently from mixed-case text. No assistive-technology testing was available for this review, so treat these as general considerations rather than verified behavior for any particular screen reader. Wherever the HTML is available, offer it as the primary accessible version and treat the plain text as a fallback.

For search and editing, the output is ordinary text. Search and find work on the real words, and the text can be edited with any tool. Watch for two side effects. Bracketed link targets add strings that will be indexed and matched. Uppercased headings change the case of the stored text, which matters for case-sensitive searches or comparisons.

For complex characters, wrapping breaks lines at whitespace, so words containing combining marks or multi-code-point emoji are normally kept whole. The library can also be configured to force-split words longer than the wrap width. If you enable that, test with accented text and emoji sequences, because splitting on characters can separate a base letter from its combining mark or break an emoji sequence. Turning wrapping off avoids the issue entirely.

Gaps in this review

  • No conversion was run and recorded for this review, so the sample output is illustrative and based on documented defaults.
  • No performance, memory or large-document measurements are available.
  • Default settings and option names vary between major versions and were not checked against a specific release.
  • Handling of script, style and hidden elements, and of bidirectional text, should be confirmed with your own inputs.
  • No screen reader or destination-specific paste tests were performed.