1.1.1 • Published 11 months ago

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

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

cast-util-to-hast

This library is part of the CAST abstract syntax tree utilities. It provides a utility to convert a CAST abstract syntax tree to a Hast abstract syntax tree.

Usage

Using the utility cast to hast looks as follows:

/**
 * @import {Root} from 'cast'
 */

import {toHast} from '@getplate/cast-util-to-hast'

/** @type {Root} */
const tree = {
    type: 'root',
    children: [
        {
            type: 'paragraph',
            children: [
                { type: 'text', value: 'Hello, world!' }
            ]
        }
    ]
}

console.info(toHast(tree))

yields:

{
    "type": "root",
        "children": [
        {
            "type": "element",
            "tagName": "p",
            "properties": {},
            "children": [
                { 
                  "type": "text", 
                  "value": "Hello, world!"
                }
            ]
        }
    ]
}

Transformers

The utility provides a set of transformers that can be used to transform the cast tree before it is converted to hast. By default the contentValue transformer removes the first block element if it has exactly one child. This enables inline rendering of content values when they are used as a single value inside another value.

/**
 * @import {Root} from 'cast'
 */

import {toHast} from '@getplate/cast-util-to-hast'

/** @type {Root} */
const cast: Root = {
  type: "root",
  children: [
    {
      type: "paragraph",
      children: [
        {
            type: "text",
            value: "My first "
        },
        {
            type: "contentValue",
            value: {
                type: "root",
                children: [
                    {
                        type: "paragraph",
                        children: [
                            {
                              type: "text",
                              value: "content"
                            },
                        ]
                    },
                ]
            },
            prn: "prn:plate-dev:a7734468-b894-4191-83b1-7d218f8c9922:cxc:content-value:b2119c94-24e6-4358-b557-c705dcd19d1d"
        },
      ],
    },
  ],
};

yields (hast):

      {
  "type": "root",
  "children": [
    {
      "type": "element",
      "tagName": "p",
      "properties": {},
      "children": [
        {
          "type": "text",
          "value": "My first "
        },
        {
          "type": "element",
          "tagName": "span",
          "properties": {},
          "children": [
            {
              "type": "text",
              "value": "content"
            }
          ]
        }
      ]
    }
  ]
}

or yields (html):

<p>My first <span>content</span></p>

Building

Run nx build cast-util-to-hast to build the library.

Running unit tests

Run nx test cast-util-to-hast to execute the unit tests via Vitest.