Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
utility to create xast trees
License
syntax-tree/xastscript
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
xast utility to create trees with ease.
- What is this?
- When should I use this?
- Install
- Use
- API
- Syntax tree
- JSX
- Types
- Compatibility
- Security
- Related
- Contribute
- License
This package is a hyperscript interface (likecreateElement from React andsuch) to help with creating xast trees.
You can use this utility in your project when you generate xast syntax treeswith code.It helps because it replaces most of the repetition otherwise needed in a syntaxtree with function calls.
You can instead useunist-builder when creating any unist nodes andhastscript when creating hast (HTML) nodes.
This package isESM only.In Node.js (version 16+), install withnpm:
npm install xastscript
In Deno withesm.sh:
import{x}from'https://esm.sh/xastscript@4'
In browsers withesm.sh:
<scripttype="module">import{x}from'https://esm.sh/xastscript@4?bundle'</script>
import{u}from'unist-builder'import{x}from'xastscript'// Children as an array:console.log(x('album',{id:123},[x('name','Born in the U.S.A.'),x('artist','Bruce Springsteen'),x('releasedate','1984-04-06')]))// Children as arguments:console.log(x('album',{id:123},x('name','Exile in Guyville'),x('artist','Liz Phair'),x('releasedate','1993-06-22')))// For other xast nodes, such as comments, instructions, doctypes, or cdata// can be created with unist-builder:console.log(x(null,[u('instruction',{name:'xml'},'version="1.0" encoding="UTF-8"'),x('album',[u('comment','Great album!'),x('name','Born in the U.S.A.'),x('description',[u('cdata','3 < 5 & 8 > 13')])])]))
Yields:
{type:'element',name:'album',attributes:{id:'123'},children:[{type:'element',name:'name',attributes:{},children:[{type:'text',value:'Born in the U.S.A.'}]},{type:'element',name:'artist',attributes:{},children:[{type:'text',value:'Bruce Springsteen'}]},{type:'element',name:'releasedate',attributes:{},children:[{type:'text',value:'1984-04-06'}]}]}{type:'element',name:'album',attributes:{id:'123'},children:[{type:'element',name:'name',attributes:{},children:[{type:'text',value:'Exile in Guyville'}]},{type:'element',name:'artist',attributes:{},children:[{type:'text',value:'Liz Phair'}]},{type:'element',name:'releasedate',attributes:{},children:[{type:'text',value:'1993-06-22'}]}]}{type:'root',children:[{type:'instruction',name:'xml',value:'version="1.0" encoding="UTF-8"'},{type:'element',name:'album',attributes:{},children:[{type:'comment',value:'Great album!'},{type:'element',name:'name',attributes:{},children:[{type:'text',value:'Born in the U.S.A.'}]},{type:'element',name:'description',attributes:{},children:[{type:'cdata',value:'3 < 5 & 8 > 13'}]}]}]}
This package exports the identifierx.There is no default export.
The export map supports the automatic JSX runtime.You can passxastscript to your build tool (TypeScript, Babel, SWC) with animportSource option or similar.
Createxast trees.
x(): rootx(null[, …children]): rootx(name[, attributes][, …children]): element
Qualified name (string, optional).
Case sensitive and can contain a namespace prefix (such asrdf:RDF).When string, anElement is built.When nullish, aRoot is built instead.
Attributes of the element (Attributes, optional).
Children of the node (Array<Child> orChild, optional).
Created tree (Result).
Element when aname is passed, otherwiseRoot.
Map of attributes (TypeScript type).
Nullish (null orundefined) orNaN values are ignored, other values areturned to strings.
typeAttributes=Record<string,boolean|number|string|null|undefined>
(Lists of) children (TypeScript type).
When strings or numbers are encountered, they are turned intoTextnodes.Root nodes are treated as “fragments”, meaning that their childrenare used instead.
typeChild=|Array<Node|boolean|number|string|null|undefined>|Node|boolean|number|string|null|undefined
Result from ax call (TypeScript type).
typeResult=Element|Root
The syntax tree isxast.
This package can be used with JSX.You should use the automatic JSX runtime set toxastscript.
🪦Legacy: you can also use the classic JSX runtime, but this is notrecommended.To do so, import
xyourself and define it as the pragma (plus set thefragment tonull).
The Use example above (omitting the second) can then be written like so:
/**@jsxImportSource xastscript */import{u}from'unist-builder'console.log(<albumid={123}><name>Born in the U.S.A.</name><artist>Bruce Springsteen</artist><releasedate>1984-04-06</releasedate></album>)console.log(<>{u('instruction',{name:'xml'},'version="1.0" encoding="UTF-8"')}<album>{u('comment','Great album!')}<name>Born in the U.S.A.</name><description>{u('cdata','3 < 5 & 8 > 13')}</description></album></>)
This package is fully typed withTypeScript.It exports the additional typesAttributes,Child, andResult.
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,xastscript@^4, compatiblewith Node.js 16.
XML can be a dangerous language: don’t trust user-provided data.
unist-builder— create any unist treehastscript— create a hast treexast-util-to-xml— serialize xast as XMLxast-util-from-xml— parse xast from XMLhast-util-to-xast— transform hast to xast
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.
About
utility to create xast trees
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.