@platecms/delta-cast-util-to-hast 1.0.0 → 1.3.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.
Files changed (69) hide show
  1. package/README.md +1 -1
  2. package/package.json +5 -7
  3. package/src/{index.d.ts → index.ts} +1 -0
  4. package/src/lib/elements/blockquote.ts +13 -0
  5. package/src/lib/elements/bold.ts +13 -0
  6. package/src/lib/elements/code.ts +13 -0
  7. package/src/lib/elements/contentValue.ts +15 -0
  8. package/src/lib/elements/heading.ts +13 -0
  9. package/src/lib/elements/{index.d.ts → index.ts} +16 -1
  10. package/src/lib/elements/italic.ts +13 -0
  11. package/src/lib/elements/link.ts +20 -0
  12. package/src/lib/elements/lists.ts +22 -0
  13. package/src/lib/elements/paragraph.ts +13 -0
  14. package/src/lib/elements/strikehrough.ts +13 -0
  15. package/src/lib/elements/text.ts +9 -0
  16. package/src/lib/elements/underline.ts +13 -0
  17. package/src/lib/index.spec.ts +404 -0
  18. package/src/lib/index.ts +78 -0
  19. package/src/lib/transformers/index.ts +25 -0
  20. package/src/lib/utils/index.ts +19 -0
  21. package/src/index.js +0 -9
  22. package/src/index.js.map +0 -1
  23. package/src/lib/elements/blockquote.d.ts +0 -4
  24. package/src/lib/elements/blockquote.js +0 -13
  25. package/src/lib/elements/blockquote.js.map +0 -1
  26. package/src/lib/elements/bold.d.ts +0 -4
  27. package/src/lib/elements/bold.js +0 -13
  28. package/src/lib/elements/bold.js.map +0 -1
  29. package/src/lib/elements/code.d.ts +0 -4
  30. package/src/lib/elements/code.js +0 -13
  31. package/src/lib/elements/code.js.map +0 -1
  32. package/src/lib/elements/contentValue.d.ts +0 -4
  33. package/src/lib/elements/contentValue.js +0 -14
  34. package/src/lib/elements/contentValue.js.map +0 -1
  35. package/src/lib/elements/heading.d.ts +0 -4
  36. package/src/lib/elements/heading.js +0 -13
  37. package/src/lib/elements/heading.js.map +0 -1
  38. package/src/lib/elements/index.js +0 -29
  39. package/src/lib/elements/index.js.map +0 -1
  40. package/src/lib/elements/italic.d.ts +0 -4
  41. package/src/lib/elements/italic.js +0 -13
  42. package/src/lib/elements/italic.js.map +0 -1
  43. package/src/lib/elements/link.d.ts +0 -4
  44. package/src/lib/elements/link.js +0 -19
  45. package/src/lib/elements/link.js.map +0 -1
  46. package/src/lib/elements/lists.d.ts +0 -5
  47. package/src/lib/elements/lists.js +0 -22
  48. package/src/lib/elements/lists.js.map +0 -1
  49. package/src/lib/elements/paragraph.d.ts +0 -4
  50. package/src/lib/elements/paragraph.js +0 -13
  51. package/src/lib/elements/paragraph.js.map +0 -1
  52. package/src/lib/elements/strikehrough.d.ts +0 -4
  53. package/src/lib/elements/strikehrough.js +0 -13
  54. package/src/lib/elements/strikehrough.js.map +0 -1
  55. package/src/lib/elements/text.d.ts +0 -3
  56. package/src/lib/elements/text.js +0 -10
  57. package/src/lib/elements/text.js.map +0 -1
  58. package/src/lib/elements/underline.d.ts +0 -4
  59. package/src/lib/elements/underline.js +0 -13
  60. package/src/lib/elements/underline.js.map +0 -1
  61. package/src/lib/index.d.ts +0 -5
  62. package/src/lib/index.js +0 -57
  63. package/src/lib/index.js.map +0 -1
  64. package/src/lib/transformers/index.d.ts +0 -5
  65. package/src/lib/transformers/index.js +0 -22
  66. package/src/lib/transformers/index.js.map +0 -1
  67. package/src/lib/utils/index.d.ts +0 -3
  68. package/src/lib/utils/index.js +0 -18
  69. package/src/lib/utils/index.js.map +0 -1
package/README.md CHANGED
@@ -89,7 +89,7 @@ const cast: Root = {
89
89
  },
90
90
  ]
91
91
  },
92
- prn: "prn:plate-dev:a7734468-b894-4191-83b1-7d218f8c9922:cxc:content-value:b2119c94-24e6-4358-b557-c705dcd19d1d"
92
+ prn: "prn:plate-dev:a7734468-b894-4191-83b1-7d218f8c9922:cms:content-value:b2119c94-24e6-4358-b557-c705dcd19d1d"
93
93
  },
94
94
  ],
95
95
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platecms/delta-cast-util-to-hast",
3
- "version": "1.0.0",
3
+ "version": "1.3.0",
4
4
  "description": "Utility to convert CAST to HAST syntax tree.",
5
5
  "license": "UNLICENSED",
6
6
  "publishConfig": {
@@ -17,14 +17,12 @@
17
17
  "src/**/*"
18
18
  ],
19
19
  "dependencies": {
20
- "@platecms/delta-cast": "1.0.0",
21
- "@platecms/delta-plate-resource-notation": "1.0.0",
20
+ "@platecms/delta-cast": "1.3.0",
21
+ "@platecms/delta-plate-resource-notation": "1.3.0",
22
22
  "class-transformer": "0.5.1",
23
23
  "hast": "1.0.0",
24
24
  "reflect-metadata": "0.2.2",
25
25
  "tslib": "2.8.1",
26
- "zwitch": "2.0.4",
27
- "lodash": "4.17.21"
28
- },
29
- "type": "commonjs"
26
+ "zwitch": "2.0.4"
27
+ }
30
28
  }
@@ -1,3 +1,4 @@
1
1
  export { toHast } from "./lib";
2
+
2
3
  export * from "./lib/utils";
3
4
  export * from "./lib/transformers";
@@ -0,0 +1,13 @@
1
+ import { Blockquote } from "@platecms/delta-cast";
2
+ import { Element as HastElement } from "hast";
3
+ import { all } from "..";
4
+ import { NodeTransformers } from "../transformers";
5
+
6
+ export function blockquote(node: Blockquote, state: object = {}, transform: NodeTransformers = {}): HastElement {
7
+ return {
8
+ type: "element",
9
+ tagName: "blockquote",
10
+ properties: {},
11
+ children: all(node.children, state, transform),
12
+ };
13
+ }
@@ -0,0 +1,13 @@
1
+ import { Bold } from "@platecms/delta-cast";
2
+ import { Element as HastElement } from "hast";
3
+ import { all } from "..";
4
+ import { NodeTransformers } from "../transformers";
5
+
6
+ export function bold(node: Bold, state: object = {}, transform: NodeTransformers = {}): HastElement {
7
+ return {
8
+ type: "element",
9
+ tagName: "strong",
10
+ properties: {},
11
+ children: all(node.children, state, transform),
12
+ };
13
+ }
@@ -0,0 +1,13 @@
1
+ import { all } from "..";
2
+ import { NodeTransformers } from "../transformers";
3
+ import { Code } from "@platecms/delta-cast";
4
+ import { Element as HastElement, Text } from "hast";
5
+
6
+ export function code(node: Code, state: object = {}, transform: NodeTransformers = {}): HastElement {
7
+ return {
8
+ type: "element",
9
+ tagName: "code",
10
+ properties: { lang: node.lang },
11
+ children: all((node as Code & { children: Text[] }).children, state, transform),
12
+ };
13
+ }
@@ -0,0 +1,15 @@
1
+ import { InterpolatedContentValue } from "@platecms/delta-cast";
2
+ import { Element as HastElement } from "hast";
3
+ import { all } from "..";
4
+ import { NodeTransformers } from "../transformers";
5
+
6
+ export function contentValue(node: InterpolatedContentValue, state: object, transform: NodeTransformers): HastElement {
7
+ const transformedNode = transform.contentValue ? transform.contentValue(node) : node;
8
+
9
+ return {
10
+ type: "element",
11
+ tagName: "span",
12
+ properties: {},
13
+ children: transformedNode.value ? all(transformedNode.value.children, state, transform) : [],
14
+ };
15
+ }
@@ -0,0 +1,13 @@
1
+ import { Heading } from "@platecms/delta-cast";
2
+ import { all } from "..";
3
+ import { NodeTransformers } from "../transformers";
4
+ import { Element as HastElement } from "hast";
5
+
6
+ export function heading(node: Heading, state: object = {}, transform: NodeTransformers = {}): HastElement {
7
+ return {
8
+ type: "element",
9
+ tagName: `h${node.level}`,
10
+ properties: {},
11
+ children: all(node.children, state, transform),
12
+ };
13
+ }
@@ -10,4 +10,19 @@ import { strikehrough } from "./strikehrough";
10
10
  import { text } from "./text";
11
11
  import { blockquote } from "./blockquote";
12
12
  import { link } from "./link";
13
- export { heading, link, list, listItem, code, contentValue, paragraph, bold, italic, underline, strikehrough, text, blockquote, };
13
+
14
+ export {
15
+ heading,
16
+ link,
17
+ list,
18
+ listItem,
19
+ code,
20
+ contentValue,
21
+ paragraph,
22
+ bold,
23
+ italic,
24
+ underline,
25
+ strikehrough,
26
+ text,
27
+ blockquote,
28
+ };
@@ -0,0 +1,13 @@
1
+ import { Italic } from "@platecms/delta-cast";
2
+ import { Element as HastElement } from "hast";
3
+ import { all } from "..";
4
+ import { NodeTransformers } from "../transformers";
5
+
6
+ export function italic(node: Italic, state: object = {}, transform: NodeTransformers = {}): HastElement {
7
+ return {
8
+ type: "element",
9
+ tagName: "em",
10
+ properties: {},
11
+ children: all(node.children, state, transform),
12
+ };
13
+ }
@@ -0,0 +1,20 @@
1
+ import { ExternalLink, InternalLink } from "@platecms/delta-cast";
2
+ import { Element as HastElement } from "hast";
3
+ import { all } from "..";
4
+ import { NodeTransformers } from "../transformers";
5
+
6
+ export function link(node: ExternalLink | InternalLink, state: object, transform: NodeTransformers): HastElement {
7
+ if (node.url === undefined) {
8
+ throw new Error("Cannot construct a link with an undefined URL.");
9
+ }
10
+
11
+ return {
12
+ type: "element",
13
+ tagName: "a",
14
+ properties: {
15
+ href: node.url,
16
+ target: node.target,
17
+ },
18
+ children: all(node.children, state, transform),
19
+ };
20
+ }
@@ -0,0 +1,22 @@
1
+ import { List, ListItem } from "@platecms/delta-cast";
2
+ import { Element as HastElement } from "hast";
3
+ import { all } from "..";
4
+ import { NodeTransformers } from "../transformers";
5
+
6
+ export function list(node: List, state: object = {}, transform: NodeTransformers = {}): HastElement {
7
+ return {
8
+ type: "element",
9
+ tagName: node.ordered ? "ol" : "ul",
10
+ properties: {},
11
+ children: all(node.children, state, transform),
12
+ };
13
+ }
14
+
15
+ export function listItem(node: ListItem, state: object = {}, transform: NodeTransformers = {}): HastElement {
16
+ return {
17
+ type: "element",
18
+ tagName: "li",
19
+ properties: {},
20
+ children: all(node.children, state, transform),
21
+ };
22
+ }
@@ -0,0 +1,13 @@
1
+ import { Paragraph } from "@platecms/delta-cast";
2
+ import { Element as HastElement } from "hast";
3
+ import { all } from "..";
4
+ import { NodeTransformers } from "../transformers";
5
+
6
+ export function paragraph(node: Paragraph, state: object = {}, transform: NodeTransformers = {}): HastElement {
7
+ return {
8
+ type: "element",
9
+ tagName: "p",
10
+ properties: {},
11
+ children: all(node.children, state, transform),
12
+ };
13
+ }
@@ -0,0 +1,13 @@
1
+ import { Strikethrough } from "@platecms/delta-cast";
2
+ import { Element as HastElement } from "hast";
3
+ import { all } from "..";
4
+ import { NodeTransformers } from "../transformers";
5
+
6
+ export function strikehrough(node: Strikethrough, state: object = {}, transform: NodeTransformers = {}): HastElement {
7
+ return {
8
+ type: "element",
9
+ tagName: "s",
10
+ properties: {},
11
+ children: all(node.children, state, transform),
12
+ };
13
+ }
@@ -0,0 +1,9 @@
1
+ import { Text } from "@platecms/delta-cast";
2
+ import { Text as HastText } from "hast";
3
+
4
+ export function text(node: Text): HastText {
5
+ return {
6
+ type: "text",
7
+ value: node.value,
8
+ };
9
+ }
@@ -0,0 +1,13 @@
1
+ import { Underline } from "@platecms/delta-cast";
2
+ import { Element as HastElement } from "hast";
3
+ import { all } from "..";
4
+ import { NodeTransformers } from "../transformers";
5
+
6
+ export function underline(node: Underline, state: object = {}, transform: NodeTransformers = {}): HastElement {
7
+ return {
8
+ type: "element",
9
+ tagName: "u",
10
+ properties: {},
11
+ children: all(node.children, state, transform),
12
+ };
13
+ }
@@ -0,0 +1,404 @@
1
+ import { toHast } from "../index";
2
+ import { c } from "@platecms/delta-castscript";
3
+ import { Root } from "@platecms/delta-cast";
4
+
5
+ describe("single nodes", () => {
6
+ it("should convert a paragraph from cast to hast", () => {
7
+ // Arrange
8
+ const cast: Root = c("root", [c("paragraph", "Hello, world!")]);
9
+
10
+ // Act
11
+ const expected = toHast(cast);
12
+
13
+ // Assert
14
+ expect(expected).toEqual({
15
+ type: "root",
16
+ children: [
17
+ {
18
+ type: "element",
19
+ tagName: "p",
20
+ properties: {},
21
+ children: [
22
+ {
23
+ type: "text",
24
+ value: "Hello, world!",
25
+ },
26
+ ],
27
+ },
28
+ ],
29
+ });
30
+ });
31
+
32
+ it("should convert a heading from cast to hast", () => {
33
+ // Arrange
34
+ const cast: Root = c("root", [c("heading", "Hello, world!", { level: 1 })]);
35
+
36
+ // Act
37
+ const expected = toHast(cast);
38
+
39
+ // Assert
40
+ expect(expected).toEqual({
41
+ type: "root",
42
+ children: [
43
+ {
44
+ type: "element",
45
+ tagName: "h1",
46
+ properties: {},
47
+ children: [
48
+ {
49
+ type: "text",
50
+ value: "Hello, world!",
51
+ },
52
+ ],
53
+ },
54
+ ],
55
+ });
56
+ });
57
+
58
+ it("should convert a list from cast to hast", () => {
59
+ // Arrange
60
+ const cast: Root = c("root", [
61
+ c("list", { ordered: true }, [c("listItem", "Hello, world!"), c("listItem", "Hello, world!")]),
62
+ ]);
63
+
64
+ // Act
65
+ const expected = toHast(cast);
66
+
67
+ // Assert
68
+ expect(expected).toEqual({
69
+ type: "root",
70
+ children: [
71
+ {
72
+ type: "element",
73
+ tagName: "ol",
74
+ properties: {},
75
+ children: [
76
+ {
77
+ type: "element",
78
+ tagName: "li",
79
+ properties: {},
80
+ children: [
81
+ {
82
+ type: "text",
83
+ value: "Hello, world!",
84
+ },
85
+ ],
86
+ },
87
+ {
88
+ type: "element",
89
+ tagName: "li",
90
+ properties: {},
91
+ children: [
92
+ {
93
+ type: "text",
94
+ value: "Hello, world!",
95
+ },
96
+ ],
97
+ },
98
+ ],
99
+ },
100
+ ],
101
+ });
102
+ });
103
+
104
+ it("should convert a blockquote from from cast to hast", () => {
105
+ // Arrange
106
+ const cast: Root = c("root", [c("blockquote", "Hello, world!")]);
107
+
108
+ // Act
109
+ const expected = toHast(cast);
110
+
111
+ // Assert
112
+ expect(expected).toEqual({
113
+ type: "root",
114
+ children: [
115
+ {
116
+ type: "element",
117
+ tagName: "blockquote",
118
+ properties: {},
119
+ children: [
120
+ {
121
+ type: "text",
122
+ value: "Hello, world!",
123
+ },
124
+ ],
125
+ },
126
+ ],
127
+ });
128
+ });
129
+
130
+ it("should convert code from cast to hast", () => {
131
+ // Arrange
132
+ const cast: Root = c("root", [c("code", [c("text", "Hello, world!")])]);
133
+
134
+ // Act
135
+ const expected = toHast(cast);
136
+
137
+ // Assert
138
+ expect(expected).toEqual({
139
+ type: "root",
140
+ children: [
141
+ {
142
+ type: "element",
143
+ tagName: "code",
144
+ properties: {},
145
+ children: [
146
+ {
147
+ type: "text",
148
+ value: "Hello, world!",
149
+ },
150
+ ],
151
+ },
152
+ ],
153
+ });
154
+ });
155
+
156
+ it("should convert a content value from cast to hast", () => {
157
+ // Arrange
158
+ const cast: Root = c("root", [
159
+ c("contentValue", {
160
+ prn: "prn:content-value",
161
+ value: c("root", [c("paragraph", "Hello, world!")]),
162
+ }),
163
+ ]);
164
+
165
+ // Act
166
+ const expected = toHast(cast);
167
+
168
+ // Assert
169
+ expect(expected).toEqual({
170
+ type: "root",
171
+ children: [
172
+ {
173
+ type: "element",
174
+ properties: {},
175
+ tagName: "span",
176
+ children: [
177
+ {
178
+ type: "text",
179
+ value: "Hello, world!",
180
+ },
181
+ ],
182
+ },
183
+ ],
184
+ });
185
+ });
186
+
187
+ describe("links", () => {
188
+ it("should convert an external link from cast to hast", () => {
189
+ // Arrange
190
+ const cast: Root = c("root", [
191
+ c("externalLink", { url: "https://www.google.com" }, [c("text", "Hello, world!")]),
192
+ ]);
193
+
194
+ // Act
195
+ const result = toHast(cast);
196
+
197
+ // Assert
198
+ expect(result).toEqual({
199
+ type: "root",
200
+ children: [
201
+ {
202
+ type: "element",
203
+ tagName: "a",
204
+ properties: {
205
+ href: "https://www.google.com",
206
+ },
207
+ children: [{ type: "text", value: "Hello, world!" }],
208
+ },
209
+ ],
210
+ });
211
+ });
212
+
213
+ it("should convert an external link with target from cast to hast", () => {
214
+ // Arrange
215
+ const cast: Root = c("root", [
216
+ c("externalLink", { url: "https://www.google.com", target: "blank" }, [c("text", "Hello, world!")]),
217
+ ]);
218
+
219
+ // Act
220
+ const result = toHast(cast);
221
+
222
+ // Assert
223
+ expect(result).toEqual({
224
+ type: "root",
225
+ children: [
226
+ {
227
+ type: "element",
228
+ tagName: "a",
229
+ properties: {
230
+ href: "https://www.google.com",
231
+ target: "blank",
232
+ },
233
+ children: [{ type: "text", value: "Hello, world!" }],
234
+ },
235
+ ],
236
+ });
237
+ });
238
+
239
+ it("should convert an internal link from cast to hast", () => {
240
+ // Arrange
241
+ const cast: Root = c("root", [
242
+ c("internalLink", { url: "https://www.google.com", prn: "prn:path-part:path-part" }, [
243
+ c("text", "Hello, world!"),
244
+ ]),
245
+ ]);
246
+
247
+ // Act
248
+ const result = toHast(cast);
249
+
250
+ // Assert
251
+ expect(result).toEqual({
252
+ type: "root",
253
+ children: [
254
+ {
255
+ type: "element",
256
+ tagName: "a",
257
+ properties: { href: "https://www.google.com" },
258
+ children: [{ type: "text", value: "Hello, world!" }],
259
+ },
260
+ ],
261
+ });
262
+ });
263
+
264
+ it("should convert an internal anchor link from cast to hast", () => {
265
+ // Arrange
266
+ const cast: Root = c("root", [
267
+ c("internalLink", { url: "https://www.google.com#anchor-name", prn: "prn:grid-placement:grid-placement" }, [
268
+ c("text", "Hello, world!"),
269
+ ]),
270
+ ]);
271
+
272
+ // Act
273
+ const result = toHast(cast);
274
+
275
+ // Assert
276
+ expect(result).toEqual({
277
+ type: "root",
278
+ children: [
279
+ {
280
+ type: "element",
281
+ tagName: "a",
282
+ properties: { href: "https://www.google.com#anchor-name" },
283
+ children: [{ type: "text", value: "Hello, world!" }],
284
+ },
285
+ ],
286
+ });
287
+ });
288
+ });
289
+ });
290
+
291
+ describe("nested nodes", () => {
292
+ it("should convert a nested tree from cast to hast", () => {
293
+ // Arrange
294
+ const tree: Root = c("root", [
295
+ c("list", { ordered: false }, [c("listItem", [c("bold", "Hello, world!")])]),
296
+ c("paragraph", [
297
+ c("text", "Hello, world!"),
298
+ c("contentValue", "", {
299
+ prn: "prn:content-value:content-value",
300
+ value: c("root", [c("paragraph", "Hello, world!")]),
301
+ }),
302
+ ]),
303
+ ]);
304
+
305
+ // Act
306
+ const expected = toHast(tree);
307
+
308
+ // Assert
309
+ expect(expected).toEqual({
310
+ type: "root",
311
+ children: [
312
+ {
313
+ type: "element",
314
+ tagName: "ul",
315
+ properties: {},
316
+ children: [
317
+ {
318
+ type: "element",
319
+ tagName: "li",
320
+ properties: {},
321
+ children: [
322
+ {
323
+ type: "element",
324
+ tagName: "strong",
325
+ properties: {},
326
+ children: [
327
+ {
328
+ type: "text",
329
+ value: "Hello, world!",
330
+ },
331
+ ],
332
+ },
333
+ ],
334
+ },
335
+ ],
336
+ },
337
+ {
338
+ type: "element",
339
+ tagName: "p",
340
+ properties: {},
341
+ children: [
342
+ {
343
+ type: "text",
344
+ value: "Hello, world!",
345
+ },
346
+ {
347
+ type: "element",
348
+ properties: {},
349
+ tagName: "span",
350
+ children: [
351
+ {
352
+ type: "text",
353
+ value: "Hello, world!",
354
+ },
355
+ ],
356
+ },
357
+ ],
358
+ },
359
+ ],
360
+ });
361
+ });
362
+ });
363
+
364
+ describe("wihtout transformations", () => {
365
+ describe("contentValue", () => {
366
+ it("should convert a content value from cast to hast", () => {
367
+ // Arrange
368
+ const cast: Root = c("root", [
369
+ c("contentValue", {
370
+ prn: "prn:content-value",
371
+ value: c("root", [c("paragraph", "Hello, world!")]),
372
+ }),
373
+ ]);
374
+
375
+ // Act
376
+ const expected = toHast(cast, {});
377
+
378
+ // Assert
379
+ expect(expected).toEqual({
380
+ type: "root",
381
+ children: [
382
+ {
383
+ type: "element",
384
+ properties: {},
385
+ tagName: "span",
386
+ children: [
387
+ {
388
+ type: "element",
389
+ properties: {},
390
+ tagName: "p",
391
+ children: [
392
+ {
393
+ type: "text",
394
+ value: "Hello, world!",
395
+ },
396
+ ],
397
+ },
398
+ ],
399
+ },
400
+ ],
401
+ });
402
+ });
403
+ });
404
+ });