@platecms/delta-cast-util-to-html 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # cast-util-to-html
2
+
3
+ This library is part of the [CAST]() abstract syntax tree utilities.
4
+ It provides a utility to convert a [CAST]() abstract syntax tree directly to html.
5
+
6
+ The converter uses cast to hast and hast to html to convert the tree.
7
+ It also sanitizes the html using [rehype-sanitize]() on the Hast syntax tree.
8
+
9
+
10
+ ## Usage
11
+
12
+ Using the utility `cast` to html looks as follows:
13
+ ```ts
14
+ /**
15
+ * @import {Root} from 'cast'
16
+ */
17
+
18
+ import {toHtml} from '@platecms/delta-cast-util-to-html'
19
+
20
+ /** @type {Root} */
21
+ const tree = [
22
+ {
23
+ type: 'paragraph',
24
+ children: [
25
+ { text: 'Hello, world!' }
26
+ ]
27
+ },
28
+ {
29
+ type: 'heading',
30
+ level: 1,
31
+ children: [
32
+ {
33
+ type: 'bold',
34
+ children: [
35
+ {
36
+ type: 'text',
37
+ text: 'Hello, world!'
38
+ }
39
+ ]
40
+ }
41
+ ]
42
+ }
43
+ ]
44
+
45
+ console.info(toHtml(tree))
46
+ ```
47
+
48
+ yields:
49
+ ```ts
50
+ const result = {
51
+ type: 'root',
52
+ children: [
53
+ {
54
+ type: 'element',
55
+ tagName: 'p',
56
+ properties: {},
57
+ children: [
58
+ { type: 'text', value: 'Hello, world!' }
59
+ ]
60
+ },
61
+ {
62
+ type: 'element',
63
+ tagName: 'h1',
64
+ properties: {},
65
+ children: [
66
+ {
67
+ type: 'element',
68
+ tagName: 'strong',
69
+ properties: {},
70
+ children: [
71
+ { type: 'text', value: 'Hello, world!' }
72
+ ]
73
+ }
74
+ ]
75
+ }
76
+ ]
77
+ }
78
+ ```
79
+
80
+ ## Building
81
+
82
+ Run `nx build cast-util-to-html` to build the library.
83
+
84
+ ## Running unit tests
85
+
86
+ Run `nx test cast-util-to-html` to execute the unit tests via [Vitest](https://vitest.dev/).
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@platecms/delta-cast-util-to-html",
3
+ "version": "0.1.0",
4
+ "description": "Utility to convert CAST directly to HTML using HAST and HAST sanitizing.",
5
+ "license": "UNLICENSED",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://bitbucket.org/startmetplate/delta.git"
12
+ },
13
+ "homepage": "https://bitbucket.org/startmetplate/delta/src/dev/packages/cast-util-to-html",
14
+ "main": "./src/index.js",
15
+ "types": "./src/index.d.ts",
16
+ "files": [
17
+ "src/**/*"
18
+ ],
19
+ "dependencies": {
20
+ "@platecms/delta-cast": "0.4.8",
21
+ "@platecms/delta-cast-util-to-hast": "1.1.1",
22
+ "@platecms/delta-plate-resource-notation": "1.4.1",
23
+ "class-transformer": "0.5.1",
24
+ "hast": "1.0.0",
25
+ "hast-util-sanitize": "5.0.2",
26
+ "hast-util-to-html": "9.0.4",
27
+ "lodash": "4.17.21",
28
+ "reflect-metadata": "0.2.2",
29
+ "tslib": "2.8.1",
30
+ "zwitch": "2.0.4"
31
+ },
32
+ "type": "commonjs"
33
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { Root } from "@platecms/delta-cast";
2
+ import { NodeTransformers } from "@platecms/delta-cast-util-to-hast";
3
+ export declare function toHtml(value: Root, transform?: NodeTransformers): string;
package/src/index.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toHtml = toHtml;
4
+ const delta_cast_util_to_hast_1 = require("@platecms/delta-cast-util-to-hast");
5
+ const hast_util_sanitize_1 = require("hast-util-sanitize");
6
+ const hast_util_to_html_1 = require("hast-util-to-html");
7
+ function toHtml(value, transform = {
8
+ contentValue: delta_cast_util_to_hast_1.flattenTextOnlyContentValueNode,
9
+ }) {
10
+ return (0, hast_util_to_html_1.toHtml)((0, hast_util_sanitize_1.sanitize)((0, delta_cast_util_to_hast_1.toHast)(value, transform)));
11
+ }
12
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/cast-util-to-html/src/index.ts"],"names":[],"mappings":";;AAYA,wBAOC;AAlBD,+EAA8G;AAC9G,2DAA8C;AAC9C,yDAAyD;AASzD,SAAgB,MAAM,CACpB,KAAW,EACX,YAA8B;IAC5B,YAAY,EAAE,yDAA+B;CAC9C;IAED,OAAO,IAAA,0BAAU,EAAC,IAAA,6BAAQ,EAAC,IAAA,gCAAM,EAAC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC;AACxD,CAAC"}