dtable-ui-component 0.1.61 → 0.1.65

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.
@@ -0,0 +1,645 @@
1
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
2
+ import { Value, Document, Block, Inline, Text, Mark, Leaf } from 'slate';
3
+
4
+ var unified = require('./unified');
5
+
6
+ var markdown = require('remark-parse');
7
+
8
+ var definitions = require('mdast-util-definitions');
9
+
10
+ var processor = unified().use(markdown, {
11
+ commonmark: true
12
+ }); // transform code value: a function used to tranform code value of markdown format to slate document
13
+
14
+ function mdCodeNodeToSlate(codeValue) {
15
+ // statement an array : filled width code value splited by '\n'
16
+ var codeValueArr = codeValue.split('\n');
17
+ var slateTextObj, slateBlockObj; // map codeValueArr item to slate document 'code_line'
18
+
19
+ var slate_code_arr = codeValueArr.map(function (text) {
20
+ // transform string to slateText object
21
+ slateTextObj = Text.create({
22
+ text: text
23
+ }); // transform slateText to Block object
24
+
25
+ slateBlockObj = Block.create({
26
+ nodes: [slateTextObj],
27
+ type: 'code_line'
28
+ });
29
+ return slateBlockObj;
30
+ });
31
+ return slate_code_arr;
32
+ } // deserialize mdTable to SlateNode
33
+
34
+
35
+ function mdTableToSlateTable(tableNode, opts) {
36
+ // get align array
37
+ var tableAlignArr = tableNode.align; // get all table_rows, return:array
38
+
39
+ var tableRows = tableNode.children; // slate table_row Node into it
40
+
41
+ var tableRowsArr = []; // traverse table_rows
42
+
43
+ for (var rowIndex = 0; rowIndex < tableRows.length; rowIndex++) {
44
+ // slate table_cell Node into it
45
+ var tableCellsArr = [];
46
+ /*
47
+ * traverse every table_cells of an table_rows,
48
+ * the length of every table_rows is equal to tableAlign array
49
+ * */
50
+
51
+ for (var columnsIndex = 0; columnsIndex < tableAlignArr.length; columnsIndex++) {
52
+ // get table_cell and tranlate it to slate Node: table_cell
53
+ var tableCell = tableRows[rowIndex].children[columnsIndex];
54
+
55
+ if (!tableCell) {
56
+ tableCell = {
57
+ type: 'tableCell',
58
+ children: [{
59
+ type: 'text',
60
+ value: ''
61
+ }]
62
+ };
63
+ }
64
+
65
+ var children = parseChildren(tableCell, opts);
66
+ tableCell = Block.create({
67
+ type: 'table_cell',
68
+ nodes: [Block.create({
69
+ type: 'paragraph',
70
+ nodes: children
71
+ })],
72
+ data: {
73
+ align: tableAlignArr[columnsIndex]
74
+ }
75
+ });
76
+ tableCellsArr.push(tableCell);
77
+ } // push add table_rows into tableRowsArr
78
+
79
+
80
+ tableRowsArr.push(Block.create({
81
+ type: 'table_row',
82
+ nodes: tableCellsArr
83
+ }));
84
+ }
85
+
86
+ return tableRowsArr;
87
+ }
88
+
89
+ function mdImageToSlate(node, opts) {
90
+ var _DOMParser$parseFromS = new DOMParser().parseFromString(node.value, 'text/html'),
91
+ body = _DOMParser$parseFromS.body;
92
+
93
+ var img = body.firstChild;
94
+ var data = {};
95
+ data['src'] = img.getAttribute('src');
96
+
97
+ if (!isNaN(img.getAttribute('width')) && img.getAttribute('width') > 0) {
98
+ data['width'] = img.getAttribute('width');
99
+ }
100
+
101
+ if (!isNaN(img.getAttribute('height')) && img.getAttribute('height') > 0) {
102
+ data['height'] = img.getAttribute('height');
103
+ }
104
+
105
+ if (img.getAttribute('alt')) {
106
+ data['alt'] = img.getAttribute('alt');
107
+ }
108
+
109
+ if (img.getAttribute('title')) {
110
+ data['title'] = img.getAttribute('title');
111
+ }
112
+
113
+ if (data['src']) {
114
+ return Inline.create({
115
+ type: 'image',
116
+ data: data
117
+ });
118
+ }
119
+ }
120
+
121
+ function _applyMark(childNodeOrNodes, mark) {
122
+ if (childNodeOrNodes instanceof Array) {
123
+ return childNodeOrNodes.map(function (item) {
124
+ return _applyMark(item, mark);
125
+ });
126
+ } else if (childNodeOrNodes.object == 'text') {
127
+ var length = childNodeOrNodes.text.length;
128
+ return childNodeOrNodes.addMark(0, length, mark);
129
+ } else {
130
+ return childNodeOrNodes;
131
+ }
132
+ }
133
+
134
+ function addChildNodeOrNodes(children, childNodeOrNodes) {
135
+ if (childNodeOrNodes instanceof Array) {
136
+ childNodeOrNodes.map(function (item) {
137
+ return children.push(item);
138
+ });
139
+ } else {
140
+ if (childNodeOrNodes !== undefined) children.push(childNodeOrNodes);
141
+ }
142
+ }
143
+ /*
144
+ parse a mark node.
145
+ Always return an array.
146
+
147
+ Example
148
+ *fdsfd __fdsf__ fdf*
149
+ */
150
+
151
+
152
+ function parseMark(node, markString, opts) {
153
+ var mark = Mark.create({
154
+ type: markString
155
+ });
156
+ var children = [];
157
+
158
+ var _iterator = _createForOfIteratorHelper(node.children),
159
+ _step;
160
+
161
+ try {
162
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
163
+ var child = _step.value;
164
+
165
+ var childNodeOrNodes = _nodeToSlate(child, opts);
166
+
167
+ childNodeOrNodes = _applyMark(childNodeOrNodes, mark);
168
+ addChildNodeOrNodes(children, childNodeOrNodes);
169
+ }
170
+ } catch (err) {
171
+ _iterator.e(err);
172
+ } finally {
173
+ _iterator.f();
174
+ }
175
+
176
+ return children;
177
+ }
178
+
179
+ function parseChildren(node, opts) {
180
+ var children = [];
181
+
182
+ var _iterator2 = _createForOfIteratorHelper(node.children),
183
+ _step2;
184
+
185
+ try {
186
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
187
+ var child = _step2.value;
188
+
189
+ var ret = _nodeToSlate(child, opts);
190
+
191
+ addChildNodeOrNodes(children, ret);
192
+ }
193
+ } catch (err) {
194
+ _iterator2.e(err);
195
+ } finally {
196
+ _iterator2.f();
197
+ }
198
+
199
+ return children;
200
+ }
201
+
202
+ function _nodeToSlate(node, opts) {
203
+ var children = [];
204
+ var mark;
205
+ var definition = opts.definition;
206
+
207
+ switch (node.type) {
208
+ case 'heading':
209
+ var header_string;
210
+ children = parseChildren(node, opts);
211
+
212
+ switch (node.depth) {
213
+ case 1:
214
+ header_string = 'header_one';
215
+ break;
216
+
217
+ case 2:
218
+ header_string = 'header_two';
219
+ break;
220
+
221
+ case 3:
222
+ header_string = 'header_three';
223
+ break;
224
+
225
+ case 4:
226
+ header_string = 'header_four';
227
+ break;
228
+
229
+ case 5:
230
+ header_string = 'header_five';
231
+ break;
232
+
233
+ case 6:
234
+ header_string = 'header_six';
235
+ break;
236
+
237
+ default:
238
+ console.log('Invalid depth: ' + node.depth);
239
+ header_string = 'header_one';
240
+ break;
241
+ }
242
+
243
+ return Block.create({
244
+ type: header_string,
245
+ nodes: children
246
+ });
247
+
248
+ case 'paragraph':
249
+ children = parseChildren(node, opts);
250
+ return Block.create({
251
+ type: 'paragraph',
252
+ nodes: children
253
+ });
254
+
255
+ case 'blockquote':
256
+ children = parseChildren(node, opts);
257
+ return Block.create({
258
+ type: 'blockquote',
259
+ nodes: children
260
+ });
261
+
262
+ case 'list':
263
+ opts.loose = node.loose;
264
+ children = parseChildren(node, opts);
265
+
266
+ if (node.ordered) {
267
+ return Block.create({
268
+ type: 'ordered_list',
269
+ nodes: children
270
+ });
271
+ } else {
272
+ return Block.create({
273
+ type: 'unordered_list',
274
+ nodes: children
275
+ });
276
+ }
277
+
278
+ case 'listItem':
279
+ children = parseChildren(node, opts);
280
+
281
+ if (children.length === 0) {
282
+ /**
283
+ * if children is an empty array push an empy paragaph to it
284
+ */
285
+ children.push(Block.create({
286
+ type: 'paragraph',
287
+ nodes: [Text.create('')]
288
+ }));
289
+ }
290
+
291
+ var data = {};
292
+
293
+ if (node.checked !== null) {
294
+ data.checked = node.checked;
295
+ }
296
+
297
+ return Block.create({
298
+ type: 'list_item',
299
+ data: data,
300
+ nodes: children
301
+ });
302
+
303
+ case 'code':
304
+ var data = {};
305
+
306
+ if (node.lang) {
307
+ data.syntax = node.lang;
308
+ }
309
+
310
+ var slate_code_arr = mdCodeNodeToSlate(node.value);
311
+ return Block.create({
312
+ type: 'code_block',
313
+ data: data,
314
+ nodes: slate_code_arr
315
+ });
316
+
317
+ case 'strong':
318
+ return parseMark(node, 'BOLD', opts);
319
+
320
+ case 'emphasis':
321
+ return parseMark(node, 'ITALIC', opts);
322
+
323
+ case 'inlineCode':
324
+ // Inline code need to be handled differently
325
+ var mark = Mark.create({
326
+ type: 'CODE'
327
+ });
328
+ return Text.create({
329
+ text: node.value,
330
+ marks: [mark]
331
+ });
332
+
333
+ case 'text':
334
+ // A plain text in markdown
335
+ // text is the botton node in Markdown AST
336
+ return Text.create({
337
+ text: node.value
338
+ });
339
+
340
+ case 'break':
341
+ return Text.create({
342
+ text: '\n'
343
+ });
344
+
345
+ case 'thematicBreak':
346
+ return Block.create({
347
+ type: 'hr'
348
+ });
349
+
350
+ case 'table':
351
+ // get all children of table by mdTableToSlateTable
352
+ children = mdTableToSlateTable(node, opts);
353
+ return Block.create({
354
+ type: 'table',
355
+ nodes: children,
356
+ data: {
357
+ align: node.align
358
+ }
359
+ });
360
+
361
+ case 'html':
362
+ if (node.value.slice(0, 4).toLowerCase() === '<img') {
363
+ return mdImageToSlate(node, opts);
364
+ } else {
365
+ var child = Text.create({
366
+ text: ''
367
+ });
368
+ children.push(child);
369
+ return Inline.create({
370
+ type: 'html_block',
371
+ data: {
372
+ html: node.value
373
+ },
374
+ nodes: children
375
+ });
376
+ }
377
+
378
+ case 'link':
379
+ children = parseChildren(node, opts);
380
+ var data = {
381
+ href: node.url
382
+ };
383
+
384
+ if (node.title) {
385
+ data.title = node.title;
386
+ }
387
+
388
+ return Inline.create({
389
+ type: 'link',
390
+ data: data,
391
+ nodes: children
392
+ });
393
+
394
+ case 'image':
395
+ var data = {
396
+ src: node.url
397
+ };
398
+
399
+ if (node.title) {
400
+ data.title = node.title;
401
+ }
402
+
403
+ if (node.alt) {
404
+ data.alt = node.alt;
405
+ }
406
+
407
+ if (node.width) {
408
+ data.width = node.width;
409
+ }
410
+
411
+ if (node.height) {
412
+ data.height = node.height;
413
+ }
414
+
415
+ return Inline.create({
416
+ type: 'image',
417
+ data: data
418
+ });
419
+
420
+ case 'linkReference':
421
+ children = parseChildren(node, opts);
422
+ var def = definition(node.identifier);
423
+ var data = {};
424
+
425
+ if (def) {
426
+ data.href = def.url;
427
+
428
+ if (def.title) {
429
+ data.title = def.title;
430
+ }
431
+
432
+ return Inline.create({
433
+ type: 'link',
434
+ data: data,
435
+ nodes: children
436
+ });
437
+ } else {
438
+ return Text.create({
439
+ text: '[' + node.identifier + ']'
440
+ });
441
+ }
442
+
443
+ case 'imageReference':
444
+ var def = definition(node.identifier);
445
+ var data = {};
446
+
447
+ if (def) {
448
+ data.src = def.url;
449
+
450
+ if (def.title) {
451
+ data.title = def.title;
452
+ }
453
+
454
+ if (node.alt) {
455
+ data.alt = node.alt;
456
+ }
457
+
458
+ return Inline.create({
459
+ type: 'image',
460
+ data: data
461
+ });
462
+ } else {
463
+ return Text.create({
464
+ text: '![' + node.alt + ']'
465
+ });
466
+ }
467
+
468
+ case 'definition':
469
+ return;
470
+
471
+ default:
472
+ console.log('unrecognized type: ' + node.type);
473
+ return;
474
+ }
475
+ }
476
+
477
+ function fixValue(nodes) {
478
+ var c = [];
479
+
480
+ var _iterator3 = _createForOfIteratorHelper(nodes),
481
+ _step3;
482
+
483
+ try {
484
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
485
+ var node = _step3.value;
486
+
487
+ if (node.object === 'inline') {
488
+ if (node.type === 'image') {
489
+ var data = {};
490
+ data['src'] = node.data.get('src');
491
+ data['width'] = node.data.get('width') || null;
492
+ data['height'] = node.data.get('height') || null;
493
+
494
+ if (node.data.get('alt')) {
495
+ data['alt'] = node.data.get('alt');
496
+ }
497
+
498
+ if (node.data.get('title')) {
499
+ data['title'] = node.data.get('title');
500
+ }
501
+
502
+ var nodeNew = Block.create({
503
+ type: 'paragraph',
504
+ nodes: [Text.create(''), Inline.create({
505
+ type: 'image',
506
+ data: data
507
+ }), Text.create('')]
508
+ });
509
+ c.push(nodeNew);
510
+ } else {
511
+ c.push(node);
512
+ }
513
+ } else if (node.object === 'block') {
514
+ if (node.type === 'html_block') {
515
+ // convert from inline to block
516
+ node = Block.create({
517
+ type: 'html_block',
518
+ data: {
519
+ html: node.get('data').get('html')
520
+ },
521
+ nodes: node.nodes
522
+ });
523
+ c.push(node);
524
+ } else if (node.type === 'code_block') {
525
+ c.push(node);
526
+ } else if (node.type === 'paragraph') {
527
+ (function () {
528
+ var nodes = [];
529
+ var textLeaves = [];
530
+
531
+ if (node.nodes.size === 0) {
532
+ nodes.push(Text.create(''));
533
+ } else {
534
+ node.nodes.forEach(function (node, index, nodesList) {
535
+ if (node.object === 'text') {
536
+ textLeaves.push(Leaf.create({
537
+ text: node.text,
538
+ marks: node.leaves.get(0).get('marks')
539
+ }));
540
+
541
+ if (index === nodesList.size - 1) {
542
+ nodes.push(Text.create({
543
+ leaves: textLeaves
544
+ }));
545
+ textLeaves = [];
546
+ }
547
+ } else {
548
+ nodes.push(Text.create({
549
+ leaves: textLeaves
550
+ }));
551
+ textLeaves = [];
552
+ var nextNode = nodesList.get(index + 1);
553
+ nodes.push(node);
554
+
555
+ if (!nextNode || nextNode.object !== 'text') {
556
+ nodes.push(Text.create(''));
557
+ }
558
+ }
559
+ });
560
+ }
561
+
562
+ c.push(Block.create({
563
+ type: 'paragraph',
564
+ nodes: nodes
565
+ }));
566
+ })();
567
+ } else {
568
+ var newNodes = fixValue(node.nodes);
569
+ c.push(Block.create({
570
+ type: node.type,
571
+ nodes: newNodes,
572
+ data: node.data
573
+ }));
574
+ }
575
+ } else if (node.object === 'text') {
576
+ c.push(node);
577
+ }
578
+ }
579
+ } catch (err) {
580
+ _iterator3.e(err);
581
+ } finally {
582
+ _iterator3.f();
583
+ }
584
+
585
+ return c;
586
+ }
587
+
588
+ function deserialize(content) {
589
+ var root = processor.parse(content);
590
+ var definition = definitions(root);
591
+ var nodes = [];
592
+
593
+ var _iterator4 = _createForOfIteratorHelper(root.children),
594
+ _step4;
595
+
596
+ try {
597
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
598
+ var _child = _step4.value;
599
+ addChildNodeOrNodes(nodes, _nodeToSlate(_child, {
600
+ definition: definition
601
+ }));
602
+ }
603
+ } catch (err) {
604
+ _iterator4.e(err);
605
+ } finally {
606
+ _iterator4.f();
607
+ }
608
+
609
+ if (nodes.length === 0) {
610
+ // add default paragraph
611
+ var child = Text.create({
612
+ text: ''
613
+ });
614
+ var node = Block.create({
615
+ type: 'paragraph',
616
+ nodes: [child]
617
+ });
618
+ nodes.push(node);
619
+ } // handle image and html_block
620
+
621
+
622
+ var c;
623
+ c = fixValue(nodes);
624
+ var firstBlockType = c[0].type;
625
+
626
+ if (firstBlockType === 'table' || firstBlockType === 'code_block' || firstBlockType === 'blockquote') {
627
+ c.unshift(Block.create({
628
+ type: 'paragraph',
629
+ nodes: [Text.create('')]
630
+ }));
631
+ }
632
+
633
+ var document = Document.create({
634
+ nodes: c
635
+ }); // normalize will cost a lot time, for 40K document, it costs 1.5 seconds
636
+
637
+ var value = Value.create({
638
+ document: document
639
+ }, {
640
+ normalize: false
641
+ });
642
+ return value;
643
+ }
644
+
645
+ export { deserialize };
@@ -0,0 +1,4 @@
1
+ import { serialize } from './serialize';
2
+ import { deserialize } from './deserialize';
3
+ import { processor, processorGetAST } from './markdown2html';
4
+ export { serialize, deserialize, processor, processorGetAST };
@@ -0,0 +1,43 @@
1
+ import gh from 'hast-util-sanitize/lib/github.json';
2
+ import unified from './unified';
3
+ import markdown from 'remark-parse';
4
+ import slug from 'remark-slug';
5
+ import breaks from 'remark-breaks';
6
+ import remark2rehype from 'remark-rehype';
7
+ import format from 'rehype-format';
8
+ import raw from 'rehype-raw';
9
+ import xtend from 'xtend';
10
+ import toHTML from 'hast-util-to-html';
11
+ import sanitize from 'hast-util-sanitize';
12
+ import deepmerge from 'deepmerge';
13
+
14
+ function stringify(config) {
15
+ var settings = xtend(config, this.data('settings'));
16
+ var schema = deepmerge(gh, {
17
+ 'attributes': {
18
+ 'input': ['type'],
19
+ 'li': ['className'],
20
+ 'code': ['className']
21
+ },
22
+ 'tagNames': ['input', 'code']
23
+ });
24
+ this.Compiler = compiler;
25
+
26
+ function compiler(tree) {
27
+ // use sanity to remove dangerous html, the default is
28
+ // GitHub style sanitation
29
+ var hast = sanitize(tree, schema);
30
+ return toHTML(hast, settings);
31
+ }
32
+ } // markdown -> mdast -> html AST -> html
33
+
34
+
35
+ var processor = unified().use(markdown, {
36
+ commonmark: true
37
+ }).use(breaks).use(slug).use(remark2rehype, {
38
+ allowDangerousHTML: true
39
+ }).use(raw).use(format).use(stringify);
40
+ var processorGetAST = unified().use(markdown, {
41
+ commonmark: true
42
+ }).use(slug);
43
+ export { processor, processorGetAST };