1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 | const {
|
17 | NS_PREFIX_CommonMarkModel
|
18 | } = require('@accordproject/markdown-common').CommonMarkModel;
|
19 |
|
20 | const CommonMarkTransformer = require('@accordproject/markdown-common').CommonMarkTransformer;
|
21 |
|
22 | const {
|
23 | NS_PREFIX_CiceroMarkModel
|
24 | } = require('@accordproject/markdown-cicero').CiceroMarkModel;
|
25 |
|
26 | const {
|
27 | isIgnorable
|
28 | } = require('./helpers');
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | const TEXT_RULE = {
|
36 | deserialize(el) {
|
37 | if (el.tagName && el.tagName.toLowerCase() === 'br') {
|
38 |
|
39 | return;
|
40 | }
|
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 | };
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | const 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 |
|
100 |
|
101 |
|
102 |
|
103 | const 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 |
|
116 |
|
117 |
|
118 |
|
119 | const 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 |
|
132 |
|
133 |
|
134 |
|
135 | const 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 |
|
148 |
|
149 |
|
150 |
|
151 | const 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 |
|
166 |
|
167 |
|
168 |
|
169 | const 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 |
|
184 |
|
185 |
|
186 |
|
187 | const 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 |
|
234 |
|
235 |
|
236 |
|
237 | const 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 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 | const 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 |
|
298 |
|
299 |
|
300 |
|
301 | const 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 |
|
316 |
|
317 |
|
318 |
|
319 | const 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 |
|
332 |
|
333 |
|
334 |
|
335 | const 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 |
|
352 |
|
353 |
|
354 |
|
355 | const 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 |
|
382 |
|
383 |
|
384 |
|
385 | const 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 |
|
405 |
|
406 |
|
407 |
|
408 | const 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 |
|
425 |
|
426 |
|
427 |
|
428 | const 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 |
|
440 |
|
441 |
|
442 |
|
443 | const 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 |
|
465 |
|
466 |
|
467 |
|
468 | const 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 | };
|
486 | const 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];
|
487 | module.exports = rules; |
\ | No newline at end of file |