-
Notifications
You must be signed in to change notification settings - Fork 4
/
markdown-it-github-preamble-spec.js
278 lines (268 loc) · 6.27 KB
/
markdown-it-github-preamble-spec.js
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*global describe, beforeEach, it, expect*/
'use strict';
const Markdown = require('markdown-it'),
markdownItGithubPreamble = require('../src/markdown-it-github-preamble');
describe('markdownItGithubPreamble', () => {
let md;
const toHtml = function (array) {
return md.render(array.join('\n')).replace(/\n/g, '');
};
beforeEach(() => {
md = new Markdown().use(markdownItGithubPreamble);
});
it('renders the yaml preamble as a table if fenced by ---', () => {
expect(toHtml([
'---',
'propa: vala',
'propb: valb',
'---',
'',
'abcd'
])).toEqual(
'<table>' +
'<thead><tr><th>propa</th><th>propb</th></tr></thead>' +
'<tbody><tr><td>vala</td><td>valb</td></tr></tbody>' +
'</table>' +
'<p>abcd</p>'
);
});
it('renders the yaml preamble as a table if fenced by more than three markers', () => {
expect(toHtml([
'---------',
'propa: vala',
'propb: valb',
'-----',
'',
'abcd'
])).toEqual(
'<table>' +
'<thead><tr><th>propa</th><th>propb</th></tr></thead>' +
'<tbody><tr><td>vala</td><td>valb</td></tr></tbody>' +
'</table>' +
'<p>abcd</p>'
);
});
it('does not render the preamble if the contents are not key-value properties', () => {
expect(toHtml([
'---------',
'propa: vala',
'propb valb',
'-----',
'',
'abcd'
])).not.toMatch(/<table>/);
});
it('does not render the preamble if the contents are indented', () => {
expect(toHtml([
' ---',
' propa: vala',
' propb: valb',
' ---',
''
])).not.toMatch(/<table>/);
});
it('does not render the preamble if prefixed by less than three markers', () => {
expect(toHtml([
'--',
'propa: vala',
'propb: valb',
'--',
'',
'abcd'
])).not.toMatch(/<table>/);
});
it('does not render the preamble if it contains empty lines', () => {
expect(toHtml([
'--',
'propa: vala',
'',
'propb: valb',
'--',
'',
'abcd'
])).not.toMatch(/<table>/);
});
it('does not render the preamble if it is unclosed', () => {
expect(toHtml([
'--',
'propa: vala',
'propb: valb'
])).not.toMatch(/<table>/);
});
it('does not count markers broken accross lines', () => {
expect(toHtml([
'--',
'-',
'propa: vala',
'propb: valb',
'---',
''
])).not.toMatch(/<table>/);
});
it('does nothing if the fenced block is not the first in the page', () => {
expect(toHtml([
'abcd',
'',
'---',
'propa: vala',
'propb: valb',
'---'
])).not.toMatch(/<table>/);
});
it('runs each time from a clean position', () => {
toHtml([
'---',
'propa: vala',
'propb: valb',
'---',
'',
'abcd'
]);
expect(toHtml([
'---',
'propa: vala',
'propb: valb',
'---',
'',
'abcd'
])).toEqual(
'<table>' +
'<thead><tr><th>propa</th><th>propb</th></tr></thead>' +
'<tbody><tr><td>vala</td><td>valb</td></tr></tbody>' +
'</table>' +
'<p>abcd</p>'
);
});
describe('options', () => {
describe('className', () => {
it('adds a css class to the table', () => {
md = new Markdown().use(markdownItGithubPreamble, {className: 'classy'});
expect(toHtml([
'---',
'propa: vala',
'propb: valb',
'---',
''
])).toEqual(
'<table class="classy">' +
'<thead><tr><th>propa</th><th>propb</th></tr></thead>' +
'<tbody><tr><td>vala</td><td>valb</td></tr></tbody>' +
'</table>'
);
});
});
describe('tableAttributeName+Value', () => {
it('add a DOM attribute and value to the table', () => {
md = new Markdown().use(markdownItGithubPreamble, {
tableAttributeName: 'some-attr',
tableAttributeValue: 'some-val'
});
expect(toHtml([
'---',
'propa: vala',
'propb: valb',
'---',
''
])).toEqual(
'<table some-attr="some-val">' +
'<thead><tr><th>propa</th><th>propb</th></tr></thead>' +
'<tbody><tr><td>vala</td><td>valb</td></tr></tbody>' +
'</table>'
);
});
});
describe('marker', () => {
it('sets the marker for parsing', () => {
md = new Markdown().use(markdownItGithubPreamble, {
marker: '~'
});
expect(toHtml([
'~~~',
'propa: vala',
'propb: valb',
'~~~',
''
])).toEqual(
'<table>' +
'<thead><tr><th>propa</th><th>propb</th></tr></thead>' +
'<tbody><tr><td>vala</td><td>valb</td></tr></tbody>' +
'</table>'
);
expect(toHtml([
'---',
'propa: vala',
'propb: valb',
'---',
''
])).not.toMatch(/<table>/);
});
});
describe('render', () => {
it('sets an alternative renderer for the opening/closing table element', () => {
md = new Markdown().use(markdownItGithubPreamble, {
render: (tokens, idx /*, _options, env, self*/) => {
return '[' + tokens[idx].type + ']';
}
});
expect(toHtml([
'---',
'propa: vala',
'propb: valb',
'---',
'',
'abcdef'
])).toEqual(
'[preamble_open]' +
'[preamble_thead_open]' +
'[preamble_tr_open]' +
'[preamble_th_open]propa[preamble_th_close]' +
'[preamble_th_open]propb[preamble_th_close]' +
'[preamble_tr_close]' +
'[preamble_thead_close]' +
'[preamble_tbody_open]' +
'[preamble_tr_open]' +
'[preamble_td_open]vala[preamble_td_close]' +
'[preamble_td_open]valb[preamble_td_close]' +
'[preamble_tr_close]' +
'[preamble_tbody_close]' +
'[preamble_close]' +
'<p>abcdef</p>'
);
});
});
describe('name', () => {
it('sets the render block name to avoid conflicts with other plugins', () => {
md = new Markdown().use(markdownItGithubPreamble, {
render: (tokens, idx /*, _options, env, self*/) => {
return '[' + tokens[idx].type + ']';
},
name: 'PPX'
});
expect(toHtml([
'---',
'propa: vala',
'propb: valb',
'---',
'',
'abcdef'
])).toEqual(
'[PPX_open]' +
'[PPX_thead_open]' +
'[PPX_tr_open]' +
'[PPX_th_open]propa[PPX_th_close]' +
'[PPX_th_open]propb[PPX_th_close]' +
'[PPX_tr_close]' +
'[PPX_thead_close]' +
'[PPX_tbody_open]' +
'[PPX_tr_open]' +
'[PPX_td_open]vala[PPX_td_close]' +
'[PPX_td_open]valb[PPX_td_close]' +
'[PPX_tr_close]' +
'[PPX_tbody_close]' +
'[PPX_close]' +
'<p>abcdef</p>'
);
});
});
});
});