3.0.1 • Published 1 month ago

hast-util-to-string v3.0.1

Weekly downloads
129,141
License
MIT
Repository
github
Last release
1 month ago

hast-util-to-string

Build Coverage Downloads Size Sponsors Backers Chat

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

Contents

What is this?

This package is a utility to get the plain-text value of a node.

When should I use this?

You can use this package when you want to get the plain text value of a node. The algorithm used by this package is like the DOMs Node#textContent getter.

To use the DOMs Node#innerText algorithm instead, use hast-util-to-text. innerText is aware of how things are displayed, for example turning hard breaks (<br>) into line endings.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install hast-util-to-string

In Deno with esm.sh:

import {toString} from 'https://esm.sh/hast-util-to-string@3'

In browsers with esm.sh:

<script type="module">
  import {toString} from 'https://esm.sh/hast-util-to-string@3?bundle'
</script>

Use

import {h} from 'hastscript'
import {toString} from 'hast-util-to-string'

toString(h('p', 'Alpha'))
//=> 'Alpha'
toString(h('div', [h('b', 'Bold'), ' and ', h('i', 'italic'), '.']))
//=> 'Bold and italic.'

API

This package exports the identifier toString. There is no default export.

toString(node)

Get the plain-text value of a node.

Parameters
  • node (Node) — node to serialize
Returns

Serialized node (string).

Syntax

HTML is parsed according to WHATWG HTML (the living standard), which is also followed by all browsers.

Syntax tree

The syntax tree used is hast.

Types

This package is fully typed with TypeScript.

Compatibility

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-string@^3, compatible with Node.js 16.

Security

As rehype works on HTML and improper use of HTML can open you up to a cross-site scripting (XSS) attack, use of rehype can also be unsafe. Use rehype-sanitize to make the tree safe.

Contribute

See contributing.md in rehypejs/.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

@vimdotmd/markdowndokztestdokz-docs-3@infinitebrahmanuniverse/nolb-hast@tinnedtea/rehype-slug@knapsack/app-ui@everything-registry/sub-chunk-1829shikiji-rehypeseiunsodou_mdcrehype-tree-sitter-highlightrehype-twoslashrehype-pretty-coderehype-prism-2rehype-prism-diffrehype-prism-enhancedrehype-prism-plusrehype-prism-templaterehype-prism-with-pluginsrehype-pseudoremark-mdx-code-metarehype-concat-css-stylerehype-concat-javascriptrehype-docmrehype-doczrehype-color-chipsrehype-custom-coderehype-calloutrehype-inline-cssrehype-minify-json-scriptrehype-mdx-headingsrehype-mdx-titlerehype-minify-css-stylerehype-minify-javascript-scriptrehype-katex-svelterehype-laserbeamstarlight-obsidianrehype-heading-anchorrehype-extract-excerptrehype-extract-metarehype-flowchartrehype-shikirehype-shiki-reloadedrehype-shikijirehype-shikixrehype-slugrehype-slug-anchor-sectionizerehype-slug-custom-idrehype-slugifyrehype-starry-nightrehype-starry-night-copy-collapserehype-plugin-umivrehype-remove-unused-cssvitdocvpressifyvite-plugin-react-mdocqjc2-blockskedush-blog-tearcw-plugin@contentedjs/contented-pipeline-mdmilo-parse-markdownmkcp-solarpunklaserbeamkulfonhast-util-selecthast-util-to-nlcstlaurosilvacom-prismglobal_components_assignmentmakyomark-appliermarkdown-doc-loadermdx-loadermdx-prismmdx-prism-2madwizardpowerthesaurus-apipressifynext-ignitereact-display-windowreact-code-markdown-loadernuxt-mdc-edgequartz-laf-jsreact-md-doc-loaderumi-plugin-father-documi-plugin-dumiumijs-preset-dumi-forktranslate-markdown@aserto/dokz@astrojs/starlight@autoanki/sync@adobe/helix-html-pipeline@amalgamated-hams/latwy-processor@birthdayresearch/contented-pipeline-md@bitmachina/highlighter@acdh-oeaw/mdx-lib@code-blocks/rehype-transform@code-blocks/rehype-wrapper@codingdud/coolicons@delicloud/umi-plugin-md@control.systems/markitzandoc-loader
3.0.1

1 month ago

3.0.0

1 year ago

2.0.0

3 years ago

1.0.4

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

7 years ago

1.0.0

8 years ago