UNPKG

11.8 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17const { NS_PREFIX_CommonMarkModel } = require('@accordproject/markdown-common').CommonMarkModel;
18const { NS_PREFIX_CiceroMarkModel } = require('@accordproject/markdown-cicero').CiceroMarkModel;
19const { NS_PREFIX_TemplateMarkModel } = require('@accordproject/markdown-template').TemplateMarkModel;
20
21/**
22 * Removes nodes if they are an empty paragraph
23 * @param {*} input - the current result of slateToCiceroMarkDom
24 * @returns {*} the final result of slateToCiceroMarkDom
25 */
26function removeEmptyParagraphs(input) {
27 let nodesWithoutBlankParagraphs = [];
28 input.nodes.forEach(node => {
29 if (node.$class === `${NS_PREFIX_CommonMarkModel}Paragraph` &&
30 node.nodes.length === 1 &&
31 node.nodes[0].$class === `${NS_PREFIX_CommonMarkModel}Text` &&
32 node.nodes[0].text === '') {
33 return;
34 }
35 nodesWithoutBlankParagraphs.push(node);
36 });
37 input.nodes = nodesWithoutBlankParagraphs;
38 return input;
39}
40
41/**
42 * Gather the text for the node
43 * @param {*} input - the current slate node
44 * @returns {string} the text contained in the slate node
45 */
46function getText (input) {
47 let result = '';
48
49 if (input.type === 'paragraph') {
50 result += '\n';
51 }
52
53 if (input.text) {
54 result += input.text;
55 }
56 if (input.children) {
57 input.children.forEach(node => {
58 result += getText(node);
59 });
60 }
61 return result;
62}
63
64/**
65 * Quote strings
66 * @param {string} value - the string
67 * @return {string} the quoted string
68 */
69function quoteString(value) {
70 return '"' + value + '"';
71}
72
73/**
74 * Handles marks
75 * @param {*} slateNode the slate node
76 * @param {*} newNode the new node
77 * @returns {*} the final ast node
78 */
79function handleMarks(slateNode,newNode) {
80 let strong = null;
81 let emph = null;
82 let result = newNode;
83
84 const isBold = slateNode.bold;
85 const isItalic = slateNode.italic;
86
87 if (isBold) {
88 strong = {$class : `${NS_PREFIX_CommonMarkModel}Strong`, nodes: []};
89 }
90
91 if (isItalic) {
92 emph = {$class : `${NS_PREFIX_CommonMarkModel}Emph`, nodes: []};
93 }
94
95 if(strong) {
96 strong.nodes.push(result);
97 result = strong;
98 }
99 if(emph) {
100 emph.nodes.push(result);
101 result = emph;
102 }
103
104 return result;
105}
106
107/**
108 * Handles a text node
109 * @param {*} node the slate text node
110 * @returns {*} the ast node
111 */
112function handleText(node) {
113 let result = null;
114 const isCode = node.code;
115 if (node.object === 'text' && node.text === '') { return null; }
116 if (isCode) {
117 result = {$class : `${NS_PREFIX_CommonMarkModel}Code`, text: node.text};
118 } else {
119 result = {$class : `${NS_PREFIX_CommonMarkModel}Text`, text : node.text};
120 }
121
122 return handleMarks(node,result);
123}
124
125/**
126 * Handles a variable node
127 * @param {*} node the slate variable node
128 * @returns {*} the ast node
129 */
130function handleVariable(node) {
131 let result = null;
132
133 const textNode = node.children[0]; // inlines always contain a single text node
134 node.children = []; // Reset the children for the inline to avoid recursion
135
136 const data = node.data;
137
138 const baseName = Object.prototype.hasOwnProperty.call(data,'format') ? 'FormattedVariable' : (Object.prototype.hasOwnProperty.call(data,'enumValues') ? 'EnumVariable' : 'Variable');
139 const className = `${NS_PREFIX_CiceroMarkModel}${baseName}`;
140
141 result = {
142 $class : className,
143 name : data.name,
144 value : textNode.text
145 };
146
147 if (Object.prototype.hasOwnProperty.call(data,'format')) {
148 result.format = data.format;
149 }
150 if (node.data.identifiedBy) {
151 result.identifiedBy = node.data.identifiedBy;
152 result.value = quoteString(result.value); // XXX Is this safe?
153 }
154 if (Object.prototype.hasOwnProperty.call(data,'enumValues')) {
155 result.enumValues = data.enumValues;
156 }
157 if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
158 result.elementType = data.elementType;
159 if (result.elementType === 'String') {
160 result.value = quoteString(result.value);
161 }
162 }
163 if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
164 result.decorators = data.decorators;
165 }
166
167 return handleMarks(node,result);
168}
169
170/**
171 * Handles a variable definition node
172 * @param {*} node the slate variable node
173 * @returns {*} the ast node
174 */
175function handleVariableDefinition(node) {
176 let result = null;
177
178 node.children = []; // Reset the children for the inline to avoid recursion
179
180 const data = node.data;
181
182 const baseName = Object.prototype.hasOwnProperty.call(data,'format') ? 'FormattedVariableDefinition' : (Object.prototype.hasOwnProperty.call(data,'enumValues') ? 'EnumVariableDefinition' : 'VariableDefinition');
183 const className = `${NS_PREFIX_TemplateMarkModel}${baseName}`;
184
185 result = {
186 $class : className,
187 name : data.name,
188 };
189
190 if (Object.prototype.hasOwnProperty.call(data,'format')) {
191 result.format = data.format;
192 }
193 if (node.data.identifiedBy) {
194 result.identifiedBy = node.data.identifiedBy;
195 }
196 if (Object.prototype.hasOwnProperty.call(data,'enumValues')) {
197 result.enumValues = data.enumValues;
198 }
199 if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
200 result.elementType = data.elementType;
201 }
202 if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
203 result.decorators = data.decorators;
204 }
205
206 return handleMarks(node,result);
207}
208
209/**
210 * Handles a conditional node
211 * @param {*} node the slate variable node
212 * @param {*} isTrue is this conditional true
213 * @param {*} whenTrue the nodes when true
214 * @param {*} whenFalse the nodes when false
215 * @returns {*} the ast node
216 */
217function handleConditional(node, isTrue, whenTrue, whenFalse) {
218 const data = node.data;
219
220 let result = {
221 $class : `${NS_PREFIX_CiceroMarkModel}Conditional`,
222 name : data.name,
223 nodes: [],
224 };
225
226 result.isTrue = isTrue;
227 result.whenTrue = whenTrue;
228 result.whenFalse = whenFalse;
229
230 if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
231 result.elementType = data.elementType;
232 }
233 if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
234 result.decorators = data.decorators;
235 }
236
237 return handleMarks(node,result);
238}
239
240/**
241 * Handles a conditional definition node
242 * @param {*} node the slate variable node
243 * @param {*} whenTrue the nodes when true
244 * @param {*} whenFalse the nodes when false
245 * @returns {*} the ast node
246 */
247function handleConditionalDefinition(node, whenTrue, whenFalse) {
248 const data = node.data;
249
250 let result = {
251 $class : `${NS_PREFIX_TemplateMarkModel}ConditionalDefinition`,
252 name : data.name,
253 };
254
255 result.whenTrue = whenTrue;
256 result.whenFalse = whenFalse;
257
258 if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
259 result.elementType = data.elementType;
260 }
261 if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
262 result.decorators = data.decorators;
263 }
264
265 return handleMarks(node,result);
266}
267
268/**
269 * Handles a optional node
270 * @param {*} node the slate variable node
271 * @param {*} hasSome is this optional is present
272 * @param {*} whenSome the nodes when present
273 * @param {*} whenNone the nodes when absent
274 * @returns {*} the ast node
275 */
276function handleOptional(node, hasSome, whenSome, whenNone) {
277 const data = node.data;
278
279 let result = {
280 $class : `${NS_PREFIX_CiceroMarkModel}Optional`,
281 name : data.name,
282 nodes: [],
283 };
284
285 result.hasSome = hasSome;
286 result.whenSome = whenSome;
287 result.whenNone = whenNone;
288
289 if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
290 result.elementType = data.elementType;
291 }
292 if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
293 result.decorators = data.decorators;
294 }
295
296 return handleMarks(node,result);
297}
298
299/**
300 * Handles a optional definition node
301 * @param {*} node the slate variable node
302 * @param {*} whenSome the nodes when true
303 * @param {*} whenNone the nodes when false
304 * @returns {*} the ast node
305 */
306function handleOptionalDefinition(node, whenSome, whenNone) {
307 const data = node.data;
308
309 let result = {
310 $class : `${NS_PREFIX_TemplateMarkModel}OptionalDefinition`,
311 name : data.name,
312 };
313
314 result.whenSome = whenSome;
315 result.whenNone = whenNone;
316
317 if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
318 result.elementType = data.elementType;
319 }
320 if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
321 result.decorators = data.decorators;
322 }
323
324 return handleMarks(node,result);
325}
326
327/**
328 * Handles a formula
329 * @param {*} node the slate formula node
330 * @returns {*} the ast node
331 */
332function handleFormula(node) {
333 let result = null;
334
335 const textNode = node.children[0]; // inlines always contain a single text node
336 node.children = []; // Reset the children for the inline to avoid recursion
337
338 const className = `${NS_PREFIX_CiceroMarkModel}Formula`;
339
340 result = {
341 $class : className,
342 value : textNode.text
343 };
344
345 const data = node.data;
346 if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
347 result.elementType = data.elementType;
348 }
349 if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
350 result.decorators = data.decorators;
351 }
352 if (Object.prototype.hasOwnProperty.call(data,'dependencies')) {
353 result.dependencies = data.dependencies;
354 }
355 if (Object.prototype.hasOwnProperty.call(data,'code')) {
356 result.code = data.code;
357 }
358 if (Object.prototype.hasOwnProperty.call(data,'name')) {
359 result.name = data.name;
360 }
361
362 return handleMarks(node,result);
363}
364
365/**
366 * Handles a formula definition
367 * @param {*} node the slate formula node
368 * @returns {*} the ast node
369 */
370function handleFormulaDefinition(node) {
371 let result = null;
372
373 node.children = []; // Reset the children for the inline to avoid recursion
374
375 const className = `${NS_PREFIX_CiceroMarkModel}Formula`;
376
377 result = {
378 $class : className,
379 };
380
381 const data = node.data;
382 if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
383 result.elementType = data.elementType;
384 }
385 if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
386 result.decorators = data.decorators;
387 }
388 if (Object.prototype.hasOwnProperty.call(data,'dependencies')) {
389 result.dependencies = data.dependencies;
390 }
391 if (Object.prototype.hasOwnProperty.call(data,'code')) {
392 result.code = data.code;
393 }
394 if (Object.prototype.hasOwnProperty.call(data,'name')) {
395 result.name = data.name;
396 }
397
398 return handleMarks(node,result);
399}
400
401module.exports = {
402 removeEmptyParagraphs,
403 getText,
404 quoteString,
405 handleMarks,
406 handleText,
407 handleVariable,
408 handleVariableDefinition,
409 handleConditional,
410 handleConditionalDefinition,
411 handleOptional,
412 handleOptionalDefinition,
413 handleFormula,
414 handleFormulaDefinition,
415};
\No newline at end of file