UNPKG

2.21 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 ToHtmlStringVisitor = require('./ToHtmlStringVisitor');
17
18const ToCiceroMarkVisitor = require('./ToCiceroMarkVisitor');
19
20const CiceroMarkTransformer = require('@accordproject/markdown-cicero').CiceroMarkTransformer;
21/**
22 * Converts a CiceroMark or CommonMark DOM to HTML
23 */
24
25
26class HtmlTransformer {
27 /**
28 * Construct the parser.
29 * @param {object} [options] configuration options
30 */
31 constructor() {
32 let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
33 this.options = options;
34 this.ciceroMarkTransformer = new CiceroMarkTransformer();
35 }
36 /**
37 * Converts a CiceroMark DOM to an html string
38 * @param {*} input - CiceroMark DOM object
39 * @returns {string} the html string
40 */
41
42
43 toHtml(input) {
44 if (!input.getType) {
45 input = this.ciceroMarkTransformer.getSerializer().fromJSON(input);
46 }
47
48 const parameters = {};
49 parameters.result = '';
50 parameters.first = true;
51 parameters.indent = 0;
52 const visitor = new ToHtmlStringVisitor(this.options);
53 input.accept(visitor, parameters);
54 return parameters.result.trim();
55 }
56 /**
57 * Converts an html string to a CiceroMark DOM
58 * @param {string} input - html string
59 * @param {string} [format] result format, defaults to 'concerto'. Pass
60 * 'json' to return the JSON data.
61 * @returns {*} CiceroMark DOM
62 */
63
64
65 toCiceroMark(input) {
66 let format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'concerto';
67 const visitor = new ToCiceroMarkVisitor(this.options);
68 return visitor.toCiceroMark(input, format);
69 }
70
71}
72
73module.exports = HtmlTransformer;
\No newline at end of file