| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 |
1
1
1
1
1
1
1
1
1
864
1
2000
2000
2000
775
775
2000
1181
2000
545
545
545
1455
17
17
17
17
17
1438
246
246
246
9
246
1192
38
38
1154
69
69
69
69
1085
879
879
879
206
58
58
58
148
86
86
86
86
86
62
2
2
2
2
2
60
4
4
8
4
4
12
4
4
56
56
56
1
1
54
53
53
53
53
9
9
9
10
10
53
53
53
53
53
53
53
53
53
1
1
| 'use strict';
var mdast = require('..'),
assert = require('assert'),
fs = require('fs'),
path = require('path');
describe('mdast()', function () {
it('should be of type `function`', function () {
assert(typeof mdast === 'function');
});
});
describe('msast.Parser()', function () {});
describe('msast.Lexer()', function () {});
describe('fixtures', function () {
var validateInput, validateInputs, optionsMap;
validateInputs = function (children) {
children.forEach(validateInput);
};
validateInput = function (context) {
var keys = Object.keys(context),
type = context.type;
assert('type' in context);
if ('children' in context) {
assert(Array.isArray(context.children));
validateInputs(context.children);
}
if ('value' in context) {
assert(typeof context.value === 'string');
}
if (
type === 'paragraph' ||
type === 'blockquote' ||
type === 'looseItem' ||
type === 'listItem'
) {
assert(keys.length === 2);
assert('children' in context);
return;
}
if (type === 'heading') {
assert(keys.length === 3);
assert(context.depth > 0);
assert(context.depth <= 6);
assert('children' in context);
return;
}
if (type === 'code') {
assert(keys.length === 2 || keys.length === 3);
assert('value' in context);
if ('lang' in context) {
assert(
context.lang === null ||
typeof context.lang === 'string'
);
}
return;
}
if (type === 'horizontalRule' || type === 'break') {
assert(keys.length === 1);
return;
}
if (type === 'list') {
assert('children' in context);
assert(typeof context.ordered === 'boolean');
assert(keys.length === 3);
return;
}
if (type === 'text') {
assert(keys.length === 2);
assert('value' in context);
return;
}
if (
type === 'strong' ||
type === 'emphasis' ||
type === 'delete'
) {
assert(keys.length === 2);
assert('children' in context);
return;
}
if (type === 'link') {
assert('children' in context);
assert(
context.title === null ||
typeof context.title === 'string'
);
assert(typeof context.href === 'string');
assert(keys.length === 4);
return;
}
if (type === 'image') {
assert(
context.title === null ||
typeof context.title === 'string'
);
assert(
context.alt === null ||
typeof context.alt === 'string'
);
assert(typeof context.href === 'string');
assert(keys.length === 4);
return;
}
if (type === 'table') {
context.header.forEach(validateInputs);
context.cells.forEach(function (row) {
row.forEach(validateInputs);
});
assert(Array.isArray(context.align));
context.align.forEach(function (align) {
assert(
align === null ||
align === 'left' ||
align === 'right' ||
align === 'center'
);
});
assert(keys.length === 4);
return;
}
/* This is the last possible type. If more types are added, they
* should be added before this block, or the type:html tests should
* be wrapped in an if statement. */
assert(type === 'html');
assert(keys.length === 2);
assert('value' in context);
};
optionsMap = {
'gfm' : ['gfm', true],
'nogfm' : ['gfm', false],
'tables' : ['tables', true],
'notables' : ['tables', false],
'breaks' : ['breaks', true],
'nobreaks' : ['breaks', false],
'pedantic' : ['pedantic', true],
'nopedantic' : ['pedantic', false],
'smartlists' : ['smartlists', true],
'nosmartlists' : ['smartlists', false]
};
fs.readdirSync(path.join(__dirname, 'input')).filter(function (filepath) {
return filepath.indexOf('.') !== 0;
}).forEach(function (filepath) {
var options, filename, input, output, flag, index;
filename = filepath.split('.');
filename.pop();
if (filename.length > 1) {
index = 0;
options = {};
while (filename[++index]) {
flag = optionsMap[filename[index]];
options[flag[0]] = flag[1];
}
}
filename = filename.join('.');
input = fs.readFileSync(
path.join(__dirname, 'input', filepath),
'utf-8'
);
output = fs.readFileSync(
path.join(__dirname, 'output', filename + '.json'),
'utf-8'
);
input = mdast(input, options);
validateInputs(input);
it('should work on ' + filename, function () {
output = JSON.parse(output);
try {
assert(JSON.stringify(input) === JSON.stringify(output));
} catch (error) {
/* istanbul ignore next: Shouldn't reach, helps debugging. */
console.log(
JSON.stringify(input, null, 2),
JSON.stringify(output, null, 2)
);
/* istanbul ignore next: Shouldn't reach, helps debugging. */
throw error;
}
});
});
});
|