UNPKG

4.01 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// @ts-nocheck
15
16/* eslint-disable no-undef */
17'use strict';
18
19const fs = require('fs');
20
21const CiceroMarkTransformer = require('@accordproject/markdown-cicero').CiceroMarkTransformer;
22
23const HtmlTransformer = require('./HtmlTransformer');
24
25let htmlTransformer = null;
26let ciceroTransformer = null; // @ts-ignore
27
28beforeAll(() => {
29 htmlTransformer = new HtmlTransformer();
30 ciceroTransformer = new CiceroMarkTransformer();
31});
32/**
33 * Get the name and contents of all markdown test files
34 * @returns {*} an array of name/contents tuples
35 */
36
37function getMarkdownFiles() {
38 const result = [];
39 const files = fs.readdirSync(__dirname + '/../test/data');
40 files.forEach(function (file) {
41 if (file.endsWith('.md')) {
42 let contents = fs.readFileSync(__dirname + '/../test/data/' + file, 'utf8');
43 result.push([file, contents]);
44 }
45 });
46 return result;
47}
48/**
49 * Get the name and contents of all markdown snippets
50 * used in a commonmark spec file
51 * @returns {*} an array of name/contents tuples
52 */
53
54
55function getMarkdownSpecFiles() {
56 const result = [];
57 const specExamples = extractSpecTests(__dirname + '/../test/data/spec.txt');
58 specExamples.forEach(function (example) {
59 result.push(["".concat(example.section, "-").concat(example.number), example.markdown]);
60 });
61 return result;
62}
63/**
64 * Extracts all the test md snippets from a commonmark spec file
65 * @param {string} testfile the file to use
66 * @return {*} the examples
67 */
68
69
70function extractSpecTests(testfile) {
71 let data = fs.readFileSync(testfile, 'utf8');
72 let examples = [];
73 let current_section = '';
74 let example_number = 0;
75 let tests = data.replace(/\r\n?/g, '\n') // Normalize newlines for platform independence
76 .replace(/^<!-- END TESTS -->(.|[\n])*/m, '');
77 tests.replace(/^`{32} example\n([\s\S]*?)^\.\n([\s\S]*?)^`{32}$|^#{1,6} *(.*)$/gm, function (_, markdownSubmatch, htmlSubmatch, sectionSubmatch) {
78 if (sectionSubmatch) {
79 current_section = sectionSubmatch;
80 } else {
81 example_number++;
82 examples.push({
83 markdown: markdownSubmatch,
84 html: htmlSubmatch,
85 section: current_section,
86 number: example_number
87 });
88 }
89 });
90 return examples;
91}
92
93describe.only('html', () => {
94 getMarkdownFiles().forEach((_ref, i) => {
95 let [file, markdownText] = _ref;
96 it("converts ".concat(file, " to html"), () => {
97 const json = ciceroTransformer.fromMarkdown(markdownText, 'json');
98 expect(json).toMatchSnapshot(); // (1)
99
100 const html = htmlTransformer.toHtml(json);
101 expect(html).toMatchSnapshot(); // (2)
102
103 const ciceroMarkDom = htmlTransformer.toCiceroMark(html, 'json');
104 expect(ciceroMarkDom).toEqual(json);
105 });
106 });
107 it('converts unwrapped <li> to html', () => {
108 const ciceroMarkDom = htmlTransformer.toCiceroMark('<p>Hello</p><li>list item</li><p>World.</p>', 'json');
109 expect(ciceroMarkDom).toMatchSnapshot(); // (1)
110
111 const md = ciceroTransformer.toMarkdown(ciceroMarkDom);
112 expect(md).toMatchSnapshot(); // (2)
113 });
114});
115describe('markdown-spec', () => {
116 getMarkdownSpecFiles().forEach((_ref2) => {
117 let [file, markdownText] = _ref2;
118 it("converts ".concat(file, " to concerto JSON"), () => {
119 const json = ciceroTransformer.fromMarkdown(markdownText, 'json');
120 expect(json).toMatchSnapshot(); // (1)
121
122 const html = htmlTransformer.toHtml(json);
123 expect(html).toMatchSnapshot(); // (2)
124 });
125 });
126});
\No newline at end of file