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'use strict';
15
16const {
17 NS_PREFIX_CommonMarkModel
18} = require('@accordproject/markdown-common').CommonMarkModel;
19
20const CommonMarkTransformer = require('@accordproject/markdown-common').CommonMarkTransformer;
21
22const {
23 NS_PREFIX_CiceroMarkModel
24} = require('@accordproject/markdown-cicero').CiceroMarkModel;
25
26const {
27 isIgnorable
28} = require('./helpers');
29/**
30 * A rule to deserialize text nodes.
31 * @type {Object}
32 */
33
34
35const TEXT_RULE = {
36 deserialize(el) {
37 if (el.tagName && el.tagName.toLowerCase() === 'br') {
38 // add Linebreak node in ciceromark
39 return;
40 } // text nodes will be of type 3
41
42
43 if (el.nodeType === 3 && !isIgnorable(el)) {
44 const textArray = el.nodeValue.split('\n');
45 const textNodes = textArray.map(text => {
46 if (text) {
47 return {
48 '$class': "".concat(NS_PREFIX_CommonMarkModel, 'Text'),
49 text
50 };
51 }
52 });
53 const result = [...textNodes].map((node, i) => i < textNodes.length - 1 ? [node, {
54 '$class': "".concat(NS_PREFIX_CommonMarkModel, 'Softbreak')
55 }] : [node]).reduce((a, b) => a.concat(b)).filter(n => !!n);
56 return result;
57 }
58 }
59
60}; // make sure these are getting set as attributes when we create the html
61
62/**
63 * A rule to deserialize list nodes.
64 * @type {Object}
65 */
66
67const LIST_RULE = {
68 deserialize(el, next) {
69 if (el.tagName && el.tagName.toLowerCase() === 'ul') {
70 return {
71 '$class': "".concat(NS_PREFIX_CommonMarkModel, "List"),
72 type: 'bullet',
73 tight: el.getAttribute('tight') ? el.getAttribute('tight') : true,
74 nodes: next(el.childNodes)
75 };
76 }
77
78 if (el.tagName && el.tagName.toLowerCase() === 'ol') {
79 return {
80 '$class': "".concat(NS_PREFIX_CommonMarkModel, "List"),
81 type: 'ordered',
82 delimiter: el.getAttribute('delimiter'),
83 start: el.getAttribute('start'),
84 tight: el.getAttribute('tight') ? el.getAttribute('tight') : true,
85 nodes: next(el.childNodes)
86 };
87 }
88
89 if (el.tagName && el.tagName.toLowerCase() === 'li') {
90 return {
91 '$class': "".concat(NS_PREFIX_CommonMarkModel, "Item"),
92 nodes: next(el.childNodes)
93 };
94 }
95 }
96
97};
98/**
99 * A rule to deserialize paragraph nodes.
100 * @type {Object}
101 */
102
103const PARAGRAPH_RULE = {
104 deserialize(el, next) {
105 if (el.tagName && el.tagName.toLowerCase() === 'p') {
106 return {
107 '$class': "".concat(NS_PREFIX_CommonMarkModel, "Paragraph"),
108 nodes: next(el.childNodes)
109 };
110 }
111 }
112
113};
114/**
115 * A rule to deserialize strong (bold) nodes.
116 * @type {Object}
117 */
118
119const STRONG_RULE = {
120 deserialize(el, next) {
121 if (el.tagName && el.tagName.toLowerCase() === 'strong') {
122 return {
123 '$class': "".concat(NS_PREFIX_CommonMarkModel, "Strong"),
124 nodes: next(el.childNodes)
125 };
126 }
127 }
128
129};
130/**
131 * A rule to deserialize emph (italic) nodes.
132 * @type {Object}
133 */
134
135const EMPH_RULE = {
136 deserialize(el, next) {
137 if (el.tagName && el.tagName.toLowerCase() === 'em') {
138 return {
139 '$class': "".concat(NS_PREFIX_CommonMarkModel, "Emph"),
140 nodes: next(el.childNodes)
141 };
142 }
143 }
144
145};
146/**
147 * A rule to deserialize link nodes.
148 * @type {Object}
149 */
150
151const LINK_RULE = {
152 deserialize(el, next) {
153 if (el.tagName && el.tagName.toLowerCase() === 'a') {
154 return {
155 '$class': "".concat(NS_PREFIX_CommonMarkModel, "Link"),
156 nodes: next(el.childNodes),
157 destination: el.getAttribute('href') ? el.getAttribute('href') : 'none',
158 title: el.getAttribute('title') ? el.getAttribute('title') : ''
159 };
160 }
161 }
162
163};
164/**
165 * A rule to deserialize link nodes.
166 * @type {Object}
167 */
168
169const IMAGE_RULE = {
170 deserialize(el, next) {
171 if (el.tagName && el.tagName.toLowerCase() === 'img') {
172 return {
173 '$class': "".concat(NS_PREFIX_CommonMarkModel, "Image"),
174 nodes: next(el.childNodes),
175 destination: el.getAttribute('src') ? el.getAttribute('src') : 'none',
176 title: el.getAttribute('title') ? el.getAttribute('title') : ''
177 };
178 }
179 }
180
181};
182/**
183 * A rule to deserialize heading nodes (all levels).
184 * @type {Object}
185 */
186
187const HEADING_RULE = {
188 deserialize(el, next) {
189 if (el.tagName) {
190 let level;
191
192 switch (el.tagName.toLowerCase()) {
193 case 'h1':
194 level = '1';
195 break;
196
197 case 'h2':
198 level = '2';
199 break;
200
201 case 'h3':
202 level = '3';
203 break;
204
205 case 'h4':
206 level = '4';
207 break;
208
209 case 'h5':
210 level = '5';
211 break;
212
213 case 'h6':
214 level = '6';
215 break;
216
217 default:
218 level = null;
219 }
220
221 if (level) {
222 return {
223 '$class': "".concat(NS_PREFIX_CommonMarkModel, "Heading"),
224 nodes: next(el.childNodes),
225 level
226 };
227 }
228 }
229 }
230
231};
232/**
233 * A rule to deserialize thematic break nodes.
234 * @type {Object}
235 */
236
237const THEMATIC_BREAK_RULE = {
238 deserialize(el, next) {
239 if (el.tagName && el.tagName.toLowerCase() === 'hr') {
240 return {
241 '$class': "".concat(NS_PREFIX_CommonMarkModel, "ThematicBreak")
242 };
243 }
244 }
245
246};
247/**
248 * A rule to deserialize html block nodes.
249 * @type {Object}
250 */
251// Look at common mark dingus and see how they are mapping html blocks
252// TODO: figure out how to handle custom html blocks (could be anything?)
253// const HTML_BLOCK_RULE = {
254// deserialize(el, next) {
255// if (el.tagName ) {
256// return {
257// '$class': `${NS_PREFIX_CommonMarkModel}HtmlBlock`,
258// };
259// }
260// }
261// };
262
263/**
264 * A rule to deserialize code block nodes.
265 * @type {Object}
266 */
267
268const CODE_BLOCK_RULE = {
269 deserialize(el, next) {
270 if (el.tagName && el.tagName.toLowerCase() === 'pre' && el.getAttribute('class') === 'code_block') {
271 const children = el.childNodes;
272
273 if (children.length === 1 && children[0].tagName.toLowerCase() === 'code') {
274 const info = children[0].getAttribute('data-ciceromark');
275
276 if (info) {
277 const decodedInfo = decodeURIComponent(info);
278 const tag = CommonMarkTransformer.parseHtmlBlock(decodedInfo);
279 return {
280 '$class': "".concat(NS_PREFIX_CommonMarkModel, "CodeBlock"),
281 text: children[0].textContent,
282 info: decodedInfo,
283 tag
284 };
285 } else {
286 return {
287 '$class': "".concat(NS_PREFIX_CommonMarkModel, "CodeBlock"),
288 text: children[0].textContent
289 };
290 }
291 }
292 }
293 }
294
295};
296/**
297 * A rule to deserialize inline code nodes.
298 * @type {Object}
299 */
300
301const INLINE_CODE_RULE = {
302 deserialize(el, next) {
303 if (el.tagName && el.tagName.toLowerCase() === 'code') {
304 {
305 return {
306 '$class': "".concat(NS_PREFIX_CommonMarkModel, "Code"),
307 text: el.textContent
308 };
309 }
310 }
311 }
312
313};
314/**
315 * A rule to deserialize block quote nodes.
316 * @type {Object}
317 */
318
319const BLOCK_QUOTE_RULE = {
320 deserialize(el, next) {
321 if (el.tagName && el.tagName.toLowerCase() === 'blockquote') {
322 return {
323 '$class': "".concat(NS_PREFIX_CommonMarkModel, "BlockQuote"),
324 nodes: next(el.childNodes)
325 };
326 }
327 }
328
329};
330/**
331 * A rule to deserialize clause nodes.
332 * @type {Object}
333 */
334
335const CLAUSE_RULE = {
336 deserialize(el, next) {
337 const tag = el.tagName;
338
339 if (tag && tag.toLowerCase() === 'div' && el.getAttribute('class') === 'clause') {
340 return {
341 '$class': "".concat(NS_PREFIX_CiceroMarkModel, "Clause"),
342 clauseid: el.getAttribute('clauseid'),
343 src: el.getAttribute('src'),
344 nodes: next(el.childNodes)
345 };
346 }
347 }
348
349};
350/**
351 * A rule to deserialize variable nodes.
352 * @type {Object}
353 */
354
355const VARIABLE_RULE = {
356 deserialize(el, next) {
357 const {
358 tagName
359 } = el;
360
361 if (tagName && tagName.toLowerCase() === 'span' && el.getAttribute('class') === 'variable') {
362 if (el.getAttribute('format')) {
363 return {
364 '$class': "".concat(NS_PREFIX_CiceroMarkModel, "Variable"),
365 id: el.getAttribute('id'),
366 value: el.textContent,
367 format: el.getAttribute('format')
368 };
369 } else {
370 return {
371 '$class': "".concat(NS_PREFIX_CiceroMarkModel, "Variable"),
372 id: el.getAttribute('id'),
373 value: el.textContent
374 };
375 }
376 }
377 }
378
379};
380/**
381 * A rule to deserialize conditional variable nodes.
382 * @type {Object}
383 */
384
385const CONDITIONAL_VARIABLE_RULE = {
386 deserialize(el, next) {
387 const {
388 tagName
389 } = el;
390
391 if (tagName && tagName.toLowerCase() === 'span' && el.getAttribute('class') === 'conditional') {
392 return {
393 '$class': "".concat(NS_PREFIX_CiceroMarkModel, "ConditionalVariable"),
394 id: el.getAttribute('id'),
395 whenTrue: el.getAttribute('whenTrue'),
396 whenFalse: el.getAttribute('whenFalse'),
397 value: el.textContent
398 };
399 }
400 }
401
402};
403/**
404 * A rule to deserialize computed variable nodes.
405 * @type {Object}
406 */
407
408const COMPUTED_VARIABLE_RULE = {
409 deserialize(el, next) {
410 const {
411 tagName
412 } = el;
413
414 if (tagName && tagName.toLowerCase() === 'span' && el.getAttribute('class') === 'computed') {
415 return {
416 '$class': "".concat(NS_PREFIX_CiceroMarkModel, "ComputedVariable"),
417 value: el.textContent
418 };
419 }
420 }
421
422};
423/**
424 * A rule to deserialize softbreak nodes.
425 * @type {Object}
426 */
427
428const SOFTBREAK_RULE = {
429 deserialize(el, next) {
430 if (el.tagName && el.tagName.toLowerCase() === 'wbr') {
431 return {
432 '$class': "".concat(NS_PREFIX_CommonMarkModel, "Softbreak")
433 };
434 }
435 }
436
437};
438/**
439 * A rule to deserialize html inline nodes.
440 * @type {Object}
441 */
442
443const HTML_INLINE_RULE = {
444 deserialize(el, next) {
445 const {
446 tagName
447 } = el;
448
449 if (tagName && tagName.toLowerCase() === 'span' && el.getAttribute('class') === 'html_inline') {
450 {
451 const text = el.innerHTML;
452 const tag = CommonMarkTransformer.parseHtmlBlock(text);
453 return {
454 '$class': "".concat(NS_PREFIX_CommonMarkModel, "HtmlInline"),
455 text: text,
456 tag
457 };
458 }
459 }
460 }
461
462};
463/**
464 * A rule to deserialize html block nodes.
465 * @type {Object}
466 */
467
468const HTML_BLOCK_RULE = {
469 deserialize(el, next) {
470 if (el.tagName && el.tagName.toLowerCase() === 'pre' && el.getAttribute('class') === 'html_block') {
471 const children = el.childNodes;
472
473 if (children.length === 1 && children[0].tagName.toLowerCase() === 'code') {
474 const text = children[0].innerHTML;
475 const tag = CommonMarkTransformer.parseHtmlBlock(text);
476 return {
477 '$class': "".concat(NS_PREFIX_CommonMarkModel, "HtmlBlock"),
478 text: text,
479 tag
480 };
481 }
482 }
483 }
484
485};
486const rules = [LIST_RULE, PARAGRAPH_RULE, STRONG_RULE, EMPH_RULE, LINK_RULE, HEADING_RULE, THEMATIC_BREAK_RULE, CODE_BLOCK_RULE, INLINE_CODE_RULE, BLOCK_QUOTE_RULE, CLAUSE_RULE, VARIABLE_RULE, CONDITIONAL_VARIABLE_RULE, SOFTBREAK_RULE, COMPUTED_VARIABLE_RULE, TEXT_RULE, HTML_INLINE_RULE, HTML_BLOCK_RULE, IMAGE_RULE];
487module.exports = rules;
\No newline at end of file