{"id":13602552,"url":"https://github.com/syntax-tree/hast-util-to-html","last_synced_at":"2025-07-12T07:38:04.258Z","repository":{"id":40477024,"uuid":"61429445","full_name":"syntax-tree/hast-util-to-html","owner":"syntax-tree","description":"utility to serialize hast to HTML ","archived":false,"fork":false,"pushed_at":"2025-02-19T13:34:45.000Z","size":395,"stargazers_count":102,"open_issues_count":0,"forks_count":19,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-03-15T17:38:44.240Z","etag":null,"topics":["hast","hast-util","html","stringify","syntax-tree","unist","util"],"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":{"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},"funding":{"github":"unifiedjs","open_collective":"unified"}},"created_at":"2016-06-18T10:58:43.000Z","updated_at":"2025-03-12T13:03:51.000Z","dependencies_parsed_at":"2024-05-01T11:43:22.775Z","dependency_job_id":"f44c44d8-d3bf-443b-8578-1bf04a878a94","html_url":"https://github.com/syntax-tree/hast-util-to-html","commit_stats":{"total_commits":146,"total_committers":6,"mean_commits":"24.333333333333332","dds":0.04109589041095896,"last_synced_commit":"3c9469abbb1ddd576e93387e37434c0f4d1db6ef"},"previous_names":["wooorm/hast-util-to-html"],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-to-html","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-to-html/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-to-html/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-to-html/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-html/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248361677,"owners_count":21090961,"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","stringify","syntax-tree","unist","util"],"created_at":"2024-08-01T18:01:27.888Z","updated_at":"2025-07-12T07:38:04.247Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","readme":"# hast-util-to-html\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] utility to serialize hast as HTML.\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  * [`toHtml(tree[, options])`](#tohtmltree-options)\n  * [`CharacterReferences`](#characterreferences)\n  * [`Options`](#options)\n  * [`Quote`](#quote-1)\n  * [`Space`](#space-1)\n* [Syntax](#syntax)\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 turns a hast tree into a string of HTML.\n\n## When should I use this?\n\nYou can use this utility when you want to get the serialized HTML that is\nrepresented by the syntax tree,\neither because you’re done with the syntax tree,\nor because you’re integrating with another tool that does not support syntax\ntrees.\n\nThis utility has many options to configure how the HTML is serialized.\nThese options help when building tools that make output pretty (such as\nformatters) or ugly (such as minifiers).\n\nThe utility [`hast-util-from-html`][github-hast-util-from-html] does the\ninverse of this utility.\nIt turns HTML into hast.\n\nThe rehype plugin [`rehype-stringify`][github-rehype-stringify] wraps this\nutility to also serialize HTML at a higher-level (easier) abstraction.\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-html\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {toHtml} from 'https://esm.sh/hast-util-to-html@9'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {toHtml} from 'https://esm.sh/hast-util-to-html@9?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\n```js\nimport {h} from 'hastscript'\nimport {toHtml} from 'hast-util-to-html'\n\nconst tree = h('.alpha', [\n  'bravo ',\n  h('b', 'charlie'),\n  ' delta ',\n  h('a.echo', {download: true}, 'foxtrot')\n])\n\nconsole.log(toHtml(tree))\n```\n\nYields:\n\n```html\n\u003cdiv class=\"alpha\"\u003ebravo \u003cb\u003echarlie\u003c/b\u003e delta \u003ca class=\"echo\" download\u003efoxtrot\u003c/a\u003e\u003c/div\u003e\n```\n\n## API\n\nThis package exports the identifier [`toHtml`][api-to-html].\nThere is no default export.\n\n### `toHtml(tree[, options])`\n\nSerialize hast as HTML.\n\n###### Parameters\n\n* `tree`\n  ([`Node`][github-hast-nodes] or `Array\u003cNode\u003e`)\n  — tree to serialize\n* `options`\n  ([`Options`][api-options], optional)\n  — configuration\n\n###### Returns\n\nSerialized HTML (`string`).\n\n### `CharacterReferences`\n\nHow to serialize character references (TypeScript type).\n\n##### Fields\n\n###### `useNamedReferences`\n\nPrefer named character references (`\u0026amp;`) where possible\n(`boolean`, default: `false`).\n\n###### `omitOptionalSemicolons`\n\nWhether to omit semicolons when possible\n(`boolean`, default: `false`).\n\n\u003e ⚠️ **Note**:\n\u003e this creates what HTML calls “parse errors” but is otherwise still valid HTML:\n\u003e don’t use this except when building a minifier;\n\u003e omitting semicolons is possible for certain named and numeric references in\n\u003e some cases.\n\n###### `useShortestReferences`\n\nPrefer the shortest possible reference,\nif that results in less bytes\n(`boolean`, default: `false`).\n\n\u003e ⚠️ **Note**:\n\u003e `useNamedReferences` can be omitted when using `useShortestReferences`.\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n##### Fields\n\n###### `allowDangerousCharacters`\n\nDo not encode some characters which cause XSS vulnerabilities in older browsers\n(`boolean`, default: `false`).\n\n\u003e ⚠️ **Danger**:\n\u003e only set this if you completely trust the content.\n\n###### `allowDangerousHtml`\n\nAllow `raw` nodes and insert them as raw HTML\n(`boolean`, default: `false`).\n\nWhen `false`,\n`Raw` nodes are encoded.\n\n\u003e ⚠️ **Danger**:\n\u003e only set this if you completely trust the content.\n\n###### `allowParseErrors`\n\nDo not encode characters which cause parse errors\n(even though they work),\nto save bytes\n(`boolean`, default: `false`).\n\nNot used in the SVG space.\n\n\u003e 👉 **Note**:\n\u003e intentionally creates parse errors in markup\n\u003e (how parse errors are handled is well defined,\n\u003e so this works but isn’t pretty).\n\n###### `bogusComments`\n\nUse “bogus comments” instead of comments to save byes:\n`\u003c?charlie\u003e` instead of `\u003c!--charlie--\u003e`\n(`boolean`, default: `false`).\n\n\u003e 👉 **Note**:\n\u003e intentionally creates parse errors in markup\n\u003e (how parse errors are handled is well defined,\n\u003e so this works but isn’t pretty).\n\n###### `characterReferences`\n\nConfigure how to serialize character references\n([`CharacterReferences`][api-character-references], optional).\n\n###### `closeEmptyElements`\n\nClose SVG elements without any content with slash (`/`) on the opening tag\ninstead of an end tag:\n`\u003ccircle /\u003e` instead of `\u003ccircle\u003e\u003c/circle\u003e`\n(`boolean`, default: `false`).\n\nSee `tightSelfClosing` to control whether a space is used before the slash.\n\nNot used in the HTML space.\n\n###### `closeSelfClosing`\n\nClose self-closing nodes with an extra slash (`/`):\n`\u003cimg /\u003e` instead of `\u003cimg\u003e`\n(`boolean`, default: `false`).\n\nSee `tightSelfClosing` to control whether a space is used before the slash.\n\nNot used in the SVG space.\n\n###### `collapseEmptyAttributes`\n\nCollapse empty attributes:\nget `class` instead of `class=\"\"`\n(`boolean`, default: `false`).\n\nNot used in the SVG space.\n\n\u003e 👉 **Note**:\n\u003e boolean attributes\n\u003e (such as `hidden`)\n\u003e are always collapsed.\n\n###### `omitOptionalTags`\n\nOmit optional opening and closing tags\n(`boolean`, default: `false`).\n\nFor example,\nin `\u003col\u003e\u003cli\u003eone\u003c/li\u003e\u003cli\u003etwo\u003c/li\u003e\u003c/ol\u003e`,\nboth `\u003c/li\u003e` closing tags can be omitted.\nThe first because it’s followed by another `li`,\nthe last because it’s followed by nothing.\n\nNot used in the SVG space.\n\n###### `preferUnquoted`\n\nLeave attributes unquoted if that results in less bytes\n(`boolean`, default: `false`).\n\nNot used in the SVG space.\n\n###### `quote`\n\nPreferred quote to use\n([`Quote`][api-quote], default: `'\"'`).\n\n###### `quoteSmart`\n\nUse the other quote if that results in less bytes\n(`boolean`, default: `false`).\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\n\u003e 👉 **Note**:\n\u003e hast is not XML.\n\u003e It supports SVG as embedded in HTML.\n\u003e It does not support the features available in XML.\n\u003e Passing SVG might break but fragments of modern SVG should be fine.\n\u003e Use [`xast`][github-xast] if you need to support SVG as XML.\n\n###### `tightAttributes`\n\nJoin attributes together,\nwithout whitespace,\nif possible:\nget `class=\"a b\"title=\"c d\"` instead of `class=\"a b\" title=\"c d\"` to save bytes\n(`boolean`, default: `false`).\n\nNot used in the SVG space.\n\n\u003e 👉 **Note**:\n\u003e intentionally creates parse errors in markup\n\u003e (how parse errors are handled is well defined,\n\u003e so this works but isn’t pretty).\n\n###### `tightCommaSeparatedLists`\n\nJoin known comma-separated attribute values with just a comma (`,`),\ninstead of padding them on the right as well\n(`,␠`, where `␠` represents a space)\n(`boolean`, default: `false`).\n\n###### `tightDoctype`\n\nDrop unneeded spaces in doctypes:\n`\u003c!doctypehtml\u003e` instead of `\u003c!doctype html\u003e` to save bytes\n(`boolean`, default: `false`).\n\n\u003e 👉 **Note**:\n\u003e intentionally creates parse errors in markup\n\u003e (how parse errors are handled is well defined,\n\u003e so this works but isn’t pretty).\n\n###### `tightSelfClosing`\n\nDo not use an extra space when closing self-closing elements:\n`\u003cimg/\u003e` instead of `\u003cimg /\u003e`\n(`boolean`, default: `false`).\n\n\u003e 👉 **Note**:\n\u003e only used if `closeSelfClosing: true` or `closeEmptyElements: true`.\n\n###### `upperDoctype`\n\nUse a `\u003c!DOCTYPE…` instead of `\u003c!doctype…`\n(`boolean`, default: `false`).\n\nUseless except for XHTML.\n\n###### `voids`\n\nTag names of elements to serialize without closing tag\n(`Array\u003cstring\u003e`,\ndefault: [`html-void-elements`][github-html-void-elements]).\n\nNot used in the SVG space.\n\n\u003e 👉 **Note**:\n\u003e it’s highly unlikely that you want to pass this,\n\u003e because hast is not for XML,\n\u003e and HTML will not add more void elements.\n\n### `Quote`\n\nHTML quotes for attribute values (TypeScript type).\n\n###### Type\n\n```ts\ntype Quote = '\"' | \"'\"\n```\n\n### `Space`\n\nNamespace (TypeScript type).\n\n###### Type\n\n```ts\ntype Space = 'html' | 'svg'\n```\n\n## Syntax\n\nHTML is serialized according to WHATWG HTML (the living standard),\nwhich is also followed by browsers such as Chrome and Firefox.\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types\n[`CharacterReferences`][api-character-references],\n[`Options`][api-options],\n[`Quote`][api-quote],\nand\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-html@9`,\ncompatible with Node.js 16.\n\n## Security\n\nUse of `hast-util-to-html` can open you up to a\n[cross-site scripting (XSS)][wikipedia-xss] attack if the hast tree is unsafe.\nUse [`hast-util-santize`][github-hast-util-sanitize] to make the hast tree\nsafe.\n\n## Related\n\n* [`hast-util-sanitize`][github-hast-util-sanitize]\n  — sanitize hast\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-character-references]: #characterreferences\n\n[api-options]: #options\n\n[api-quote]: #quote-1\n\n[api-space]: #space-1\n\n[api-to-html]: #tohtmltree-options\n\n[badge-build-image]: https://github.com/syntax-tree/hast-util-to-html/workflows/main/badge.svg\n\n[badge-build-url]: https://github.com/syntax-tree/hast-util-to-html/actions\n\n[badge-coverage-image]: https://img.shields.io/codecov/c/github/syntax-tree/hast-util-to-html.svg\n\n[badge-coverage-url]: https://codecov.io/github/syntax-tree/hast-util-to-html\n\n[badge-downloads-image]: https://img.shields.io/npm/dm/hast-util-to-html.svg\n\n[badge-downloads-url]: https://www.npmjs.com/package/hast-util-to-html\n\n[badge-size-image]: https://img.shields.io/bundlejs/size/hast-util-to-html\n\n[badge-size-url]: https://bundlejs.com/?q=hast-util-to-html\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-from-html]: https://github.com/syntax-tree/hast-util-from-html\n\n[github-hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize\n\n[github-html-void-elements]: https://github.com/wooorm/html-void-elements\n\n[github-rehype-stringify]: https://github.com/rehypejs/rehype/tree/main/packages/rehype-stringify#readme\n\n[github-xast]: https://github.com/syntax-tree/xast\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[wikipedia-xss]: https://en.wikipedia.org/wiki/Cross-site_scripting\n\n[wooorm]: https://wooorm.com\n","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":["JavaScript","hast utilities"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fhast-util-to-html","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Fhast-util-to-html","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fhast-util-to-html/lists"}