@accordproject/markdown-common
Version:
A framework for transforming markdown
222 lines (204 loc) • 7.67 kB
JavaScript
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// @ts-nocheck
/* eslint-disable no-undef */
;
const fs = require('fs');
const diff = require('jest-diff');
const CommonMarkTransformer = require('./CommonMarkTransformer');
let commonMark = null;
expect.extend({
toMarkdownRoundtrip(markdownText) {
const json1 = commonMark.fromMarkdown(markdownText, 'json');
const newMarkdown = commonMark.toMarkdown(json1);
const json2 = commonMark.fromMarkdown(newMarkdown, 'json');
const pass = JSON.stringify(json1) === JSON.stringify(json2);
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)) : () => {
const diffString = diff(json1, json2, {
expand: true
});
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)));
};
return {
actual: markdownText,
message,
pass
};
}
}); // @ts-ignore
beforeAll(() => {
commonMark = new CommonMarkTransformer({
tagInfo: true
});
});
/**
* Get the name and contents of all markdown test files
* @returns {*} an array of name/contents tuples
*/
function getMarkdownFiles() {
const result = [];
const files = fs.readdirSync(__dirname + '/../test/data');
files.forEach(function (file) {
if (file.endsWith('.md')) {
let contents = fs.readFileSync(__dirname + '/../test/data/' + file, 'utf8');
result.push([file, contents]);
}
});
return result;
}
/**
* Get the name and contents of all markdown snippets
* used in a commonmark spec file
* @returns {*} an array of name/contents tuples
*/
function getMarkdownSpecFiles() {
const result = [];
const specExamples = extractSpecTests(__dirname + '/../test/data/spec.txt');
specExamples.forEach(function (example) {
result.push(["".concat(example.section, "-").concat(example.number), example.markdown]);
});
return result;
}
/**
* Extracts all the test md snippets from a commonmark spec file
* @param {string} testfile the file to use
* @return {*} the examples
*/
function extractSpecTests(testfile) {
let data = fs.readFileSync(testfile, 'utf8');
let examples = [];
let current_section = '';
let example_number = 0;
let tests = data.replace(/\r\n?/g, '\n') // Normalize newlines for platform independence
.replace(/^<!-- END TESTS -->(.|[\n])*/m, '');
tests.replace(/^`{32} example\n([\s\S]*?)^\.\n([\s\S]*?)^`{32}$|^#{1,6} *(.*)$/gm, function (_, markdownSubmatch, htmlSubmatch, sectionSubmatch) {
if (sectionSubmatch) {
current_section = sectionSubmatch;
} else {
example_number++;
examples.push({
markdown: markdownSubmatch,
html: htmlSubmatch,
section: current_section,
number: example_number
});
}
});
return examples;
}
describe('markdown', () => {
getMarkdownFiles().forEach((_ref) => {
let [file, markdownText] = _ref;
it("converts ".concat(file, " to concerto JSON"), () => {
const json = commonMark.fromMarkdown(markdownText, 'json');
expect(json).toMatchSnapshot();
});
it("roundtrips ".concat(file), () => {
expect(markdownText).toMarkdownRoundtrip();
});
});
});
describe('readme', () => {
it('converts example1 to CommonMark DOM', () => {
const json = commonMark.fromMarkdown('# Heading\n\nThis is some `code`.\n\nFin.', 'json'); // console.log(JSON.stringify(json, null, 4));
expect(json).toMatchSnapshot();
json.nodes[0].nodes[0].text = 'My New Heading';
const newMarkdown = commonMark.toMarkdown(json); // console.log(newMarkdown);
expect(newMarkdown).toMatchSnapshot();
});
});
describe('merge adjacent text nodes', () => {
it('merges text nodes', () => {
const input = {
$class: 'org.accordproject.commonmark.Paragraph',
nodes: [{
$class: 'org.accordproject.commonmark.Text',
text: 'one '
}, {
$class: 'org.accordproject.commonmark.Text',
text: 'two'
}]
};
const nodes = CommonMarkTransformer.mergeAdjacentTextNodes(input.nodes);
expect(nodes).toHaveLength(1);
});
it('handles empty paragraphs', () => {
const input = {
$class: 'org.accordproject.commonmark.Paragraph',
nodes: []
};
const nodes = CommonMarkTransformer.mergeAdjacentTextNodes(input.nodes);
expect(nodes).toHaveLength(0);
});
it('handles single text nodes', () => {
const input = {
$class: 'org.accordproject.commonmark.Paragraph',
nodes: [{
$class: 'org.accordproject.commonmark.Text',
text: 'one '
}]
};
const nodes = CommonMarkTransformer.mergeAdjacentTextNodes(input.nodes);
expect(nodes).toHaveLength(1);
});
it('respects formatting', () => {
const input = {
$class: 'org.accordproject.commonmark.Paragraph',
nodes: [{
$class: 'org.accordproject.commonmark.Text',
text: 'one '
}, {
$class: 'org.accordproject.commonmark.Emph',
nodes: [{
$class: 'org.accordproject.commonmark.Text',
text: 'emph'
}]
}, {
$class: 'org.accordproject.commonmark.Text',
text: 'two '
}]
};
const nodes = CommonMarkTransformer.mergeAdjacentTextNodes(input.nodes);
expect(nodes).toHaveLength(3);
});
});
describe('acceptance', () => {
it('converts acceptance to CommonMark DOM', () => {
const markdownText = fs.readFileSync(__dirname + '/../test/data/acceptance.md', 'utf8');
const json = commonMark.fromMarkdown(markdownText, 'json'); // console.log(JSON.stringify(json, null, 4));
expect(json).toMatchSnapshot();
const newMarkdown = commonMark.toMarkdown(json);
expect(newMarkdown).toMatchSnapshot();
});
it('converts *children* of acceptance to CommonMark DOM', () => {
const markdownText = fs.readFileSync(__dirname + '/../test/data/acceptance.md', 'utf8');
const json = commonMark.fromMarkdown(markdownText, 'json'); // console.log(JSON.stringify(json, null, 4));
expect(json).toMatchSnapshot();
const newMarkdown = commonMark.toMarkdownChildren(json);
expect(newMarkdown).toMatchSnapshot();
});
});
describe('markdown-spec', () => {
getMarkdownSpecFiles().forEach((_ref2) => {
let [file, markdownText] = _ref2;
it("converts ".concat(file, " to concerto JSON"), () => {
const json = commonMark.fromMarkdown(markdownText, 'json');
expect(json).toMatchSnapshot();
}); // currently skipped because not all examples roundtrip
// needs more investigation!!
it.skip("roundtrips ".concat(file), () => {
expect(markdownText).toMarkdownRoundtrip();
});
});
});