{"id":13716849,"url":"https://github.com/syntax-tree/hast-util-to-xast","last_synced_at":"2025-07-12T07:37:53.685Z","repository":{"id":43396330,"uuid":"234759061","full_name":"syntax-tree/hast-util-to-xast","owner":"syntax-tree","description":"utility to transform to xast (xml)","archived":false,"fork":false,"pushed_at":"2025-02-19T14:59:33.000Z","size":121,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-07-07T04:16:58.354Z","etag":null,"topics":["hast","hast-util","html","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","thanks_dev":"u/gh/syntax-tree"},"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,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-01-18T15:59:46.000Z","updated_at":"2025-02-19T14:59:35.000Z","dependencies_parsed_at":"2025-05-07T06:32:29.512Z","dependency_job_id":"45b6d924-2ac9-42dc-bdc6-1531e5805ccf","html_url":"https://github.com/syntax-tree/hast-util-to-xast","commit_stats":{"total_commits":53,"total_committers":2,"mean_commits":26.5,"dds":"0.018867924528301883","last_synced_commit":"6e67e180ebf222cda016e45b02e14d82fb5b56f0"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/syntax-tree/hast-util-to-xast","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-to-xast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-to-xast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-to-xast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-to-xast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/hast-util-to-xast/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-to-xast/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264958203,"owners_count":23689012,"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":["hast","hast-util","html","syntax-tree","unist","util","xast","xast-util","xml"],"created_at":"2024-08-03T00:01:15.044Z","updated_at":"2025-07-12T07:37:53.663Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified","https://thanks.dev/u/gh/syntax-tree"],"categories":["hast utilities"],"sub_categories":[],"readme":"# hast-util-to-xast\n\n[![Build][badge-build-image]][badge-build-url]\n[![Coverage][badge-coverage-image]][badge-coverage-url]\n[![Downloads][badge-downloads-image]][badge-downloads-url]\n[![Size][badge-size-image]][badge-size-url]\n\n[hast][github-hast] (HTML) utility to transform to [xast][github-xast] (XML).\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  * [`toXast(tree[, options])`](#toxasttree-options)\n  * [`Options`](#options)\n  * [`Space`](#space-1)\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 utility that takes a\n[hast][github-hast] (HTML)\nsyntax tree as input and turns it into a\n[xast][github-xast] (XML)\nsyntax tree.\nThis package also supports embedded MDX nodes.\n\n## When should I use this?\n\nThis project is useful when you want to deal with ASTs,\nand for some reason,\n*have* to deal with XML.\nOne example of this is for EPUB (digital books).\n\nThere is no inverse of this utility,\nbecause not all XML is HTML.\n\nA similar package,\n[`hast-util-to-estree`][github-hast-util-to-estree],\ncan turn hast into estree (JavaScript) as JSX,\nwhich has some similarities to XML.\n\n## Install\n\nThis package is [ESM only][github-gist-esm].\nIn Node.js (version 16+),\ninstall with [npm][npmjs-install]:\n\n```sh\nnpm install hast-util-to-xast\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {toXast} from 'https://esm.sh/hast-util-to-xast@3'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {toXast} from 'https://esm.sh/hast-util-to-xast@3?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\nSay our document `example.html` contains:\n\n```html\n\u003c!doctypehtml\u003e\n\u003ctitle\u003eHello, World!\u003c/title\u003e\n\u003ch1\u003e👋, 🌍\u003c/h1\u003e\n```\n\n…and our module `example.js` looks as follows:\n\n```js\nimport fs from 'node:fs/promises'\nimport {fromHtml} from 'hast-util-from-html'\nimport {toXast} from 'hast-util-to-xast'\nimport {toXml} from 'xast-util-to-xml'\n\n// Get the HTML syntax tree:\nconst hast = fromHtml(await fs.readFile('example.html'))\n\n// Turn hast to xast:\nconst xast = toXast(hast)\n\n// Serialize xast:\nconsole.log(toXml(xast))\n```\n\n…now running `node example.js` yields:\n\n```xml\n\u003c!DOCTYPE html\u003e\u003chtml xmlns=\"http://www.w3.org/1999/xhtml\"\u003e\u003chead\u003e\u003ctitle\u003eHello, World!\u003c/title\u003e\n\u003c/head\u003e\u003cbody\u003e\u003ch1\u003e👋, 🌍\u003c/h1\u003e\n\u003c/body\u003e\u003c/html\u003e\n```\n\n## API\n\nThis package exports the identifier [`toXast`][api-to-xast].\nThere is no default export.\n\n### `toXast(tree[, options])`\n\nTurn a [hast][github-hast] tree into a [xast][github-xast] tree.\n\n###### Parameters\n\n* `tree`\n  ([`HastNode`][github-hast-nodes])\n  — hast tree to transform\n* `options`\n  ([`Options`][api-options], optional)\n  — configuration\n\n###### Returns\n\nxast tree ([`XastNode`][github-xast-nodes]).\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n##### Fields\n\n###### `space`\n\nWhich space the document is in\n([`Space`][api-space], default: `'html'`).\n\nWhen an `\u003csvg\u003e` element is found in the HTML space,\nthis package already automatically switches to and from the SVG space when\nentering and exiting it.\n\nYou can also switch explicitly with `xmlns` properties in hast,\nbut note that only HTML and SVG are supported.\n\n### `Space`\n\nNamespace (TypeScript type).\n\n###### Type\n\n```ts\ntype Space = 'html' | 'svg'\n```\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types [`Options`][api-options] and\n[`Space`][api-space].\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,\nwe drop support for unmaintained versions of Node.\nThis means we try to keep the current release line,\n`hast-util-to-xast@3`,\ncompatible with Node.js 16.\n\n## Security\n\nBoth HTML and XML can be dangerous languages:\ndon’t trust user-provided data.\nUse [`hast-util-santize`][github-hast-util-sanitize]\nto make the hast tree safe before using this utility.\n\n## Related\n\n* [`hastscript`](https://github.com/syntax-tree/hastscript)\n  — create [hast][github-hast] (HTML or SVG) trees\n* [`xastscript`](https://github.com/syntax-tree/xastscript)\n  — create [xast][github-xast] (XML) trees\n* [`xast-util-to-xml`](https://github.com/syntax-tree/xast-util-to-xml)\n  — serialize as XML\n\n## Contribute\n\nSee [`contributing.md`][health-contributing]\nin\n[`syntax-tree/.github`][health]\nfor ways to get started.\nSee [`support.md`][health-support] for ways to get help.\n\nThis project has a [code of conduct][health-coc].\nBy interacting with this repository,\norganization,\nor community you agree to abide by its terms.\n\n## License\n\n[MIT][file-license] © [Titus Wormer][wooorm]\n\n\u003c!-- Definitions --\u003e\n\n[api-options]: #options\n\n[api-space]: #space-1\n\n[api-to-xast]: #toxasttree-options\n\n[badge-build-image]: https://github.com/syntax-tree/hast-util-to-xast/workflows/main/badge.svg\n\n[badge-build-url]: https://github.com/syntax-tree/hast-util-to-xast/actions\n\n[badge-coverage-image]: https://img.shields.io/codecov/c/github/syntax-tree/hast-util-to-xast.svg\n\n[badge-coverage-url]: https://codecov.io/github/syntax-tree/hast-util-to-xast\n\n[badge-downloads-image]: https://img.shields.io/npm/dm/hast-util-to-xast.svg\n\n[badge-downloads-url]: https://www.npmjs.com/package/hast-util-to-xast\n\n[badge-size-image]: https://img.shields.io/bundlejs/size/hast-util-to-xast\n\n[badge-size-url]: https://bundlejs.com/?q=hast-util-to-xast\n\n[esmsh]: https://esm.sh\n\n[file-license]: license\n\n[github-gist-esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[github-hast]: https://github.com/syntax-tree/hast\n\n[github-hast-nodes]: https://github.com/syntax-tree/hast#nodes\n\n[github-hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize\n\n[github-hast-util-to-estree]: https://github.com/syntax-tree/hast-util-to-estree\n\n[github-xast]: https://github.com/syntax-tree/xast\n\n[github-xast-nodes]: https://github.com/syntax-tree/xast#nodes\n\n[health]: https://github.com/syntax-tree/.github\n\n[health-coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md\n\n[health-contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md\n\n[health-support]: https://github.com/syntax-tree/.github/blob/main/support.md\n\n[npmjs-install]: https://docs.npmjs.com/cli/install\n\n[typescript]: https://www.typescriptlang.org\n\n[wooorm]: https://wooorm.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fhast-util-to-xast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Fhast-util-to-xast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fhast-util-to-xast/lists"}