Wondering what’s next for npm?Check out our public roadmap! »

    hast-to-hyperscript
    TypeScript icon, indicating that this package has built-in type declarations

    10.0.0 • Public • Published

    hast-to-hyperscript

    Build Coverage Downloads Size Sponsors Backers Chat

    hast utility to transform a tree to something else through a hyperscript interface.

    Install

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

    npm:

    npm install hast-to-hyperscript

    Use

    import {toH} from 'hast-to-hyperscript'
    import h from '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

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

    toH(h, tree[, options|prefix])

    Transform a hast tree to something else through a hyperscript interface.

    Parameters
    • h (Function) — Hyperscript function
    • tree (Node) — Tree to transform
    • prefix — Treated as {prefix: prefix}
    • options.prefix (string or boolean, optional) — Prefix to use as a prefix for keys passed in attrs to h(), this behavior is turned off by passing false, turned on by passing a string. By default, h- is used as a prefix if the given h is detected as being virtual-dom/h or React.createElement
    • options.space (enum, 'svg' or 'html', default: 'html') — Whether node is in the 'html' or 'svg' space. If an svg element is found when inside the HTML space, toH automatically 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 create
    • attrs (Object.<string>) — Attributes to set
    • children (Array.<* | string>) — List of children (results of previously invoking h())
    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 element or root node is given, toH throws
    • If a root is given with no children, an empty div element is returned
    • If a root is given with one element child, that element is transformed
    • Otherwise, the children are wrapped in a div element

    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-sanitize to make the hast tree safe.

    Related

    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.

    License

    MIT © Titus Wormer

    Install

    npm i hast-to-hyperscript

    DownloadsWeekly Downloads

    1,453,808

    Version

    10.0.0

    License

    MIT

    Unpacked Size

    20.8 kB

    Total Files

    5

    Last publish

    Collaborators

    • avatar
    • avatar