{"id":13716954,"url":"https://github.com/syntax-tree/xastscript","last_synced_at":"2025-06-29T22:03:41.825Z","repository":{"id":43396600,"uuid":"231242302","full_name":"syntax-tree/xastscript","owner":"syntax-tree","description":"utility to create xast trees","archived":false,"fork":false,"pushed_at":"2023-07-31T10:33:40.000Z","size":128,"stargazers_count":12,"open_issues_count":0,"forks_count":3,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-05-23T14:19:53.432Z","etag":null,"topics":["syntax-tree","unist","util","xast","xast-util","xml"],"latest_commit_sha":null,"homepage":"https://unifiedjs.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syntax-tree.png","metadata":{"funding":{"github":"unifiedjs","open_collective":"unified"},"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-01-01T17:06:40.000Z","updated_at":"2024-10-03T14:54:20.000Z","dependencies_parsed_at":"2024-01-14T22:03:39.564Z","dependency_job_id":"65a7b68b-8ab7-4dd1-833b-718764e4b54d","html_url":"https://github.com/syntax-tree/xastscript","commit_stats":{"total_commits":64,"total_committers":5,"mean_commits":12.8,"dds":0.140625,"last_synced_commit":"22c43df451961c513764a3d744bf45f6b4bb753a"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/syntax-tree/xastscript","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fxastscript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fxastscript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fxastscript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fxastscript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/xastscript/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fxastscript/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262123567,"owners_count":23262620,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["syntax-tree","unist","util","xast","xast-util","xml"],"created_at":"2024-08-03T00:01:15.999Z","updated_at":"2025-06-29T22:03:41.775Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","readme":"# xastscript\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n[![Sponsors][sponsors-badge]][collective]\n[![Backers][backers-badge]][collective]\n[![Chat][chat-badge]][chat]\n\n[xast][] utility to create trees with ease.\n\n## Contents\n\n*   [What is this?](#what-is-this)\n*   [When should I use this?](#when-should-i-use-this)\n*   [Install](#install)\n*   [Use](#use)\n*   [API](#api)\n    *   [`x(name?[, attributes][, …children])`](#xname-attributes-children)\n    *   [`Attributes`](#attributes-1)\n    *   [`Child`](#child)\n    *   [`Result`](#result)\n*   [Syntax tree](#syntax-tree)\n*   [JSX](#jsx)\n*   [Types](#types)\n*   [Compatibility](#compatibility)\n*   [Security](#security)\n*   [Related](#related)\n*   [Contribute](#contribute)\n*   [License](#license)\n\n## What is this?\n\nThis package is a hyperscript interface (like `createElement` from React and\nsuch) to help with creating xast trees.\n\n## When should I use this?\n\nYou can use this utility in your project when you generate xast syntax trees\nwith code.\nIt helps because it replaces most of the repetition otherwise needed in a syntax\ntree with function calls.\n\nYou can instead use [`unist-builder`][u] when creating any unist nodes and\n[`hastscript`][h] when creating hast (HTML) nodes.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install xastscript\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {x} from 'https://esm.sh/xastscript@4'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {x} from 'https://esm.sh/xastscript@4?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\n```js\nimport {u} from 'unist-builder'\nimport {x} from 'xastscript'\n\n// Children as an array:\nconsole.log(\n  x('album', {id: 123}, [\n    x('name', 'Born in the U.S.A.'),\n    x('artist', 'Bruce Springsteen'),\n    x('releasedate', '1984-04-06')\n  ])\n)\n\n// Children as arguments:\nconsole.log(\n  x(\n    'album',\n    {id: 123},\n    x('name', 'Exile in Guyville'),\n    x('artist', 'Liz Phair'),\n    x('releasedate', '1993-06-22')\n  )\n)\n\n// For other xast nodes, such as comments, instructions, doctypes, or cdata\n// can be created with unist-builder:\nconsole.log(\n  x(null, [\n    u('instruction', {name: 'xml'}, 'version=\"1.0\" encoding=\"UTF-8\"'),\n    x('album', [\n      u('comment', 'Great album!'),\n      x('name', 'Born in the U.S.A.'),\n      x('description', [u('cdata', '3 \u003c 5 \u0026 8 \u003e 13')])\n    ])\n  ])\n)\n```\n\nYields:\n\n```js\n{\n  type: 'element',\n  name: 'album',\n  attributes: {id: '123'},\n  children: [\n    {\n      type: 'element',\n      name: 'name',\n      attributes: {},\n      children: [{type: 'text', value: 'Born in the U.S.A.'}]\n    },\n    {\n      type: 'element',\n      name: 'artist',\n      attributes: {},\n      children: [{type: 'text', value: 'Bruce Springsteen'}]\n    },\n    {\n      type: 'element',\n      name: 'releasedate',\n      attributes: {},\n      children: [{type: 'text', value: '1984-04-06'}]\n    }\n  ]\n}\n{\n  type: 'element',\n  name: 'album',\n  attributes: {id: '123'},\n  children: [\n    {\n      type: 'element',\n      name: 'name',\n      attributes: {},\n      children: [{type: 'text', value: 'Exile in Guyville'}]\n    },\n    {\n      type: 'element',\n      name: 'artist',\n      attributes: {},\n      children: [{type: 'text', value: 'Liz Phair'}]\n    },\n    {\n      type: 'element',\n      name: 'releasedate',\n      attributes: {},\n      children: [{type: 'text', value: '1993-06-22'}]\n    }\n  ]\n}\n{\n  type: 'root',\n  children: [\n    {type: 'instruction', name: 'xml', value: 'version=\"1.0\" encoding=\"UTF-8\"'},\n    {\n      type: 'element',\n      name: 'album',\n      attributes: {},\n      children: [\n        {type: 'comment', value: 'Great album!'},\n        {\n          type: 'element',\n          name: 'name',\n          attributes: {},\n          children: [{type: 'text', value: 'Born in the U.S.A.'}]\n        },\n        {\n          type: 'element',\n          name: 'description',\n          attributes: {},\n          children: [{type: 'cdata', value: '3 \u003c 5 \u0026 8 \u003e 13'}]\n        }\n      ]\n    }\n  ]\n}\n```\n\n## API\n\nThis package exports the identifier [`x`][api-x].\nThere is no default export.\n\nThe export map supports the automatic JSX runtime.\nYou can pass `xastscript` to your build tool (TypeScript, Babel, SWC) with an\n`importSource` option or similar.\n\n### `x(name?[, attributes][, …children])`\n\nCreate [xast][] trees.\n\n##### Signatures\n\n*   `x(): root`\n*   `x(null[, …children]): root`\n*   `x(name[, attributes][, …children]): element`\n\n##### Parameters\n\n###### `name`\n\nQualified name (`string`, optional).\n\nCase sensitive and can contain a namespace prefix (such as `rdf:RDF`).\nWhen string, an [`Element`][element] is built.\nWhen nullish, a [`Root`][root] is built instead.\n\n###### `attributes`\n\nAttributes of the element ([`Attributes`][api-attributes], optional).\n\n###### `children`\n\nChildren of the node ([`Array\u003cChild\u003e`][api-child] or `Child`, optional).\n\n##### Returns\n\nCreated tree ([`Result`][api-result]).\n\n[`Element`][element] when a `name` is passed, otherwise [`Root`][root].\n\n### `Attributes`\n\nMap of attributes (TypeScript type).\n\nNullish (`null` or `undefined`) or `NaN` values are ignored, other values are\nturned to strings.\n\n###### Type\n\n```ts\ntype Attributes = Record\u003cstring, boolean | number | string | null | undefined\u003e\n```\n\n### `Child`\n\n(Lists of) children (TypeScript type).\n\nWhen strings or numbers are encountered, they are turned into [`Text`][text]\nnodes.\n[`Root`][root] nodes are treated as “fragments”, meaning that their children\nare used instead.\n\n###### Type\n\n```ts\ntype Child =\n  | Array\u003cNode | boolean | number | string | null | undefined\u003e\n  | Node\n  | boolean\n  | number\n  | string\n  | null\n  | undefined\n```\n\n### `Result`\n\nResult from a `x` call (TypeScript type).\n\n###### Type\n\n```ts\ntype Result = Element | Root\n```\n\n## Syntax tree\n\nThe syntax tree is [xast][].\n\n## JSX\n\nThis package can be used with JSX.\nYou should use the automatic JSX runtime set to `xastscript`.\n\n\u003e 🪦 **Legacy**: you can also use the classic JSX runtime, but this is not\n\u003e recommended.\n\u003e To do so, import `x` yourself and define it as the pragma (plus set the\n\u003e fragment to `null`).\n\nThe Use example above (omitting the second) can then be written like so:\n\n```jsx\n/** @jsxImportSource xastscript */\n\nimport {u} from 'unist-builder'\n\nconsole.log(\n  \u003calbum id={123}\u003e\n    \u003cname\u003eBorn in the U.S.A.\u003c/name\u003e\n    \u003cartist\u003eBruce Springsteen\u003c/artist\u003e\n    \u003creleasedate\u003e1984-04-06\u003c/releasedate\u003e\n  \u003c/album\u003e\n)\n\nconsole.log(\n  \u003c\u003e\n    {u('instruction', {name: 'xml'}, 'version=\"1.0\" encoding=\"UTF-8\"')}\n    \u003calbum\u003e\n      {u('comment', 'Great album!')}\n      \u003cname\u003eBorn in the U.S.A.\u003c/name\u003e\n      \u003cdescription\u003e{u('cdata', '3 \u003c 5 \u0026 8 \u003e 13')}\u003c/description\u003e\n    \u003c/album\u003e\n  \u003c/\u003e\n)\n```\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types [`Attributes`][api-attributes],\n[`Child`][api-child], and [`Result`][api-result].\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line, `xastscript@^4`, compatible\nwith Node.js 16.\n\n## Security\n\nXML can be a dangerous language: don’t trust user-provided data.\n\n## Related\n\n*   [`unist-builder`][u]\n    — create any unist tree\n*   [`hastscript`][h]\n    — create a hast tree\n*   [`xast-util-to-xml`](https://github.com/syntax-tree/xast-util-to-xml)\n    — serialize xast as XML\n*   [`xast-util-from-xml`](https://github.com/syntax-tree/xast-util-from-xml)\n    — parse xast from XML\n*   [`hast-util-to-xast`](https://github.com/syntax-tree/hast-util-to-xast)\n    — transform hast to xast\n\n## Contribute\n\nSee [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for\nways to get started.\nSee [`support.md`][support] for ways to get help.\n\nThis project has a [code of conduct][coc].\nBy interacting with this repository, organization, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n\u003c!-- Definitions --\u003e\n\n[build-badge]: https://github.com/syntax-tree/xastscript/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/xastscript/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/xastscript.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/xastscript\n\n[downloads-badge]: https://img.shields.io/npm/dm/xastscript.svg\n\n[downloads]: https://www.npmjs.com/package/xastscript\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size\u0026query=$.size.compressedSize\u0026url=https://deno.bundlejs.com/?q=xastscript\n\n[size]: https://bundlejs.com/?q=xastscript\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[collective]: https://opencollective.com/unified\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[chat]: https://github.com/syntax-tree/unist/discussions\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[typescript]: https://www.typescriptlang.org\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[health]: https://github.com/syntax-tree/.github\n\n[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md\n\n[support]: https://github.com/syntax-tree/.github/blob/main/support.md\n\n[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md\n\n[xast]: https://github.com/syntax-tree/xast\n\n[root]: https://github.com/syntax-tree/xast#root\n\n[element]: https://github.com/syntax-tree/xast#element\n\n[text]: https://github.com/syntax-tree/xast#text\n\n[u]: https://github.com/syntax-tree/unist-builder\n\n[h]: https://github.com/syntax-tree/hastscript\n\n[api-x]: #xname-attributes-children\n\n[api-attributes]: #attributes-1\n\n[api-child]: #child\n\n[api-result]: #result\n","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":["xast utilities"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fxastscript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Fxastscript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fxastscript/lists"}