xastscript

4.0.0 · active · verified Sun Apr 19

xastscript is a utility library providing a hyperscript-like interface for programmatically creating xast (XML Abstract Syntax Tree) nodes. Similar to React's `createElement` or `hastscript` for HTML, it simplifies the creation of XML syntax trees by allowing nested function calls instead of verbose object literals. The current stable version is 4.0.0, which requires Node.js 16 or later and is exclusively ESM. It's part of the unified ecosystem and is primarily used when generating XML structures within code. Unlike `unist-builder` which handles generic unist nodes, `xastscript` is specifically tailored for XML elements, providing convenience for defining attributes and children in a more declarative way. Its release cadence typically involves minor and patch updates every few months, with major versions occurring less frequently, driven by breaking changes or significant feature additions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating xast XML elements with attributes and children using both array and argument-based child passing, and integrating with `unist-builder` for non-element xast nodes like comments and processing instructions.

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: 456},
    x('name', 'Exile in Guyville'),
    x('artist', 'Liz Phair'),
    x('releasedate', '1993-06-22')
  )
);

// Combining with unist-builder for other xast nodes (comments, instructions, cdata):
console.log(
  x(null, [
    u('instruction', {name: 'xml'}, 'version="1.0" encoding="UTF-8"'),
    x('album', [
      u('comment', 'A fantastic rock album!'),
      x('name', 'Born in the U.S.A.'),
      x('description', [u('cdata', '3 < 5 & 8 > 13')])
    ])
  ])
);

view raw JSON →