@platecms/delta-cast 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,53 @@
1
+ # CAST (Content Abstract Syntax Tree)
2
+
3
+ This package contains the CAST (Content Abstract Syntax Tree) schema and tools for working with it. CAST is a schema
4
+ that adheres to the [**Unified collective**](https://github.com/unifiedjs/unified)
5
+
6
+ # Features
7
+
8
+ ## Schema
9
+
10
+ The schema is defined in [schema.ts](src/lib/schemas/schema.ts), and is rendered into JSON Schema format
11
+ in [schema.json](src/lib/schemas/schema.json).
12
+
13
+ ## Utils
14
+
15
+ To work with CAST documents, we implement the following utitlities:
16
+
17
+ - `InvalidCastError`: An error that is thrown when a CAST document is invalid.
18
+ - `validateCast`: Validates any input against the schema. Throws `InvalidCastError` if the input is invalid.
19
+
20
+ ### **WIP** fromSlate (Or some other editor)
21
+
22
+ As Delta uses [Slate](https://www.slatejs.org) as a wysiwyg editor, we need to convert ~Slate~ documents to CAST
23
+ documents, in order to process them in Delta.
24
+
25
+ ### **WIP** toSlate
26
+
27
+ As Delta uses Slate as a wysiwyg editor, we need to convert CAST documents to ~Slate~ documents, in order to display
28
+ them in the editor.
29
+
30
+ ### **WIP** toHast
31
+
32
+ To display CAST documents in a web browser, we need to convert them to [HAST](https://github.com/syntax-tree/hast). From
33
+ a HAST tree, we can then use [**hast-util-to-html**](https://github.com/syntax-tree/hast-util-to-html) to create an HTML
34
+ string or [**hast-util-to-dom**](https://github.com/syntax-tree/hast-util-to-dom) to create a DOM tree.
35
+
36
+ Example usage of cast-utils-to-hast to render a CAST tree to a HTML string:
37
+
38
+ ```ts
39
+ const castTree = {
40
+ type: 'root',
41
+ children: [
42
+ {
43
+ type: 'paragraph',
44
+ children: [
45
+ { type: 'text', value: 'Hello, world!' }
46
+ ]
47
+ }
48
+ ]
49
+ }
50
+
51
+ const hast = toHast(castTree);
52
+ const htmlString = toHtml(hast);
53
+ ```
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@platecms/delta-cast",
3
+ "description": "Package containing the definition of CAST",
4
+ "version": "0.1.0",
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",
14
+ "main": "./src/index.js",
15
+ "types": "./src/index.d.ts",
16
+ "files": [
17
+ "src/**/*"
18
+ ],
19
+ "dependencies": {
20
+ "class-transformer": "0.5.1",
21
+ "reflect-metadata": "0.2.2",
22
+ "tslib": "2.8.1"
23
+ },
24
+ "peerDependencies": {
25
+ "@platecms/delta-plate-resource-notation": "*"
26
+ },
27
+ "type": "commonjs"
28
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./lib/schemas/schema";
2
+ export * from "./lib/validate-cast";
3
+ export * from "./lib/invalid-cast.error";
package/src/index.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./lib/schemas/schema"), exports);
5
+ tslib_1.__exportStar(require("./lib/validate-cast"), exports);
6
+ tslib_1.__exportStar(require("./lib/invalid-cast.error"), exports);
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/cast/src/index.ts"],"names":[],"mappings":";;;AAAA,+DAAqC;AACrC,8DAAoC;AACpC,mEAAyC"}
@@ -0,0 +1,3 @@
1
+ export declare class InvalidCastError extends Error {
2
+ constructor(message?: string);
3
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InvalidCastError = void 0;
4
+ class InvalidCastError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = this.constructor.name;
8
+ }
9
+ }
10
+ exports.InvalidCastError = InvalidCastError;
11
+ //# sourceMappingURL=invalid-cast.error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invalid-cast.error.js","sourceRoot":"","sources":["../../../../../../packages/cast/src/lib/invalid-cast.error.ts"],"names":[],"mappings":";;;AAAA,MAAa,gBAAiB,SAAQ,KAAK;IACzC,YAAmB,OAAgB;QACjC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;IACpC,CAAC;CACF;AALD,4CAKC"}
@@ -0,0 +1,90 @@
1
+ import { Node, Literal as UnistLiteral, Parent as UnistParent } from "unist";
2
+ export type Content = BlockContent | InlineContent;
3
+ export type BlockContent = BlockContentMap[keyof BlockContentMap];
4
+ export type InlineContent = InlineContentMap[keyof InlineContentMap];
5
+ export interface BlockContentMap {
6
+ paragraph: Paragraph;
7
+ heading: Heading;
8
+ list: List;
9
+ listItem: ListItem;
10
+ blockquote: Blockquote;
11
+ code: Code;
12
+ }
13
+ export interface InlineContentMap {
14
+ text: Text;
15
+ bold: Bold;
16
+ italic: Italic;
17
+ underline: Underline;
18
+ strikethrough: Strikethrough;
19
+ inlineCode: InlineCode;
20
+ highlight: Highlight;
21
+ contentValue: InterpolatedContentValue;
22
+ }
23
+ export interface Parent extends UnistParent {
24
+ children: Content[];
25
+ }
26
+ export interface Literal extends UnistLiteral {
27
+ value: string;
28
+ }
29
+ export interface Root extends Parent {
30
+ type: "root";
31
+ }
32
+ export interface Paragraph extends Parent {
33
+ type: "paragraph";
34
+ children: InlineContent[];
35
+ }
36
+ export interface Heading extends Parent {
37
+ type: "heading";
38
+ level: 1 | 2 | 3 | 4 | 5 | 6;
39
+ children: InlineContent[];
40
+ }
41
+ export interface List extends Parent {
42
+ type: "list";
43
+ children: ListItem[];
44
+ ordered: boolean;
45
+ start?: number;
46
+ }
47
+ export interface ListItem extends Parent {
48
+ type: "listItem";
49
+ children: InlineContent[];
50
+ }
51
+ export interface Blockquote extends Parent {
52
+ type: "blockquote";
53
+ children: InlineContent[];
54
+ }
55
+ export interface Code extends Parent {
56
+ type: "code";
57
+ lang?: string;
58
+ children: Text[];
59
+ }
60
+ export interface Text extends Literal {
61
+ type: "text";
62
+ }
63
+ export interface Bold extends Parent {
64
+ type: "bold";
65
+ children: InlineContent[];
66
+ }
67
+ export interface Italic extends Parent {
68
+ type: "italic";
69
+ children: InlineContent[];
70
+ }
71
+ export interface Underline extends Parent {
72
+ type: "underline";
73
+ children: InlineContent[];
74
+ }
75
+ export interface Strikethrough extends Parent {
76
+ type: "strikethrough";
77
+ children: InlineContent[];
78
+ }
79
+ export interface InlineCode extends Literal {
80
+ type: "inlineCode";
81
+ }
82
+ export interface Highlight extends Parent {
83
+ type: "highlight";
84
+ children: InlineContent[];
85
+ }
86
+ export interface InterpolatedContentValue extends Node {
87
+ type: "contentValue";
88
+ prn: string;
89
+ value?: Root;
90
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../../../../../packages/cast/src/lib/schemas/schema.ts"],"names":[],"mappings":""}
@@ -0,0 +1,513 @@
1
+ {
2
+ "$ref": "#/definitions/Root",
3
+ "$schema": "http://json-schema.org/draft-07/schema#",
4
+ "definitions": {
5
+ "Blockquote": {
6
+ "properties": {
7
+ "children": {
8
+ "description": "The children of a blockquote are inline content.",
9
+ "items": {
10
+ "$ref": "#/definitions/InlineContent"
11
+ },
12
+ "type": "array"
13
+ },
14
+ "data": {
15
+ "$ref": "#/definitions/Data",
16
+ "description": "Info from the ecosystem."
17
+ },
18
+ "position": {
19
+ "$ref": "#/definitions/Position",
20
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
21
+ },
22
+ "type": {
23
+ "const": "blockquote",
24
+ "description": "Node type.",
25
+ "type": "string"
26
+ }
27
+ },
28
+ "type": "object"
29
+ },
30
+ "Bold": {
31
+ "properties": {
32
+ "children": {
33
+ "description": "The children of a bold node are inline content.",
34
+ "items": {
35
+ "$ref": "#/definitions/InlineContent"
36
+ },
37
+ "type": "array"
38
+ },
39
+ "data": {
40
+ "$ref": "#/definitions/Data",
41
+ "description": "Info from the ecosystem."
42
+ },
43
+ "position": {
44
+ "$ref": "#/definitions/Position",
45
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
46
+ },
47
+ "type": {
48
+ "const": "bold",
49
+ "description": "Node type.",
50
+ "type": "string"
51
+ }
52
+ },
53
+ "type": "object"
54
+ },
55
+ "Code": {
56
+ "properties": {
57
+ "children": {
58
+ "description": "List of children.",
59
+ "items": {
60
+ "$ref": "#/definitions/Text"
61
+ },
62
+ "type": "array"
63
+ },
64
+ "data": {
65
+ "$ref": "#/definitions/Data",
66
+ "description": "Info from the ecosystem."
67
+ },
68
+ "lang": {
69
+ "description": "The language of the code block.",
70
+ "type": "string"
71
+ },
72
+ "position": {
73
+ "$ref": "#/definitions/Position",
74
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
75
+ },
76
+ "type": {
77
+ "const": "code",
78
+ "description": "Node type.",
79
+ "type": "string"
80
+ }
81
+ },
82
+ "type": "object"
83
+ },
84
+ "Content": {
85
+ "anyOf": [
86
+ {
87
+ "$ref": "#/definitions/Code"
88
+ },
89
+ {
90
+ "$ref": "#/definitions/Blockquote"
91
+ },
92
+ {
93
+ "$ref": "#/definitions/Paragraph"
94
+ },
95
+ {
96
+ "$ref": "#/definitions/Heading"
97
+ },
98
+ {
99
+ "$ref": "#/definitions/List"
100
+ },
101
+ {
102
+ "$ref": "#/definitions/ListItem"
103
+ },
104
+ {
105
+ "$ref": "#/definitions/Bold"
106
+ },
107
+ {
108
+ "$ref": "#/definitions/Text"
109
+ },
110
+ {
111
+ "$ref": "#/definitions/Highlight"
112
+ },
113
+ {
114
+ "$ref": "#/definitions/Italic"
115
+ },
116
+ {
117
+ "$ref": "#/definitions/Underline"
118
+ },
119
+ {
120
+ "$ref": "#/definitions/Strikethrough"
121
+ },
122
+ {
123
+ "$ref": "#/definitions/InlineCode"
124
+ },
125
+ {
126
+ "$ref": "#/definitions/InterpolatedContentValue"
127
+ }
128
+ ]
129
+ },
130
+ "Data": {
131
+ "description": "Info associated with nodes by the ecosystem.\n\nThis space is guaranteed to never be specified by unist or specifications\nimplementing unist.\nBut you can use it in utilities and plugins to store data.\n\nThis type can be augmented to register custom data.\nFor example:\n\n```ts\ndeclare module 'unist' {\n interface Data {\n // `someNode.data.myId` is typed as `number | undefined`\n myId?: number | undefined\n }\n}\n```",
132
+ "type": "object"
133
+ },
134
+ "Heading": {
135
+ "properties": {
136
+ "children": {
137
+ "description": "The children of a heading are inline content.",
138
+ "items": {
139
+ "$ref": "#/definitions/InlineContent"
140
+ },
141
+ "type": "array"
142
+ },
143
+ "data": {
144
+ "$ref": "#/definitions/Data",
145
+ "description": "Info from the ecosystem."
146
+ },
147
+ "level": {
148
+ "description": "The level of a heading is a number between 1 and 6.",
149
+ "enum": [
150
+ 1,
151
+ 2,
152
+ 3,
153
+ 4,
154
+ 5,
155
+ 6
156
+ ],
157
+ "type": "number"
158
+ },
159
+ "position": {
160
+ "$ref": "#/definitions/Position",
161
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
162
+ },
163
+ "type": {
164
+ "const": "heading",
165
+ "description": "Node type.",
166
+ "type": "string"
167
+ }
168
+ },
169
+ "type": "object"
170
+ },
171
+ "Highlight": {
172
+ "properties": {
173
+ "children": {
174
+ "description": "The children of a highlight node are inline content",
175
+ "items": {
176
+ "$ref": "#/definitions/InlineContent"
177
+ },
178
+ "type": "array"
179
+ },
180
+ "data": {
181
+ "$ref": "#/definitions/Data",
182
+ "description": "Info from the ecosystem."
183
+ },
184
+ "position": {
185
+ "$ref": "#/definitions/Position",
186
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
187
+ },
188
+ "type": {
189
+ "const": "highlight",
190
+ "description": "Node type.",
191
+ "type": "string"
192
+ }
193
+ },
194
+ "type": "object"
195
+ },
196
+ "InlineCode": {
197
+ "properties": {
198
+ "data": {
199
+ "$ref": "#/definitions/Data",
200
+ "description": "Info from the ecosystem."
201
+ },
202
+ "position": {
203
+ "$ref": "#/definitions/Position",
204
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
205
+ },
206
+ "type": {
207
+ "const": "inlineCode",
208
+ "description": "Node type.",
209
+ "type": "string"
210
+ },
211
+ "value": {
212
+ "description": "Plain value.",
213
+ "type": "string"
214
+ }
215
+ },
216
+ "type": "object"
217
+ },
218
+ "InlineContent": {
219
+ "anyOf": [
220
+ {
221
+ "$ref": "#/definitions/Bold"
222
+ },
223
+ {
224
+ "$ref": "#/definitions/Text"
225
+ },
226
+ {
227
+ "$ref": "#/definitions/Highlight"
228
+ },
229
+ {
230
+ "$ref": "#/definitions/Italic"
231
+ },
232
+ {
233
+ "$ref": "#/definitions/Underline"
234
+ },
235
+ {
236
+ "$ref": "#/definitions/Strikethrough"
237
+ },
238
+ {
239
+ "$ref": "#/definitions/InlineCode"
240
+ },
241
+ {
242
+ "$ref": "#/definitions/InterpolatedContentValue"
243
+ }
244
+ ]
245
+ },
246
+ "InterpolatedContentValue": {
247
+ "description": "Represents an interpolated Content Value. Interpolated values are values that are stored elsewhere in the database\nand inserted into the document at request-time.",
248
+ "properties": {
249
+ "data": {
250
+ "$ref": "#/definitions/Data",
251
+ "description": "Info from the ecosystem."
252
+ },
253
+ "position": {
254
+ "$ref": "#/definitions/Position",
255
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
256
+ },
257
+ "prn": {
258
+ "description": "The PRN (identifier) of the referenced Content Value.",
259
+ "type": "string"
260
+ },
261
+ "type": {
262
+ "const": "contentValue",
263
+ "description": "Node type.",
264
+ "type": "string"
265
+ },
266
+ "value": {
267
+ "$ref": "#/definitions/Root",
268
+ "description": "The nested CAST tree of the referenced Content Value."
269
+ }
270
+ },
271
+ "type": "object"
272
+ },
273
+ "Italic": {
274
+ "properties": {
275
+ "children": {
276
+ "description": "The children of an italic node are inline content.",
277
+ "items": {
278
+ "$ref": "#/definitions/InlineContent"
279
+ },
280
+ "type": "array"
281
+ },
282
+ "data": {
283
+ "$ref": "#/definitions/Data",
284
+ "description": "Info from the ecosystem."
285
+ },
286
+ "position": {
287
+ "$ref": "#/definitions/Position",
288
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
289
+ },
290
+ "type": {
291
+ "const": "italic",
292
+ "description": "Node type.",
293
+ "type": "string"
294
+ }
295
+ },
296
+ "type": "object"
297
+ },
298
+ "List": {
299
+ "properties": {
300
+ "children": {
301
+ "description": "List of children.",
302
+ "items": {
303
+ "$ref": "#/definitions/ListItem"
304
+ },
305
+ "type": "array"
306
+ },
307
+ "data": {
308
+ "$ref": "#/definitions/Data",
309
+ "description": "Info from the ecosystem."
310
+ },
311
+ "ordered": {
312
+ "description": "Whether the list is ordered or not.",
313
+ "type": "boolean"
314
+ },
315
+ "position": {
316
+ "$ref": "#/definitions/Position",
317
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
318
+ },
319
+ "start": {
320
+ "description": "The start of an ordered list.",
321
+ "type": "number"
322
+ },
323
+ "type": {
324
+ "const": "list",
325
+ "description": "Node type.",
326
+ "type": "string"
327
+ }
328
+ },
329
+ "type": "object"
330
+ },
331
+ "ListItem": {
332
+ "properties": {
333
+ "children": {
334
+ "description": "The children of a list item are inline content.",
335
+ "items": {
336
+ "$ref": "#/definitions/InlineContent"
337
+ },
338
+ "type": "array"
339
+ },
340
+ "data": {
341
+ "$ref": "#/definitions/Data",
342
+ "description": "Info from the ecosystem."
343
+ },
344
+ "position": {
345
+ "$ref": "#/definitions/Position",
346
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
347
+ },
348
+ "type": {
349
+ "const": "listItem",
350
+ "description": "Node type.",
351
+ "type": "string"
352
+ }
353
+ },
354
+ "type": "object"
355
+ },
356
+ "Paragraph": {
357
+ "properties": {
358
+ "children": {
359
+ "description": "The children of a paragraph are inline content.",
360
+ "items": {
361
+ "$ref": "#/definitions/InlineContent"
362
+ },
363
+ "type": "array"
364
+ },
365
+ "data": {
366
+ "$ref": "#/definitions/Data",
367
+ "description": "Info from the ecosystem."
368
+ },
369
+ "position": {
370
+ "$ref": "#/definitions/Position",
371
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
372
+ },
373
+ "type": {
374
+ "const": "paragraph",
375
+ "description": "Node type.",
376
+ "type": "string"
377
+ }
378
+ },
379
+ "type": "object"
380
+ },
381
+ "Point": {
382
+ "description": "One place in a source file.",
383
+ "properties": {
384
+ "column": {
385
+ "description": "Column in a source file (1-indexed integer).",
386
+ "type": "number"
387
+ },
388
+ "line": {
389
+ "description": "Line in a source file (1-indexed integer).",
390
+ "type": "number"
391
+ },
392
+ "offset": {
393
+ "description": "Character in a source file (0-indexed integer).",
394
+ "type": "number"
395
+ }
396
+ },
397
+ "type": "object"
398
+ },
399
+ "Position": {
400
+ "description": "Position of a node in a source document.\n\nA position is a range between two points.",
401
+ "properties": {
402
+ "end": {
403
+ "$ref": "#/definitions/Point",
404
+ "description": "Place of the first character after the parsed source region."
405
+ },
406
+ "start": {
407
+ "$ref": "#/definitions/Point",
408
+ "description": "Place of the first character of the parsed source region."
409
+ }
410
+ },
411
+ "type": "object"
412
+ },
413
+ "Root": {
414
+ "description": "The root node of a CAST document. The unist tree starts here. Each CAST document has one root node.\n\n```json\n{\n \"type\": \"root\",\n \"children\": [\n ...\n ]\n}",
415
+ "properties": {
416
+ "children": {
417
+ "description": "List of children.",
418
+ "items": {
419
+ "$ref": "#/definitions/Content"
420
+ },
421
+ "type": "array"
422
+ },
423
+ "data": {
424
+ "$ref": "#/definitions/Data",
425
+ "description": "Info from the ecosystem."
426
+ },
427
+ "position": {
428
+ "$ref": "#/definitions/Position",
429
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
430
+ },
431
+ "type": {
432
+ "const": "root",
433
+ "description": "Node type.",
434
+ "type": "string"
435
+ }
436
+ },
437
+ "type": "object"
438
+ },
439
+ "Strikethrough": {
440
+ "properties": {
441
+ "children": {
442
+ "description": "The children of a strikethrough node are inline content.",
443
+ "items": {
444
+ "$ref": "#/definitions/InlineContent"
445
+ },
446
+ "type": "array"
447
+ },
448
+ "data": {
449
+ "$ref": "#/definitions/Data",
450
+ "description": "Info from the ecosystem."
451
+ },
452
+ "position": {
453
+ "$ref": "#/definitions/Position",
454
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
455
+ },
456
+ "type": {
457
+ "const": "strikethrough",
458
+ "description": "Node type.",
459
+ "type": "string"
460
+ }
461
+ },
462
+ "type": "object"
463
+ },
464
+ "Text": {
465
+ "properties": {
466
+ "data": {
467
+ "$ref": "#/definitions/Data",
468
+ "description": "Info from the ecosystem."
469
+ },
470
+ "position": {
471
+ "$ref": "#/definitions/Position",
472
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
473
+ },
474
+ "type": {
475
+ "const": "text",
476
+ "description": "Node type.",
477
+ "type": "string"
478
+ },
479
+ "value": {
480
+ "description": "Plain value.",
481
+ "type": "string"
482
+ }
483
+ },
484
+ "type": "object"
485
+ },
486
+ "Underline": {
487
+ "properties": {
488
+ "children": {
489
+ "description": "The children of an underline node are inline content.",
490
+ "items": {
491
+ "$ref": "#/definitions/InlineContent"
492
+ },
493
+ "type": "array"
494
+ },
495
+ "data": {
496
+ "$ref": "#/definitions/Data",
497
+ "description": "Info from the ecosystem."
498
+ },
499
+ "position": {
500
+ "$ref": "#/definitions/Position",
501
+ "description": "Position of a node in a source document.\n\nNodes that are generated (not in the original source document) must not\nhave a position."
502
+ },
503
+ "type": {
504
+ "const": "underline",
505
+ "description": "Node type.",
506
+ "type": "string"
507
+ }
508
+ },
509
+ "type": "object"
510
+ }
511
+ },
512
+ "description": "The root node of a CAST document. The unist tree starts here. Each CAST document has one root node.\n\n```json\n{\n \"type\": \"root\",\n \"children\": [\n ...\n ]\n}"
513
+ }
@@ -0,0 +1 @@
1
+ export declare function validateCast(value: unknown): void;
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateCast = validateCast;
4
+ const invalid_cast_error_1 = require("./invalid-cast.error");
5
+ const delta_plate_resource_notation_1 = require("@platecms/delta-plate-resource-notation");
6
+ function assertNonNullObject(value) {
7
+ if (typeof value !== "object" || value == null) {
8
+ throw new invalid_cast_error_1.InvalidCastError(`Invalid object`);
9
+ }
10
+ }
11
+ function assertValidParent(value) {
12
+ assertNonNullObject(value);
13
+ const parent = value;
14
+ if (!Array.isArray(parent.children)) {
15
+ throw new invalid_cast_error_1.InvalidCastError("Invalid parent: children is not an array");
16
+ }
17
+ parent.children.forEach(assertValidContent);
18
+ }
19
+ function assertValidLiteral(value) {
20
+ assertNonNullObject(value);
21
+ const literal = value;
22
+ if (typeof literal.value !== "string") {
23
+ throw new invalid_cast_error_1.InvalidCastError("Invalid literal: value is not a string");
24
+ }
25
+ }
26
+ function assertValidHeading(value) {
27
+ assertNonNullObject(value);
28
+ const list = value;
29
+ if (typeof list.level !== "number" || list.level < 1 || list.level > 6) {
30
+ throw new invalid_cast_error_1.InvalidCastError("Invalid heading");
31
+ }
32
+ assertValidParent(list);
33
+ }
34
+ function assertValidList(value) {
35
+ assertNonNullObject(value);
36
+ const list = value;
37
+ if (typeof list.ordered !== "boolean" || (list.start != null && typeof list.start !== "number")) {
38
+ throw new invalid_cast_error_1.InvalidCastError("Invalid list");
39
+ }
40
+ assertValidParent(list);
41
+ }
42
+ function assertValidCode(value) {
43
+ assertNonNullObject(value);
44
+ const code = value;
45
+ if (code.lang != null && typeof code.lang !== "string") {
46
+ throw new invalid_cast_error_1.InvalidCastError("Invalid code");
47
+ }
48
+ assertValidParent(code);
49
+ }
50
+ function assertValidInterpolatedContentValue(value) {
51
+ assertNonNullObject(value);
52
+ const contentValue = value;
53
+ try {
54
+ delta_plate_resource_notation_1.PRN.fromString(contentValue.prn);
55
+ }
56
+ catch (error) {
57
+ if (error instanceof delta_plate_resource_notation_1.InvalidPrnError) {
58
+ throw new invalid_cast_error_1.InvalidCastError(`Invalid contentValue: invalid PRN ${contentValue.prn}`);
59
+ }
60
+ throw error;
61
+ }
62
+ }
63
+ function assertValidContent(value) {
64
+ assertNonNullObject(value);
65
+ const content = value;
66
+ switch (content.type) {
67
+ case "paragraph":
68
+ case "listItem":
69
+ case "blockquote":
70
+ case "bold":
71
+ case "italic":
72
+ case "underline":
73
+ case "strikethrough":
74
+ case "highlight":
75
+ assertValidParent(content);
76
+ break;
77
+ case "heading":
78
+ assertValidHeading(content);
79
+ break;
80
+ case "text":
81
+ case "inlineCode":
82
+ assertValidLiteral(content);
83
+ break;
84
+ case "list":
85
+ assertValidList(content);
86
+ break;
87
+ case "code":
88
+ assertValidCode(content);
89
+ break;
90
+ case "contentValue":
91
+ assertValidInterpolatedContentValue(content);
92
+ break;
93
+ default:
94
+ throw new invalid_cast_error_1.InvalidCastError("Invalid Content type.");
95
+ }
96
+ }
97
+ function validateCast(value) {
98
+ assertNonNullObject(value);
99
+ const root = value;
100
+ if (root.type !== "root") {
101
+ throw new invalid_cast_error_1.InvalidCastError("Invalid root: type is not 'root'");
102
+ }
103
+ if (!Array.isArray(root.children)) {
104
+ throw new invalid_cast_error_1.InvalidCastError("Invalid root: children is not an array");
105
+ }
106
+ root.children.forEach(assertValidContent);
107
+ }
108
+ //# sourceMappingURL=validate-cast.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate-cast.js","sourceRoot":"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