unified

Project:syntax-tree/hast-util-to-text

Package:hast-util-to-text@2.0.0

  1. Dependencies:3·Dependents:4
  2. hast utility to get the plain-text value of a node according to the innerText algorithm
  1. util 105
  2. utility 105
  3. html 100
  4. unist 90
  5. hast 51
  6. hast-util 40
  7. text 13
  8. content 9
  9. string 4
  10. innertext 2

hast-util-to-text

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.

Install

npm:

npm install hast-util-to-text

Use

var h = require('hastscript')
var toText = require('hast-util-to-text')

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

console.log(toText(tree))

Yields:

Bravo
charlie.

Delta echo foxtrot.

API

toText(node)

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

Parameters
Returns

string — Stringified node.

Notes

Security

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

Contribute

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.

License

MIT © Titus Wormer