1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | 'use strict';
|
18 |
|
19 | const fs = require('fs');
|
20 |
|
21 | const CiceroMarkTransformer = require('@accordproject/markdown-cicero').CiceroMarkTransformer;
|
22 |
|
23 | const HtmlTransformer = require('./HtmlTransformer');
|
24 |
|
25 | let htmlTransformer = null;
|
26 | let ciceroTransformer = null;
|
27 |
|
28 | beforeAll(() => {
|
29 | htmlTransformer = new HtmlTransformer();
|
30 | ciceroTransformer = new CiceroMarkTransformer();
|
31 | });
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function 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 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function 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 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function 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')
|
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 |
|
93 | describe.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();
|
99 |
|
100 | const html = htmlTransformer.toHtml(json);
|
101 | expect(html).toMatchSnapshot();
|
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();
|
110 |
|
111 | const md = ciceroTransformer.toMarkdown(ciceroMarkDom);
|
112 | expect(md).toMatchSnapshot();
|
113 | });
|
114 | });
|
115 | describe('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();
|
121 |
|
122 | const html = htmlTransformer.toHtml(json);
|
123 | expect(html).toMatchSnapshot();
|
124 | });
|
125 | });
|
126 | }); |
\ | No newline at end of file |