UNPKG

7.67 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 diff = require('jest-diff');
22
23const CommonMarkTransformer = require('./CommonMarkTransformer');
24
25let commonMark = null;
26expect.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}); // @ts-ignore
46
47beforeAll(() => {
48 commonMark = new CommonMarkTransformer({
49 tagInfo: true
50 });
51});
52/**
53 * Get the name and contents of all markdown test files
54 * @returns {*} an array of name/contents tuples
55 */
56
57function 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 * Get the name and contents of all markdown snippets
70 * used in a commonmark spec file
71 * @returns {*} an array of name/contents tuples
72 */
73
74
75function 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 * Extracts all the test md snippets from a commonmark spec file
85 * @param {string} testfile the file to use
86 * @return {*} the examples
87 */
88
89
90function 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') // Normalize newlines for platform independence
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
113describe('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});
125describe('readme', () => {
126 it('converts example1 to CommonMark DOM', () => {
127 const json = commonMark.fromMarkdown('# Heading\n\nThis is some `code`.\n\nFin.', 'json'); // console.log(JSON.stringify(json, null, 4));
128
129 expect(json).toMatchSnapshot();
130 json.nodes[0].nodes[0].text = 'My New Heading';
131 const newMarkdown = commonMark.toMarkdown(json); // console.log(newMarkdown);
132
133 expect(newMarkdown).toMatchSnapshot();
134 });
135});
136describe('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});
191describe('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'); // console.log(JSON.stringify(json, null, 4));
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'); // console.log(JSON.stringify(json, null, 4));
203
204 expect(json).toMatchSnapshot();
205 const newMarkdown = commonMark.toMarkdownChildren(json);
206 expect(newMarkdown).toMatchSnapshot();
207 });
208});
209describe('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 }); // currently skipped because not all examples roundtrip
216 // needs more investigation!!
217
218 it.skip("roundtrips ".concat(file), () => {
219 expect(markdownText).toMarkdownRoundtrip();
220 });
221 });
222});
\No newline at end of file