hast-util-from-string

hast utility to set the plain-text value of a node

Usage no npm install needed!

<script type="module">
  import hastUtilFromString from 'https://cdn.skypack.dev/hast-util-from-string';
</script>

README

hast-util-from-string

Build Coverage Downloads Size Sponsors Backers Chat

Set the plain-text value of a hast node. This is like the DOMs Node#textContent setter. The given node is returned.

Install

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

npm:

npm install hast-util-from-string

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

Use

import {h} from 'hastscript'
import {fromString} from 'hast-util-from-string'

fromString(h('p'), 'Alpha')
// { type: 'element',
//   tagName: 'p',
//   properties: {},
//   children: [ { type: 'text', value: 'Alpha' } ] }
fromString(h('div', [h('b', 'Bold'), ' and ', h('i', 'italic'), '.']), 'Charlie')
// { type: 'element',
//   tagName: 'div',
//   properties: {},
//   children: [ { type: 'text', value: 'Charlie' } ] }

API

fromString(node[, value])

If node is a text node (has a value property), set that to the given value or an empty string. If node is a parent node (has children), replace them with a text node whose data is set to the given value, or if value is not given, remove all its children.

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