Build Coverage Downloads Size Sponsors Backers Chat

hast utility to get the plain-text value of a node.

This is like the DOMs Node#innerText getter but there are some deviations from the spec. The resulting text is returned.

You’d typically want to use hast-util-to-string (textContent), but hast-util-to-text (innerText) adds for example line breaks where <br> elements are used.


This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.


npm install hast-util-to-text


import {h} from 'hastscript'
import {toText} from 'hast-util-to-text'

const tree = h('div', [
  h('h1', {hidden: true}, 'Alpha.'),
  h('article', [
    h('p', ['Bravo', h('br'), 'charlie.']),
    h('p', 'Delta echo \t foxtrot.')




Delta echo foxtrot.


This package exports the following identifiers: toText. There is no default export.

toText(node, options?)

Utility to get the plain-text value of a node.

  • If node is a comment, returns its value
  • If node is a text, applies normal white-space collapsing to its value, as defined by the CSS Text spec
  • If node is a root or element, applies an algorithm similar to the innerText getter as defined by HTML

Default whitespace setting to use ('normal' or 'pre', default: 'normal').


string — Stringified node.

  • If node is an element that is not displayed (such as a head), we’ll still use the innerText algorithm instead of switching to textContent
  • If descendants of node are elements that are not displayed, they are ignored
  • CSS is not considered, except for the default user agent style sheet
  • A line feed is collapsed instead of ignored in cases where Fullwidth, Wide, or Halfwidth East Asian Width characters are used, the same goes for a case with Chinese, Japanese, or Yi writing systems
  • Replaced elements (such as audio) are treated like non-replaced elements


hast-util-to-text does not change the syntax tree so there are no openings for cross-site scripting (XSS) attacks.


See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.


MIT © Titus Wormer

Hast Util To Text

utility to get the plain-text value of a node according to the `innerText` algorithm

Hast Util To Text Info

⭐ Stars12
🔗 Homepageunifiedjs.com
🔗 Source Codegithub.com
🕒 Last Updatea year ago
🕒 Created4 years ago
🐞 Open Issues1
➗ Star-Issue Ratio12
😎 Authorsyntax-tree