@platecms/delta-cast-util-from-slate 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 +49 -0
- package/package.json +37 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +6 -0
- package/src/index.js.map +1 -0
- package/src/lib/index.d.ts +3 -0
- package/src/lib/index.js +87 -0
- package/src/lib/index.js.map +1 -0
- package/src/lib/utils/wrapNode.d.ts +3 -0
- package/src/lib/utils/wrapNode.js +16 -0
- package/src/lib/utils/wrapNode.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# cast-util-from-slate
|
|
2
|
+
|
|
3
|
+
This library is part of the [CAST]() abstract syntax tree utilities.
|
|
4
|
+
It provides a utility to convert a [Slate](https://docs.slatejs.org/) editor document to a [CAST]() abstract syntax tree.
|
|
5
|
+
|
|
6
|
+
## Usage
|
|
7
|
+
|
|
8
|
+
Using the utility `cast` from `slate` looks as follows:
|
|
9
|
+
```ts
|
|
10
|
+
import {fromSlate} from '@platecms/delta-cast-util-from-slate'
|
|
11
|
+
|
|
12
|
+
const slate = [
|
|
13
|
+
{
|
|
14
|
+
type: 'paragraph',
|
|
15
|
+
children: [
|
|
16
|
+
{ text: 'Hello, world!' }
|
|
17
|
+
]
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
console.info(fromSlate(slate))
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
yields:
|
|
25
|
+
```json
|
|
26
|
+
{
|
|
27
|
+
"type": "root",
|
|
28
|
+
"children": [
|
|
29
|
+
{
|
|
30
|
+
"type": "paragraph",
|
|
31
|
+
"children": [
|
|
32
|
+
{
|
|
33
|
+
"type": "text",
|
|
34
|
+
"value": "Hello, world!"
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
]
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Building
|
|
43
|
+
|
|
44
|
+
Run `nx build cast-util-from-slate` to build the library.
|
|
45
|
+
|
|
46
|
+
## Running unit tests
|
|
47
|
+
|
|
48
|
+
Run `nx test cast-util-from-slate` to execute the unit tests via [Vitest](https://vitest.dev/).
|
|
49
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@platecms/delta-cast-util-from-slate",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Utility to convert slate nodes to a CAST tree.",
|
|
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-from-slate",
|
|
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-castscript": "1.0.3",
|
|
22
|
+
"@platecms/delta-client": "1.1.0",
|
|
23
|
+
"@platecms/delta-plate-resource-notation": "1.4.1",
|
|
24
|
+
"@graphql-typed-document-node/core": "3.2.0",
|
|
25
|
+
"class-transformer": "0.5.1",
|
|
26
|
+
"graphql": "16.10.0",
|
|
27
|
+
"lodash": "4.17.21",
|
|
28
|
+
"reflect-metadata": "0.2.2",
|
|
29
|
+
"slate": "0.112.0",
|
|
30
|
+
"slate-react": "0.112.1",
|
|
31
|
+
"tslib": "2.8.1",
|
|
32
|
+
"zwitch": "2.0.4",
|
|
33
|
+
"@apollo/client": "3.13.1",
|
|
34
|
+
"defu": "^6.1.4"
|
|
35
|
+
},
|
|
36
|
+
"type": "commonjs"
|
|
37
|
+
}
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fromSlate = void 0;
|
|
4
|
+
const index_1 = require("./lib/index");
|
|
5
|
+
Object.defineProperty(exports, "fromSlate", { enumerable: true, get: function () { return index_1.fromSlate; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/cast-util-from-slate/src/index.ts"],"names":[],"mappings":";;;AAAA,uCAAwC;AAE/B,0FAFA,iBAAS,OAEA"}
|
package/src/lib/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fromSlate = fromSlate;
|
|
4
|
+
const wrapNode_1 = require("./utils/wrapNode");
|
|
5
|
+
const zwitch_1 = require("zwitch");
|
|
6
|
+
function fromSlate(value) {
|
|
7
|
+
return {
|
|
8
|
+
type: "root",
|
|
9
|
+
children: all(value, {}),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
const handleNode = (0, zwitch_1.zwitch)("type", { handlers: { paragraph, heading, list, listItem, blockquote, code, contentValue } });
|
|
13
|
+
function all(nodes, state = {}) {
|
|
14
|
+
let index = -1;
|
|
15
|
+
const results = [];
|
|
16
|
+
while (++index < nodes.length) {
|
|
17
|
+
const result = one(nodes[index], state);
|
|
18
|
+
results.push(result);
|
|
19
|
+
}
|
|
20
|
+
return results;
|
|
21
|
+
}
|
|
22
|
+
function one(node, state = {}) {
|
|
23
|
+
let result;
|
|
24
|
+
if ("text" in node) {
|
|
25
|
+
result = (0, wrapNode_1.wrapNode)(node);
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
result = handleNode(node, state);
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
function paragraph(node, state = {}) {
|
|
32
|
+
return {
|
|
33
|
+
type: "paragraph",
|
|
34
|
+
children: all(node.children, state),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function heading(node, state = {}) {
|
|
38
|
+
return {
|
|
39
|
+
type: "heading",
|
|
40
|
+
level: node.level,
|
|
41
|
+
children: all(node.children, state),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function list(node, state = {}) {
|
|
45
|
+
return {
|
|
46
|
+
type: "list",
|
|
47
|
+
ordered: node.ordered,
|
|
48
|
+
children: all(node.children, state),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function listItem(node, state = {}) {
|
|
52
|
+
return {
|
|
53
|
+
type: "listItem",
|
|
54
|
+
children: all(node.children, state),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function blockquote(node, state = {}) {
|
|
58
|
+
return {
|
|
59
|
+
type: "blockquote",
|
|
60
|
+
children: all(node.children, state),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function code(node, state = {}) {
|
|
64
|
+
return {
|
|
65
|
+
type: "code",
|
|
66
|
+
children: all(node.children, state),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function contentValue(node) {
|
|
70
|
+
const firstChild = node.children[0];
|
|
71
|
+
if ("text" in firstChild) {
|
|
72
|
+
return (0, wrapNode_1.wrapNode)({
|
|
73
|
+
...firstChild,
|
|
74
|
+
text: "",
|
|
75
|
+
}, {
|
|
76
|
+
prn: node.prn,
|
|
77
|
+
type: "contentValue",
|
|
78
|
+
value: node.root,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
type: "contentValue",
|
|
83
|
+
prn: node.prn,
|
|
84
|
+
value: node.root,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
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ACb;YACE,GAAG,UAAU;YACb,IAAI,EAAE,EAAE;SACT,EACD;YACE,GAAG,EAAE,IAAI,CAAC,GAAG;YACb,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE,IAAI,CAAC,IAAI;SACjB,CACF,CAAC;IACJ,CAAC;IAED,OAAO;QACL,IAAI,EAAE,cAAc;QACpB,GAAG,EAAE,IAAI,CAAC,GAAG;QACb,KAAK,EAAE,IAAI,CAAC,IAAI;KACjB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wrapNode = wrapNode;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
function wrapNode(node, enrich) {
|
|
6
|
+
const wrapIn = (0, lodash_1.keys)(node).filter((key) => key !== "text");
|
|
7
|
+
return (0, lodash_1.reduce)(wrapIn, (acc, key) => ({
|
|
8
|
+
type: key,
|
|
9
|
+
children: [acc],
|
|
10
|
+
}), {
|
|
11
|
+
type: "text",
|
|
12
|
+
value: node.text,
|
|
13
|
+
...enrich,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=wrapNode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrapNode.js","sourceRoot":"","sources":["../../../../../../../packages/cast-util-from-slate/src/lib/utils/wrapNode.ts"],"names":[],"mappings":";;AAYA,4BAmBC;AA/BD,mCAAsC;AAYtC,SAAgB,QAAQ,CAAC,IAAe,EAAE,MAAe;IACvD,MAAM,MAAM,GAAsC,IAAA,aAAI,EAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,KAAK,MAAM,CAGvF,CAAC;IAEL,OAAO,IAAA,eAAM,EACX,MAAM,EACN,CAAC,GAAkB,EAAE,GAAG,EAAE,EAAE,CAC1B,CAAC;QACC,IAAI,EAAE,GAAG;QACT,QAAQ,EAAE,CAAC,GAAG,CAAC;KAChB,CAA6B,EAChC;QACE,IAAI,EAAE,MAAM;QACZ,KAAK,EAAE,IAAI,CAAC,IAAI;QAChB,GAAG,MAAM;KACV,CACF,CAAC;AACJ,CAAC"}
|