unist-builder

unist utility to create a new trees with a nice syntax

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
unist-builder
5323.0.02 years ago7 years agoMinified + gzip package size for unist-builder in KB

Readme

unist-builder
!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
unistunist utility to create a new trees with hyperscript-like syntax.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.
npm install unist-builder
## Use

```js
import {u} from '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'),
    u('void', {id: 4})
  ])
])

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'},
        {type: 'void', id: 4}
      ]
    }
  ]
}

API

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

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 literal
  • u(type[, props]) — create a void node
Parameters
  • type (string) — node type
  • props (Object) — other values assigned to node
  • children (Array.<Node>node) — children of node
  • value (*) — value of node (cast to string)
Returns
Nodenode.

Related

— Convert unist trees to `unist-builder` notation
— Create [hast][] elements
— Create [xast][] elements

Contribute

See contributing.md in syntax-tree/.githubcontributing for ways to get started. See support.mdsupport for ways to get help.
This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MITlicense © Eugene Sharygin