@platecms/delta-castscript 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 +120 -0
- package/package.json +28 -0
- package/src/index.d.ts +5 -0
- package/src/index.js +6 -0
- package/src/index.js.map +1 -0
- package/src/lib/index.d.ts +7 -0
- package/src/lib/index.js +51 -0
- package/src/lib/index.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# castscript
|
|
2
|
+
|
|
3
|
+
This library is part of the [CAST]() abstract syntax tree utilities.
|
|
4
|
+
It provides a scriptable interface to help with creating cast trees.
|
|
5
|
+
|
|
6
|
+
## Usage
|
|
7
|
+
|
|
8
|
+
Castscript provides an easy way to create cast trees using a scriptable interface.
|
|
9
|
+
Using the utility `script` to `cast` looks as follows:
|
|
10
|
+
```ts
|
|
11
|
+
import {c} from '@platecms/delta-castscript'
|
|
12
|
+
|
|
13
|
+
const tree = c('root', [
|
|
14
|
+
// only using the type argument
|
|
15
|
+
c('paragraph', [
|
|
16
|
+
// using the type and text arguments
|
|
17
|
+
c('text', 'Hello, world!')
|
|
18
|
+
]),
|
|
19
|
+
// using the type, properties and children arguments
|
|
20
|
+
c('heading', { level: 1 }, [
|
|
21
|
+
c('bold', [
|
|
22
|
+
c('text', 'Hello, world!')
|
|
23
|
+
])
|
|
24
|
+
]),
|
|
25
|
+
//using the type, text, properties and children arguments
|
|
26
|
+
c('paragraph', 'Hello,', { class: 'text' }, [
|
|
27
|
+
c('text', 'world!')
|
|
28
|
+
])
|
|
29
|
+
])
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
yields:
|
|
33
|
+
```json
|
|
34
|
+
{
|
|
35
|
+
"type": "root",
|
|
36
|
+
"children": [
|
|
37
|
+
{
|
|
38
|
+
"type": "paragraph",
|
|
39
|
+
"children": [
|
|
40
|
+
{ "type": "text", "value": "Hello, world!" }
|
|
41
|
+
]
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"type": "heading",
|
|
45
|
+
"level": 1,
|
|
46
|
+
"children": [
|
|
47
|
+
{
|
|
48
|
+
"type": "bold",
|
|
49
|
+
"children": [
|
|
50
|
+
{ "type": "text", "value": "Hello, world!" }
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"type": "paragraph",
|
|
57
|
+
"properties": { "class": "text" },
|
|
58
|
+
"children": [
|
|
59
|
+
{ "type": "text", "value": "Hello," },
|
|
60
|
+
{ "type": "text", "value": "world!" }
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
]
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
You can use the following arguments to create complex cast trees:
|
|
68
|
+
|
|
69
|
+
Required arguments
|
|
70
|
+
- `type`: The type of the node
|
|
71
|
+
|
|
72
|
+
Optional arguments, these can be used in different combinations
|
|
73
|
+
- `text`: The text value of the node.
|
|
74
|
+
- `properties`: The properties of the node.
|
|
75
|
+
- `children`: The children of the node.
|
|
76
|
+
|
|
77
|
+
Signature and overloads:
|
|
78
|
+
```ts
|
|
79
|
+
import {Root, Content} from 'cast'
|
|
80
|
+
|
|
81
|
+
function c(type: string): Root | Content {
|
|
82
|
+
// function body
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function c(type: string, text: string): Root | Content {
|
|
86
|
+
// function body
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function c(type: string, properties: Record<string, any>): Root | Content {
|
|
90
|
+
// function body
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function c(type: string, children: Node[]): Root | Content {
|
|
94
|
+
// function body
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function c(type: string, text: string, properties: Record<string, any>): Root | Content {
|
|
98
|
+
// function body
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function c(type: string, text: string, children: Node[]): Root | Content {
|
|
102
|
+
// function body
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function c(type: string, properties: Record<string, any>, children: Node[]): Root | Content {
|
|
106
|
+
// function body
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function c(type: string, text: string, properties: Record<string, any>, children: Node[]): Root | Content {
|
|
110
|
+
// function body
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Building
|
|
115
|
+
|
|
116
|
+
Run `nx build castscript` to build the library.
|
|
117
|
+
|
|
118
|
+
## Running unit tests
|
|
119
|
+
|
|
120
|
+
Run `nx test castscript` to execute the unit tests via [Vitest](https://vitest.dev/).
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@platecms/delta-castscript",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Helper function for quickly creating CAST based trees.",
|
|
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/castscript",
|
|
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-plate-resource-notation": "1.4.1",
|
|
22
|
+
"class-transformer": "0.5.1",
|
|
23
|
+
"lodash": "4.17.21",
|
|
24
|
+
"reflect-metadata": "0.2.2",
|
|
25
|
+
"tslib": "2.8.1"
|
|
26
|
+
},
|
|
27
|
+
"type": "commonjs"
|
|
28
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const c: <T extends "paragraph" | "heading" | "list" | "listItem" | "blockquote" | "code" | "text" | "bold" | "italic" | "underline" | "strikethrough" | "inlineCode" | "highlight" | "contentValue" | "root">(type: T, args_0: string | {
|
|
2
|
+
[x: string]: unknown;
|
|
3
|
+
} | import("generated/dist/packages/cast/src").Content[], args_1?: {
|
|
4
|
+
[x: string]: unknown;
|
|
5
|
+
} | import("generated/dist/packages/cast/src").Content[] | undefined, args_2?: import("generated/dist/packages/cast/src").Content[] | undefined) => T extends "root" ? import("generated/dist/packages/cast/src").Root : import("generated/dist/packages/cast/src").Content;
|
package/src/index.js
ADDED
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/castscript/src/index.ts"],"names":[],"mappings":";;;AAAA,+BAAgC;AAKnB,QAAA,CAAC,GAAG,IAAA,aAAO,GAAE,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Content, Root } from "@platecms/delta-cast";
|
|
2
|
+
type Children = Content[];
|
|
3
|
+
type Properties = Record<string, unknown>;
|
|
4
|
+
type Text = string;
|
|
5
|
+
type NodeType = Content["type"] | "root";
|
|
6
|
+
export declare function createC(): <T extends NodeType>(type: T, ...args: [Children | Properties | Text, (Children | Properties)?, Children?]) => T extends "root" ? Root : Content;
|
|
7
|
+
export {};
|
package/src/lib/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createC = createC;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
function isTextArgument(arg) {
|
|
6
|
+
return typeof arg === "string";
|
|
7
|
+
}
|
|
8
|
+
function isObjectArgument(arg) {
|
|
9
|
+
return (0, lodash_1.isObject)(arg);
|
|
10
|
+
}
|
|
11
|
+
function isChildrenArgument(arg) {
|
|
12
|
+
return Array.isArray(arg);
|
|
13
|
+
}
|
|
14
|
+
function addChild(node, child) {
|
|
15
|
+
if (!node.children)
|
|
16
|
+
node.children = [];
|
|
17
|
+
node.children.push(child);
|
|
18
|
+
}
|
|
19
|
+
function createC() {
|
|
20
|
+
function create(type, ...args) {
|
|
21
|
+
const node = {
|
|
22
|
+
type,
|
|
23
|
+
};
|
|
24
|
+
args.forEach((arg) => {
|
|
25
|
+
if ((0, lodash_1.isUndefined)(arg))
|
|
26
|
+
return;
|
|
27
|
+
if (isTextArgument(arg)) {
|
|
28
|
+
if (type === "text") {
|
|
29
|
+
node.value = arg;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
addChild(node, {
|
|
33
|
+
type: "text",
|
|
34
|
+
value: arg,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else if (isChildrenArgument(arg)) {
|
|
39
|
+
for (const child of arg) {
|
|
40
|
+
addChild(node, child);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else if (isObjectArgument(arg)) {
|
|
44
|
+
Object.assign(node, arg);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return node;
|
|
48
|
+
}
|
|
49
|
+
return create;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/castscript/src/lib/index.ts"],"names":[],"mappings":";;AAmCA,0BAqCC;AAvED,mCAA+C;AAgB/C,SAAS,cAAc,CAAC,GAAS;IAC/B,OAAO,OAAO,GAAG,KAAK,QAAQ,CAAC;AACjC,CAAC;AAED,SAAS,gBAAgB,CAAC,GAAS;IACjC,OAAO,IAAA,iBAAQ,EAAC,GAAG,CAAC,CAAC;AACvB,CAAC;AAED,SAAS,kBAAkB,CAAC,GAAS;IACnC,OAAO,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAC5B,CAAC;AAED,SAAS,QAAQ,CAAC,IAAU,EAAE,KA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