Gathering detailed insights and metrics for hast-util-to-text
Gathering detailed insights and metrics for hast-util-to-text
Gathering detailed insights and metrics for hast-util-to-text
Gathering detailed insights and metrics for hast-util-to-text
utility to get the plain-text value of a node according to the `innerText` algorithm
npm install hast-util-to-text
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
19 Stars
78 Commits
2 Forks
9 Watching
1 Branches
12 Contributors
Updated on 26 May 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
1%
146,819
Compared to previous day
Last week
3.6%
866,107
Compared to previous week
Last month
10.6%
3,589,271
Compared to previous month
Last year
118.6%
30,329,491
Compared to previous year
hast utility to get the plain-text value of a node.
This package is a utility that takes a hast node and gets its plain-text
value.
It is like the DOMs Node#innerText
, which is a bit nicer than
Node#textContent
, because this turns <br>
elements into line breaks and
uses '\t'
(tabs) between table cells.
There are some small deviations from the spec, because the DOM has knowledge of
associated CSS, and can take into account that elements have display: none
or
text-transform
association with them, and this utility canβt do that.
This is a small utility that is useful when you want a plain-text version of a node that is close to how itβs βvisibleβ to users.
This utility is similar to hast-util-to-string
, which
is simpler, and more like the Node#textContent
algorithm discussed above.
There is also a package hast-util-from-text
, which sort
of does the inverse: it takes a string, sets that as text on the node, while
turning line endings into <br>
s
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install hast-util-to-text
In Deno with esm.sh
:
1import {toText} from 'https://esm.sh/hast-util-to-text@4'
In browsers with esm.sh
:
1<script type="module"> 2 import {toText} from 'https://esm.sh/hast-util-to-text@4?bundle' 3</script>
1import {h} from 'hastscript' 2import {toText} from 'hast-util-to-text' 3 4const tree = h('div', [ 5 h('h1', {hidden: true}, 'Alpha.'), 6 h('article', [ 7 h('p', ['Bravo', h('br'), 'charlie.']), 8 h('p', 'Delta echo \t foxtrot.') 9 ]) 10]) 11 12console.log(toText(tree))
Yields:
1Bravo 2charlie. 3 4Delta echo foxtrot.
This package exports the identifier toText
.
There is no default export.
toText(tree[, options])
Get the plain-text value of a node.
Serialized tree
(string
).
tree
is a comment, returns its value
tree
is a text, applies normal whitespace collapsing to its
value
, as defined by the CSS Text spectree
is a root or element, applies an algorithm similar to the
innerText
getter as defined by HTMLπ Note: the algorithm acts as if
tree
is being rendered, and as if weβre a CSS-supporting user agent, with scripting enabled.
tree
is an element that is not displayed (such as a head
), weβll
still use the innerText
algorithm instead of switching to textContent
tree
are elements that are not displayed, they are
ignoredaudio
) are treated like non-replaced elementsOptions
Configuration (TypeScript type).
whitespace
(Whitespace
, default: 'normal'
)
β default whitespace setting to useWhitespace
Valid and useful whitespace values (from CSS) (TypeScript type).
1type Whitespace = 'normal' | 'nowrap' | 'pre' | 'pre-wrap'
This package is fully typed with TypeScript.
It exports the additional types Options
and
Whitespace
.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, hast-util-to-text@^4
,
compatible with Node.js 16.
hast-util-to-text
does not change the syntax tree so there are no
openings for cross-site scripting (XSS) attacks.
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
)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
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
Found 2/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More