1.1.1 • Published 6 months ago

@getplate/cast-util-to-html v1.1.1

Weekly downloads
-
License
UNLICENSED
Repository
bitbucket
Last release
6 months ago

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 '@getplate/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.