A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/syntax-tree/xastscript below:

syntax-tree/xastscript: utility to create xast trees

xast utility to create trees with ease.

This package is a hyperscript interface (like createElement from React and such) to help with creating xast trees.

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

You can instead use unist-builder when creating any unist nodes and hastscript when creating hast (HTML) nodes.

This package is ESM only. In Node.js (version 16+), install with npm:

In Deno with esm.sh:

import {x} from 'https://esm.sh/xastscript@4'

In browsers with esm.sh:

<script type="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 identifier x. There is no default export.

The export map supports the automatic JSX runtime. You can pass xastscript to your build tool (TypeScript, Babel, SWC) with an importSource option or similar.

x(name?[, attributes][, …children])

Create xast trees.

Qualified name (string, optional).

Case sensitive and can contain a namespace prefix (such as rdf:RDF). When string, an Element is built. When nullish, a Root is built instead.

Attributes of the element (Attributes, optional).

Children of the node (Array<Child> or Child, optional).

Created tree (Result).

Element when a name is passed, otherwise Root.

Map of attributes (TypeScript type).

Nullish (null or undefined) or NaN values are ignored, other values are turned to strings.

type Attributes = Record<string, boolean | number | string | null | undefined>

(Lists of) children (TypeScript type).

When strings or numbers are encountered, they are turned into Text nodes. Root nodes are treated as “fragments”, meaning that their children are used instead.

type Child =
  | Array<Node | boolean | number | string | null | undefined>
  | Node
  | boolean
  | number
  | string
  | null
  | undefined

Result from a x call (TypeScript type).

type Result = Element | Root

The syntax tree is xast.

This package can be used with JSX. You should use the automatic JSX runtime set to xastscript.

🪦 Legacy: you can also use the classic JSX runtime, but this is not recommended. To do so, import x yourself and define it as the pragma (plus set the fragment to null).

The Use example above (omitting the second) can then be written like so:

/** @jsxImportSource xastscript */

import {u} from 'unist-builder'

console.log(
  <album id={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 with TypeScript. It exports the additional types Attributes, Child, and Result.

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

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

XML can be a dangerous language: don’t trust user-provided data.

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

MIT © Titus Wormer


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4