Package Exports
- hast-util-to-text
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hast-util-to-text) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
hast-util-to-text
Get the plain-text value of a hast 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
Usage
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)
Get the plain-text value of a node.
- If
node
is a comment, returns itsvalue
- If
node
is a text, applies normal white-space collapsing to itsvalue
, as defined by the CSS Text spec - If
node
is a root or element, applies an algorithm similar to theinnerText
getter as defined by HTML
Parameters
node
(Node
) — Thing to stringify
Returns
string
— Stringified node
.
Notes
- If an element is given that is not displayed (like a
head
), we’ll still use theinnerText
algorithm instead of switching totextContent
- If child elements are not displayed, they will be ignored
- CSS is not taken into account, 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 are treated like normal elements
Related
hast-util-to-string
— Get the plain-text value (textContent
)hast-util-from-text
— Set the plain-text value (innerText
)hast-util-from-string
— Set the plain-text value (textContent
)
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, organisation, or community you agree to abide by its terms.