1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 | const ToSlateVisitor = require('./ToSlateVisitor');
|
17 |
|
18 | const slateToCiceroMarkDom = require('./slateToCiceroMarkDom');
|
19 |
|
20 | const CiceroMarkTransformer = require('@accordproject/markdown-cicero').CiceroMarkTransformer;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | class SlateTransformer {
|
27 | |
28 |
|
29 |
|
30 | constructor() {
|
31 | this.ciceroMarkTransformer = new CiceroMarkTransformer();
|
32 | this.serializer = this.ciceroMarkTransformer.getSerializer();
|
33 | }
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | fromCiceroMark(input) {
|
42 | if (!input.getType) {
|
43 | input = this.serializer.fromJSON(input);
|
44 | }
|
45 |
|
46 | const CLAUSE = 'clause';
|
47 | const parameters = {};
|
48 | parameters.result = {};
|
49 | parameters.marks = [];
|
50 | const visitor = new ToSlateVisitor();
|
51 | input.accept(visitor, parameters);
|
52 | const result = {
|
53 | object: 'value',
|
54 | document: parameters.result
|
55 | };
|
56 | const paragraphSpaceNodeJSON = {
|
57 | object: 'block',
|
58 | type: 'paragraph',
|
59 | data: {},
|
60 | nodes: [{
|
61 | object: 'text',
|
62 | text: '',
|
63 | marks: []
|
64 | }]
|
65 | };
|
66 |
|
67 | if (result.document.nodes.length > 1) {
|
68 | let newArray = [];
|
69 |
|
70 | for (let i = 0; i <= result.document.nodes.length - 1; i++) {
|
71 | newArray.push(result.document.nodes[i]);
|
72 |
|
73 | if (result.document.nodes[i].type === CLAUSE && result.document.nodes[i + 1] && result.document.nodes[i + 1].type === CLAUSE) {
|
74 | newArray.push(paragraphSpaceNodeJSON);
|
75 | }
|
76 | }
|
77 |
|
78 | result.document.nodes = newArray;
|
79 | }
|
80 |
|
81 |
|
82 | const lastNodeType = result.document.nodes[result.document.nodes.length - 1] ? result.document.nodes[result.document.nodes.length - 1].type : null;
|
83 |
|
84 | if (lastNodeType === CLAUSE) {
|
85 | result.document.nodes.push(paragraphSpaceNodeJSON);
|
86 | }
|
87 |
|
88 | return result;
|
89 | }
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | toCiceroMark(value) {
|
100 | let format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'concerto';
|
101 | const json = slateToCiceroMarkDom(value.document);
|
102 |
|
103 | if (format === 'concerto') {
|
104 | return this.serializer.fromJSON(json);
|
105 | } else {
|
106 | return json;
|
107 | }
|
108 | }
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | toMarkdown(value, options) {
|
118 | const ciceroMark = this.toCiceroMark(value, 'json');
|
119 | return this.ciceroMarkTransformer.toMarkdown(ciceroMark, options);
|
120 | }
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | fromMarkdown(markdown) {
|
129 | const ciceroMarkDom = this.ciceroMarkTransformer.fromMarkdown(markdown, 'json');
|
130 |
|
131 | return this.fromCiceroMark(ciceroMarkDom);
|
132 | }
|
133 |
|
134 | }
|
135 |
|
136 | module.exports = SlateTransformer; |
\ | No newline at end of file |