Package Exports
- hast-to-hyperscript
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-to-hyperscript) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
hast-to-hyperscript
hast utility to transform a tree to something else through a hyperscript interface.
Install
npm:
npm install hast-to-hyperscriptUse
var toH = require('hast-to-hyperscript')
var h = require('hyperscript')
var tree = {
type: 'element',
tagName: 'p',
properties: {id: 'alpha', className: ['bravo']},
children: [
{type: 'text', value: 'charlie '},
{
type: 'element',
tagName: 'strong',
properties: {style: 'color: red;'},
children: [{type: 'text', value: 'delta'}]
},
{type: 'text', value: ' echo.'}
]
}
// Transform (`hyperscript` needs `outerHTML` to serialize):
var doc = toH(h, tree).outerHTML
console.log(doc)Yields:
<p class="bravo" id="alpha">charlie <strong>delta</strong> echo.</p>API
toH(h, tree[, options|prefix])
Transform a hast tree to something else through a hyperscript interface.
Parameters
h(Function) — Hyperscript functiontree(Node) — Tree to transformprefix— Treated as{prefix: prefix}options.prefix(stringorboolean, optional) — Prefix to use as a prefix for keys passed inattrstoh(), this behavior is turned off by passingfalse, turned on by passing astring. By default,h-is used as a prefix if the givenhis detected as beingvirtual-dom/horReact.createElementoptions.space(enum,'svg'or'html', default:'html') — Whethernodeis in the'html'or'svg'space. If ansvgelement is found when inside the HTML space,toHautomatically switches to the SVG space when entering the element, and switches back when exiting
Returns
* — Anything returned by invoking h().
function h(name, attrs, children)
Create an element from the given values.
Content
h is called with the node that is currently compiled as the context object
(this).
Parameters
name(string) — Tag-name of element to createattrs(Object.<string>) — Attributes to setchildren(Array.<* | string>) — List of children (results of previously invokingh())
Returns
* — Anything.
Caveats
Nodes
Most hyperscript implementations only support elements and texts. hast supports doctype, comment, and root as well.
- If anything other than an
elementorrootnode is given,toHthrows - If a root is given with no children, an empty
divelement is returned - If a root is given with one element child, that element is transformed
- Otherwise, the children are wrapped in a
divelement
If unknown nodes (a node with a type not defined by hast) are found as descendants of the given tree, they are ignored: only text and element are transformed.
Support
Although there are lots of libraries mentioning support for a hyperscript-like
interface, there are significant differences between them.
For example, hyperscript doesn’t support classes in attrs and
virtual-dom/h needs an attributes object inside attrs most of the
time.
toH works around these differences for:
Security
Use of hast-to-hyperscript can open you up to a
cross-site scripting (XSS) attack if the hast tree is unsafe.
Use hast-util-santize to make the hast tree safe.
Related
hastscript— Hyperscript compatible interface for creating nodeshast-util-sanitize— Sanitize nodeshast-util-from-dom— Transform a DOM tree to hastunist-builder— Create any unist treexastscript— Create a xast tree
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.