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 diff = require('jest-diff');
|
22 |
|
23 | const CommonMarkTransformer = require('./CommonMarkTransformer');
|
24 |
|
25 | let commonMark = null;
|
26 | expect.extend({
|
27 | toMarkdownRoundtrip(markdownText) {
|
28 | const json1 = commonMark.fromMarkdown(markdownText, 'json');
|
29 | const newMarkdown = commonMark.toMarkdown(json1);
|
30 | const json2 = commonMark.fromMarkdown(newMarkdown, 'json');
|
31 | const pass = JSON.stringify(json1) === JSON.stringify(json2);
|
32 | const message = pass ? () => this.utils.matcherHint("toMarkdownRoundtrip - ".concat(markdownText, " -> ").concat(newMarkdown), undefined, undefined, undefined) + '\n\n' + "Expected: ".concat(this.utils.printExpected(json1), "\n") + "Received: ".concat(this.utils.printReceived(json2)) : () => {
|
33 | const diffString = diff(json1, json2, {
|
34 | expand: true
|
35 | });
|
36 | return this.utils.matcherHint("toMarkdownRoundtrip - ".concat(JSON.stringify(markdownText), " -> ").concat(JSON.stringify(newMarkdown)), undefined, undefined, undefined) + '\n\n' + (diffString && diffString.includes('- Expect') ? "Difference:\n\n".concat(diffString) : "Expected: ".concat(this.utils.printExpected(json1), "\n") + "Received: ".concat(this.utils.printReceived(json2)));
|
37 | };
|
38 | return {
|
39 | actual: markdownText,
|
40 | message,
|
41 | pass
|
42 | };
|
43 | }
|
44 |
|
45 | });
|
46 |
|
47 | beforeAll(() => {
|
48 | commonMark = new CommonMarkTransformer({
|
49 | tagInfo: true
|
50 | });
|
51 | });
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function getMarkdownFiles() {
|
58 | const result = [];
|
59 | const files = fs.readdirSync(__dirname + '/../test/data');
|
60 | files.forEach(function (file) {
|
61 | if (file.endsWith('.md')) {
|
62 | let contents = fs.readFileSync(__dirname + '/../test/data/' + file, 'utf8');
|
63 | result.push([file, contents]);
|
64 | }
|
65 | });
|
66 | return result;
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | function getMarkdownSpecFiles() {
|
76 | const result = [];
|
77 | const specExamples = extractSpecTests(__dirname + '/../test/data/spec.txt');
|
78 | specExamples.forEach(function (example) {
|
79 | result.push(["".concat(example.section, "-").concat(example.number), example.markdown]);
|
80 | });
|
81 | return result;
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | function extractSpecTests(testfile) {
|
91 | let data = fs.readFileSync(testfile, 'utf8');
|
92 | let examples = [];
|
93 | let current_section = '';
|
94 | let example_number = 0;
|
95 | let tests = data.replace(/\r\n?/g, '\n')
|
96 | .replace(/^<!-- END TESTS -->(.|[\n])*/m, '');
|
97 | tests.replace(/^`{32} example\n([\s\S]*?)^\.\n([\s\S]*?)^`{32}$|^#{1,6} *(.*)$/gm, function (_, markdownSubmatch, htmlSubmatch, sectionSubmatch) {
|
98 | if (sectionSubmatch) {
|
99 | current_section = sectionSubmatch;
|
100 | } else {
|
101 | example_number++;
|
102 | examples.push({
|
103 | markdown: markdownSubmatch,
|
104 | html: htmlSubmatch,
|
105 | section: current_section,
|
106 | number: example_number
|
107 | });
|
108 | }
|
109 | });
|
110 | return examples;
|
111 | }
|
112 |
|
113 | describe('markdown', () => {
|
114 | getMarkdownFiles().forEach((_ref) => {
|
115 | let [file, markdownText] = _ref;
|
116 | it("converts ".concat(file, " to concerto JSON"), () => {
|
117 | const json = commonMark.fromMarkdown(markdownText, 'json');
|
118 | expect(json).toMatchSnapshot();
|
119 | });
|
120 | it("roundtrips ".concat(file), () => {
|
121 | expect(markdownText).toMarkdownRoundtrip();
|
122 | });
|
123 | });
|
124 | });
|
125 | describe('readme', () => {
|
126 | it('converts example1 to CommonMark DOM', () => {
|
127 | const json = commonMark.fromMarkdown('# Heading\n\nThis is some `code`.\n\nFin.', 'json');
|
128 |
|
129 | expect(json).toMatchSnapshot();
|
130 | json.nodes[0].nodes[0].text = 'My New Heading';
|
131 | const newMarkdown = commonMark.toMarkdown(json);
|
132 |
|
133 | expect(newMarkdown).toMatchSnapshot();
|
134 | });
|
135 | });
|
136 | describe('merge adjacent text nodes', () => {
|
137 | it('merges text nodes', () => {
|
138 | const input = {
|
139 | $class: 'org.accordproject.commonmark.Paragraph',
|
140 | nodes: [{
|
141 | $class: 'org.accordproject.commonmark.Text',
|
142 | text: 'one '
|
143 | }, {
|
144 | $class: 'org.accordproject.commonmark.Text',
|
145 | text: 'two'
|
146 | }]
|
147 | };
|
148 | const nodes = CommonMarkTransformer.mergeAdjacentTextNodes(input.nodes);
|
149 | expect(nodes).toHaveLength(1);
|
150 | });
|
151 | it('handles empty paragraphs', () => {
|
152 | const input = {
|
153 | $class: 'org.accordproject.commonmark.Paragraph',
|
154 | nodes: []
|
155 | };
|
156 | const nodes = CommonMarkTransformer.mergeAdjacentTextNodes(input.nodes);
|
157 | expect(nodes).toHaveLength(0);
|
158 | });
|
159 | it('handles single text nodes', () => {
|
160 | const input = {
|
161 | $class: 'org.accordproject.commonmark.Paragraph',
|
162 | nodes: [{
|
163 | $class: 'org.accordproject.commonmark.Text',
|
164 | text: 'one '
|
165 | }]
|
166 | };
|
167 | const nodes = CommonMarkTransformer.mergeAdjacentTextNodes(input.nodes);
|
168 | expect(nodes).toHaveLength(1);
|
169 | });
|
170 | it('respects formatting', () => {
|
171 | const input = {
|
172 | $class: 'org.accordproject.commonmark.Paragraph',
|
173 | nodes: [{
|
174 | $class: 'org.accordproject.commonmark.Text',
|
175 | text: 'one '
|
176 | }, {
|
177 | $class: 'org.accordproject.commonmark.Emph',
|
178 | nodes: [{
|
179 | $class: 'org.accordproject.commonmark.Text',
|
180 | text: 'emph'
|
181 | }]
|
182 | }, {
|
183 | $class: 'org.accordproject.commonmark.Text',
|
184 | text: 'two '
|
185 | }]
|
186 | };
|
187 | const nodes = CommonMarkTransformer.mergeAdjacentTextNodes(input.nodes);
|
188 | expect(nodes).toHaveLength(3);
|
189 | });
|
190 | });
|
191 | describe('acceptance', () => {
|
192 | it('converts acceptance to CommonMark DOM', () => {
|
193 | const markdownText = fs.readFileSync(__dirname + '/../test/data/acceptance.md', 'utf8');
|
194 | const json = commonMark.fromMarkdown(markdownText, 'json');
|
195 |
|
196 | expect(json).toMatchSnapshot();
|
197 | const newMarkdown = commonMark.toMarkdown(json);
|
198 | expect(newMarkdown).toMatchSnapshot();
|
199 | });
|
200 | it('converts *children* of acceptance to CommonMark DOM', () => {
|
201 | const markdownText = fs.readFileSync(__dirname + '/../test/data/acceptance.md', 'utf8');
|
202 | const json = commonMark.fromMarkdown(markdownText, 'json');
|
203 |
|
204 | expect(json).toMatchSnapshot();
|
205 | const newMarkdown = commonMark.toMarkdownChildren(json);
|
206 | expect(newMarkdown).toMatchSnapshot();
|
207 | });
|
208 | });
|
209 | describe('markdown-spec', () => {
|
210 | getMarkdownSpecFiles().forEach((_ref2) => {
|
211 | let [file, markdownText] = _ref2;
|
212 | it("converts ".concat(file, " to concerto JSON"), () => {
|
213 | const json = commonMark.fromMarkdown(markdownText, 'json');
|
214 | expect(json).toMatchSnapshot();
|
215 | });
|
216 |
|
217 |
|
218 | it.skip("roundtrips ".concat(file), () => {
|
219 | expect(markdownText).toMarkdownRoundtrip();
|
220 | });
|
221 | });
|
222 | }); |
\ | No newline at end of file |