Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

utility to create a new trees with a nice syntax

License

NotificationsYou must be signed in to change notification settings

syntax-tree/unist-builder

Repository files navigation

BuildCoverageDownloadsSizeSponsorsBackersChat

unist utility to create trees with ease.

Contents

What is this?

This package is a hyperscript interface (likecreateElement from React andh from Vue and such) to help with creating unist trees.

When should I use this?

You can use this utility in your project when you generate syntax trees withcode.It helps because it replaces most of the repetition otherwise needed in a syntaxtree with function calls.

You can instead usehastscript orxastscriptwhen creating hast (HTML) or xast (XML) nodes.

Install

This package isESM only.In Node.js (version 16+), install withnpm:

npm install unist-builder

In Deno withesm.sh:

import{u}from'https://esm.sh/unist-builder@4'

In browsers withesm.sh:

<scripttype="module">import{u}from'https://esm.sh/unist-builder@4?bundle'</script>

Use

import{u}from'unist-builder'consttree=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:undefined})

…yields:

{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 identifieru.There is no default export.

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

Build a node.

Signatures
  • u(type[, props], children) — create a parent (Parent)
  • u(type[, props], value) — create a literal (Literal)
  • u(type[, props]) — create a void node (neither parent not literal)
Parameters
  • type (string)— node type
  • props (Record<string, unknown>)— fields assigned to node
  • children (Array<Node>)— children of node
  • value (*)— value ofnode (cast to string)
Returns

Built node (Node).

ChildrenOrValue

List to use aschildren or value to use asvalue (TypeScript type).

Type
typeChildrenOrValue=Array<Node>|string

Props

Other fields to add to the node (TypeScript type).

Type
exporttypeProps=Record<string,unknown>

Types

This package is fully typed withTypeScript.It exports the additional typesChildrenOrValue andProps.

Compatibility

Projects maintained by the unified collective are compatible with maintainedversions of Node.js.

When we cut a new major release, we drop support for unmaintained versions ofNode.This means we try to keep the current release line,unist-builder@^4,compatible with Node.js 16.

Related

Contribute

Seecontributing.md insyntax-tree/.github forways to get started.Seesupport.md for ways to get help.

This project has acode of conduct.By interacting with this repository, organization, or community you agree toabide by its terms.

License

MIT © Eugene Sharygin


[8]ページ先頭

©2009-2025 Movatter.jp