Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

unist-builder

1.0.3 • Public • Published

unist-builder Build Status Coverage Status

Helper for creating unist trees with hyperscript-like syntax.

Installation

npm:

npm install unist-builder

Usage

var u = require('unist-builder')
 
var tree = u('root', [
  u('subtree', {id: 1}),
  u('subtree', {id: 2}, [
    u('node', [u('leaf', 'leaf-1'), u('leaf', 'leaf-2')]),
    u('leaf', {id: 3}, 'leaf-3')
  ])
])
 
console.dir(tree, {depth: null})

results in the following tree:

{
  type: 'root',
  children: [
    {type: 'subtree', id: 1},
    {
      type: 'subtree',
      id: 2,
      children: [
        {
          type: 'node',
          children: [
            {type: 'leaf', value: 'leaf-1'},
            {type: 'leaf', value: 'leaf-2'}
          ]
        },
        {type: 'leaf', id: 3, value: 'leaf-3'}
      ]
    }
  ]
}

API

u(type[, props][, children|value])

Creates a node from props, children, and optionally value.

Signatures
  • u(type[, props], children) — create a parent
  • u(type[, props], value) — create a text
  • u(type[, props]) — create a void node
Parameters
  • type (string) — node type
  • props (Object) — other values assigned to node
  • children (Array.<Node>) — children of node
  • value (*) — value of node (cast to string)
Returns

Node.

Related

Contribute

See contributing.md in syntax-tree/unist for ways to get started.

This organisation has a Code of Conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MIT © Eugene Sharygin

install

npm i unist-builder

Downloadsweekly downloads

157,681

version

1.0.3

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar
Report a vulnerability