@larsbaalmans/cast-util-to-html
v1.2.0
Published
This library is part of the [CAST]() abstract syntax tree utilities. It provides a utility to convert a [CAST]() abstract syntax tree directly to html.
Downloads
76
Readme
cast-util-to-html
This library is part of the CAST abstract syntax tree utilities. It provides a utility to convert a CAST abstract syntax tree directly to html.
The converter uses cast to hast and hast to html to convert the tree. It also sanitizes the html using rehype-sanitize on the Hast syntax tree.
Usage
Using the utility cast
to html looks as follows:
/**
* @import {Root} from 'cast'
*/
import {toHtml} from 'cast-util-to-html'
/** @type {Root} */
const tree = [
{
type: 'paragraph',
children: [
{ text: 'Hello, world!' }
]
},
{
type: 'heading',
level: 1,
children: [
{
type: 'bold',
children: [
{
type: 'text',
text: 'Hello, world!'
}
]
}
]
}
]
console.info(toHtml(tree))
yields:
const result = {
type: 'root',
children: [
{
type: 'element',
tagName: 'p',
properties: {},
children: [
{ type: 'text', value: 'Hello, world!' }
]
},
{
type: 'element',
tagName: 'h1',
properties: {},
children: [
{
type: 'element',
tagName: 'strong',
properties: {},
children: [
{ type: 'text', value: 'Hello, world!' }
]
}
]
}
]
}
Building
Run nx build cast-util-to-html
to build the library.
Running unit tests
Run nx test cast-util-to-html
to execute the unit tests via Vitest.