JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1040634
  • Score
    100M100P100Q16119F
  • License MIT

Convert a HAST Node through a hyperscript interface

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 Build Status Coverage Status

Convert a HAST Node to another virtual node through a hyperscript compatible interface such as virtual dom, React.createElement, or hyperscript.

Installation

npm:

npm install hast-to-hyperscript

hast-to-hyperscript is also available as an AMD, CommonJS, and globals module, uncompressed and compressed.

Usage

Dependencies:

var toHyperscript = require('hast-to-hyperscript');
var React = require('react');
var h = require('hyperscript');
var v = require('virtual-dom/h');

HAST Tree:

var tree = {
    'type': 'element',
    'tagName': 'a',
    'properties': {
        'href': 'http://alpha.com',
        'id': 'bravo',
        'className': ['charlie', 'delta'],
        'download': true
    },
    'children': [{
        'type': 'text',
        'value': 'Echo'
    }]
};

Compiling with hyperscript:

var result = toHyperscript(tree, h).outerHTML;

Yields:

<a href="http://alpha.com" id="bravo" download="download" class="charlie delta">Echo</a>

Or with virtual-dom/h:

result = toHyperscript(tree, v);

Yields:

VirtualNode {
  tagName: 'A',
  properties: 
   { href: 'http://alpha.com',
     id: 'bravo',
     download: true,
     attributes: { class: 'charlie delta' } },
  children: [ VirtualText { text: 'Echo' } ],
  key: undefined,
  namespace: null,
  count: 1,
  hasWidgets: false,
  hasThunks: false,
  hooks: undefined,
  descendantHooks: false }

Or React.createElement:

result = toHyperscript(tree, React.createElement);

Yields:

{ '$$typeof': Symbol(react.element),
  type: 'a',
  key: null,
  ref: null,
  props: 
   { href: 'http://alpha.com',
     id: 'bravo',
     className: 'charlie delta',
     download: true,
     children: [ 'Echo' ] },
  _owner: null,
  _store: {} }

API

toHyperscript(node, h)

Convert a HAST Node to another virtual node through a hyperscript compatible interface such as virtual dom, React.createElement, or hyperscript.

Note that, although the signatures of the above mentioned “compatible” interfaces are the same, they differ in how they handle text or properties. Other “compatible” interfaces might not be “compatible”.

Parameters:

  • node (HAST Node) — Node to convert;
  • h (Function) — Hyperscript compatible interface.

Returns: Array|Object? — A node, created through invoking h(). undefined is returned if the given HAST node is neither element nor text (such as comments, character-data, or doctypes). Root nodes result in Array.<Object>.

License

MIT © Titus Wormer