UNPKG

3.96 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 ToSlateVisitor = require('./ToSlateVisitor');
17
18const slateToCiceroMarkDom = require('./slateToCiceroMarkDom');
19
20const CiceroMarkTransformer = require('@accordproject/markdown-cicero').CiceroMarkTransformer;
21/**
22 * Converts a CiceroMark DOM to/from a Slate DOM.
23 */
24
25
26class SlateTransformer {
27 /**
28 * Construct the Slate transformer.
29 */
30 constructor() {
31 this.ciceroMarkTransformer = new CiceroMarkTransformer();
32 this.serializer = this.ciceroMarkTransformer.getSerializer();
33 }
34 /**
35 * Converts a CiceroMark DOM to a Slate DOM
36 * @param {*} input - CiceroMark DOM
37 * @returns {*} Slate JSON
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 }; // Find any clauses next to each other, force in a paragraph between
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 } // If the final node is a clause, force in a paragraph after
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 * Converts a Slate JSON to CiceroMark DOM
92 * @param {*} value - Slate json
93 * @param {string} [format] - result format, defaults to 'concerto'. Pass
94 * 'json' to return the JSON data.
95 * @returns {*} the CiceroMark DOM
96 */
97
98
99 toCiceroMark(value) {
100 let format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'concerto';
101 const json = slateToCiceroMarkDom(value.document); // console.log(JSON.stringify(json, null, 3));
102
103 if (format === 'concerto') {
104 return this.serializer.fromJSON(json);
105 } else {
106 return json;
107 }
108 }
109 /**
110 * Converts a Slate JSON to a markdown string
111 * @param {*} value - Slate json
112 * @param {object} [options] - configuration options
113 * @returns {*} markdown string
114 */
115
116
117 toMarkdown(value, options) {
118 const ciceroMark = this.toCiceroMark(value, 'json');
119 return this.ciceroMarkTransformer.toMarkdown(ciceroMark, options);
120 }
121 /**
122 * Converts a markdown string to a Slate JSON
123 * @param {string} markdown - a markdown string
124 * @returns {*} Slate json
125 */
126
127
128 fromMarkdown(markdown) {
129 const ciceroMarkDom = this.ciceroMarkTransformer.fromMarkdown(markdown, 'json'); // console.log(JSON.stringify(ciceroMarkDom, null, 4));
130
131 return this.fromCiceroMark(ciceroMarkDom);
132 }
133
134}
135
136module.exports = SlateTransformer;
\No newline at end of file