{"id":13716775,"url":"https://github.com/syntax-tree/hast-util-from-dom","last_synced_at":"2025-06-29T22:03:43.416Z","repository":{"id":34913236,"uuid":"135494063","full_name":"syntax-tree/hast-util-from-dom","owner":"syntax-tree","description":"utility to transform a DOM tree to hast","archived":false,"fork":false,"pushed_at":"2024-11-19T09:17:55.000Z","size":640,"stargazers_count":22,"open_issues_count":0,"forks_count":4,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-05-23T02:23:30.483Z","etag":null,"topics":["dom","hast","hast-util","html","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":"isc","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":"2018-05-30T20:26:56.000Z","updated_at":"2024-11-19T09:17:57.000Z","dependencies_parsed_at":"2024-01-14T22:03:36.917Z","dependency_job_id":"4c914b02-2b32-4d8c-9f89-99f8e4d11d7a","html_url":"https://github.com/syntax-tree/hast-util-from-dom","commit_stats":{"total_commits":81,"total_committers":6,"mean_commits":13.5,"dds":0.1728395061728395,"last_synced_commit":"444dbb39dd1112dd1eb3bb278bdc4441c8b65639"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/syntax-tree/hast-util-from-dom","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-from-dom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-from-dom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-from-dom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-from-dom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/hast-util-from-dom/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-from-dom/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261758532,"owners_count":23205444,"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":["dom","hast","hast-util","html","syntax-tree","unist","util"],"created_at":"2024-08-03T00:01:14.211Z","updated_at":"2025-06-29T22:03:43.371Z","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-from-dom\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[hast][] utility to transform from a [DOM][] tree.\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  * [`fromDom(tree, options?)`](#fromdomtree-options)\n  * [`AfterTransform`](#aftertransform)\n  * [`Options`](#options)\n* [Types](#types)\n* [Compatibility](#compatibility)\n* [Security](#security)\n* [Contribute](#contribute)\n* [Related](#related)\n* [License](#license)\n\n## What is this?\n\nThis package is a utility that takes a DOM tree (from the actual DOM or from\nthings like [`jsdom`][jsdom]) as input and turns it into a [hast][] (HTML)\nsyntax tree.\n\n## When should I use this?\n\nYou can use this project when you want to use hast in browsers.\nThis package is very small, but it does so by:\n\n* …not providing positional information\n* …potentially yielding varying results in different (especially older)\n  browsers\n\nThe hast utility [`hast-util-to-dom`][hast-util-to-dom] does the inverse of this\nutility.\nIt turns hast into a DOM tree.\n\nThe rehype plugin [`rehype-dom-parse`][rehype-dom-parse] wraps this utility to\nparse HTML with DOM APIs.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install hast-util-from-dom\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {fromDom} from 'https://esm.sh/hast-util-from-dom@5'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {fromDom} from 'https://esm.sh/hast-util-from-dom@5?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\nSay our page `example.html` looks as follows:\n\n```html\n\u003c!doctype html\u003e\n\u003ctitle\u003eExample\u003c/title\u003e\n\u003cbody\u003e\n  \u003cmain\u003e\n    \u003ch1\u003eHi\u003c/h1\u003e\n    \u003cp\u003e\u003cem\u003eHello\u003c/em\u003e, world!\u003c/p\u003e\n  \u003c/main\u003e\n  \u003cscript type=\"module\"\u003e\n    import {fromDom} from 'https://esm.sh/hast-util-from-dom@4?bundle'\n\n    const hast = fromDom(document.querySelector('main'))\n\n    console.log(hast)\n  \u003c/script\u003e\n```\n\nNow running `open example.html` prints the following to the console:\n\n```js\n{type: \"element\", tagName: \"main\", properties: {}, children: Array}\n```\n\n## API\n\nThis package exports the identifier [`fromDom`][api-from-dom].\nThere is no default export.\n\n### `fromDom(tree, options?)`\n\nTransform a DOM tree to a hast tree.\n\n###### Parameters\n\n* `tree` ([`DomNode`][dom-node])\n  — DOM tree to transform\n* `options` ([`Options`][api-options], optional)\n  — configuration\n\n###### Returns\n\nEquivalent hast node ([`HastNode`][hast-node]).\n\n### `AfterTransform`\n\nCallback called when each node is transformed (TypeScript type).\n\n###### Parameters\n\n* `domNode` ([`DomNode`][dom-node])\n  — DOM node that was handled\n* `hastNode` ([`HastNode`][hast-node])\n  — corresponding hast node\n\n###### Returns\n\nNothing.\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n##### Fields\n\n* `afterTransform` ([`AfterTransform`][api-after-transform], optional)\n  — callback called when each node is transformed\n\n##### Returns\n\n[`HastNode`][hast-node].\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types [`AfterTransform`][api-after-transform] and\n[`Options`][api-options].\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, `hast-util-from-dom@^5`,\ncompatible with Node.js 16.\n\n## Security\n\nUse of `hast-util-from-dom` itself is safe but see other utilities for more\ninformation on potential security problems.\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, organisation, or community you agree to\nabide by its terms.\n\n## Related\n\n* [`hast-util-from-html`][hast-util-from-html]\n  — parse hast from a string of HTML\n* [`hast-util-sanitize`](https://github.com/syntax-tree/hast-util-sanitize)\n  — sanitize hast nodes\n* [`hast-util-to-html`](https://github.com/syntax-tree/hast-util-to-html)\n  — serialize hast as HTML\n* [`hast-util-to-dom`](https://github.com/syntax-tree/hast-util-to-dom)\n  — create DOM trees from hast\n\n## License\n\n[ISC][license] © [Keith McKnight][author]\n\n\u003c!-- Definitions --\u003e\n\n[build-badge]: https://github.com/syntax-tree/hast-util-from-dom/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/hast-util-from-dom/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/hast-util-from-dom.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/hast-util-from-dom\n\n[downloads-badge]: https://img.shields.io/npm/dm/hast-util-from-dom.svg\n\n[downloads]: https://www.npmjs.com/package/hast-util-from-dom\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size\u0026query=$.size.compressedSize\u0026url=https://deno.bundlejs.com/?q=hast-util-from-dom\n\n[size]: https://bundlejs.com/?q=hast-util-from-dom\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://keith.mcknig.ht\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[hast]: https://github.com/syntax-tree/hast\n\n[hast-node]: https://github.com/syntax-tree/hast#nodes\n\n[dom]: https://developer.mozilla.org/docs/Web/API/Document_Object_Model\n\n[dom-node]: https://developer.mozilla.org/docs/Web/API/Node\n\n[hast-util-from-html]: https://github.com/syntax-tree/hast-util-from-html\n\n[hast-util-to-dom]: https://github.com/syntax-tree/hast-util-to-dom\n\n[rehype-dom-parse]: https://github.com/rehypejs/rehype-dom/tree/main/packages/rehype-dom-parse\n\n[jsdom]: https://github.com/jsdom/jsdom\n\n[api-from-dom]: #fromdomtree-options\n\n[api-options]: #options\n\n[api-after-transform]: #aftertransform\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fhast-util-from-dom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Fhast-util-from-dom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fhast-util-from-dom/lists"}