Coder Social home page Coder Social logo

hast-to-hyperscript's Introduction

hast-to-hyperscript Build Status Coverage Status

Transform HAST to something else through a hyperscript DSL.

Installation

npm:

npm install hast-to-hyperscript

Usage

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 stringify):
var doc = toH(h, tree).outerHTML;

Yields:

<p class="bravo" id="alpha">charlie <strong>delta</strong> echo.</p>

API

toH(h, node[, prefix])

Transform HAST to something else through a hyperscript DSL.

Parameters
  • h (Function);
  • node (Element);
  • prefix (string or boolean, optional) — Prefix to use as a prefix for keys passed in attrs to h(), this behaviour 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.
Returns

* — Anything returned by invoking h().

function h(name, attrs, children)

Transform HAST to something else through a hyperscript DSL.

Parameters
  • name (string) — Tag-name of element to create.
  • attrs (Object.<string>) — Attributes to set.
  • children (Array.<* | string>) — List of children and text, where children are the result of invoking h() previously.
Returns

* — Anything.

Caveats

Although there are lots of libs mentioning support for this interface, there are significant differences between them. For example, hyperscript doesn’t support classes in attrs, virtual-dom/h needs an attributes object inside attrs most of the time. hast-to-hyperscript works around these differences for:

Related

License

MIT © Titus Wormer

hast-to-hyperscript's People

Contributors

wooorm avatar greenkeeperio-bot avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.