hast (HTML / SVG) utility to transform trees to xast (XML).
npm:
npm install hast-util-to-xast
Say we have an example.html
file, that looks as follows:
<!doctypehtml>
<title>Hello, World!</title>
<h1>π, π</h1>
β¦and our script, example.js
, looks as follows:
var fs = require('fs')
var unified = require('unified')
var parse = require('rehype-parse')
var toXast = require('hast-util-to-xast')
var toXml = require('xast-util-to-xml')
// Get the HTML syntax tree:
var hast = unified()
.use(parse)
.parse(fs.readFileSync('example.html'))
// Turn hast to xast:
var xast = toXast(hast)
// Serialize xast:
console.log(toXml(xast))
Yields:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Hello, World!</title>
</head><body><h1>π, π</h1>
</body></html>
Transform the given hast tree to xast.
Treated as options.space
.
Whether the root of the tree is in the 'html'
or 'svg'
space (enum, 'svg'
or 'html'
, default: 'html'
).
If an svg
element is found in the HTML space, toXast
automatically switches
to the SVG space when entering the element, and switches back when exiting.
You can also switch explicitly with xmlns
properties in hast, but note that
only HTML and SVG are supported.
Both HTML and XML can be a dangerous language: donβt trust user-provided data.
Use hast-util-santize
to make the hast tree safe before using this
utility.
unist-builder
β Create any unist treehastscript
β Create a hast (HTML or SVG) treexastscript
β Create a xast (XML) treexast-util-to-xml
β Serialize nodes to XML
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, organisation, or community you agree to abide by its terms.
MIT Β© Titus Wormer