aboutsummaryrefslogtreecommitdiff
path: root/__tests__/index.js
blob: d6f1ed92b31ed221fb5cbcfe25b3381539e3a36f (plain)
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
'use strict';

import {join} from 'path';
import {readFileSync as file} from 'fs';
import test from 'ava';
import raw from 'rehype-raw';
import reParse from 'remark-parse';
import stringify from 'rehype-stringify';
import remark2rehype from 'remark-rehype';
import unified from 'unified';
import parse5 from 'parse5';
import plugin from '..';

const renderDefault = text => unified()
  .use(reParse)
  .use(plugin)
  .use(remark2rehype)
  .use(stringify)
  .processSync(text);

const render = text => unified()
  .use(reParse)
  .use(plugin, {allowDangerousDOMEventHandlers: false, scope: 'permissive'})
  .use(remark2rehype)
  .use(stringify)
  .processSync(text);

const renderRaw = text => unified()
  .use(reParse)
  .use(plugin, {allowDangerousDOMEventHandlers: false, scope: 'permissive'})
  .use(remark2rehype, {allowDangerousHTML: true})
  .use(raw)
  .use(stringify)
  .processSync(text);

const generateExtendParser = extendsOptions => text => unified()
  .use(reParse)
  .use(plugin, extendsOptions)
  .use(remark2rehype)
  .use(stringify)
  .processSync(text);

const parse = x => parse5.parse(x);

const mainTestString = 'Inline *test*{style="em:4"} paragraph. Use **multiple**{ style="color:pink"} inline ~~block~~ tag. Line `tagCode`{ style="color:yellow"}.';

test('basic-default', t => {
  const {contents} = renderDefault(mainTestString);
  t.deepEqual(parse(contents), parse('<p>Inline <em style="em:4">test</em> paragraph. Use <strong style="color:pink">multiple</strong> inline <del>block</del> tag. Line <code style="color:yellow">tagCode</code>.</p>'));
});

test('basic', t => {
  const {contents} = render(mainTestString);
  t.deepEqual(parse(contents), parse(`
<p>Inline <em style="em:4">test</em> paragraph. Use <strong style="color:pink">multiple</strong> inline <del>block</del> tag. Line <code style="color:yellow">tagCode</code>.</p>`));
});

test('basic-raw', t => {
  const {contents} = renderRaw(mainTestString);
  t.deepEqual(parse(contents), parse(`
<p>Inline <em style="em:4">test</em> paragraph. Use <strong style="color:pink">multiple</strong> inline <del>block</del> tag. Line <code style="color:yellow">tagCode</code>.</p>`));
});

test('em', t => {
  const {contents} = render('textexamplenointerest **Important**{style=4em} still no interest');
  t.deepEqual(parse(contents), parse('<p>textexamplenointerest <strong style="4em">Important</strong> still no interest</p>'));
});

test('readme-default', t => {
  const fileExample = file(join(__dirname, 'readMeTest.txt'));
  const {contents} = renderDefault(fileExample);
  t.deepEqual(parse(contents), parse(`
<p><img src="img" alt="alt" height="50"></p>
<p><a href="https://rms.sexy" rel="external">Hot babe with computer</a></p>
<h3 style="color:red;">This is a title</h3>
<p>Npm stand for <em style="color:yellow;">node</em> packet manager.</p>
<p>This is a <strong>Unicorn</strong> !</p>
<p>Your problem is <del style="color: grey;">at line 18</del>. My mistake, it's at line 14.</p>
<p>You can use the <code>fprintf</code> function to format the output to a file.</p>`));
});

test('readme', t => {
  const fileExample = file(join(__dirname, 'readMeTest.txt'));
  const {contents} = render(fileExample);
  t.deepEqual(parse(contents), parse(`
<p><img src="img" alt="alt" height="50"></p>
<p><a href="https://rms.sexy" rel="external">Hot babe with computer</a></p>
<h3 style="color:red;">This is a title</h3>
<p>Npm stand for <em style="color:yellow;">node</em> packet manager.</p>
<p>This is a <strong awesome="">Unicorn</strong> !</p>
<p>Your problem is <del style="color: grey;">at line 18</del>. My mistake, it's at line 14.</p>
<p>You can use the <code language="c">fprintf</code> function to format the output to a file.</p>`));
});

test('extended', t => {
  const renderExtended = generateExtendParser({extends: {image: ['quality']}});
  const extentedString = `*Wait* !
This is an awesome image : ![Awesome image](aws://image.jpg){ quality="80" awesomeness="max" }
`;
  const {contents} = renderExtended(extentedString);
  t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is an awesome image : <img src="aws://image.jpg" alt="Awesome image"></p>`));
});

test('extended Dangerous', t => {
  const renderExtended = generateExtendParser({extend: {image: ['quality', 'onload']}});
  const dangerousString = `*Wait* !
This is an awesome image : ![Awesome image](aws://image.jpg){ quality="80" awesomeness="max" onload="launchAwesomeFunction();" }
`;
  const {contents} = renderExtended(dangerousString);
  t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is an awesome image : <img src="aws://image.jpg" alt="Awesome image" quality="80" onload="launchAwesomeFunction();"></p>`));
});

test('extended-global', t => {
  const renderExtended = generateExtendParser({extend: {'*': ['ex-attr']}});
  const globalString = ' *Wait* ! You are **beautiful**{ ex-attr="true" } !';
  const {contents} = renderExtended(globalString);
  t.deepEqual(parse(contents), parse('<p> <em>Wait</em> ! You are <strong ex-attr="true">beautiful</strong> !</p>'));
});

test('extended-invalid-scope', t => {
  const renderExtended = generateExtendParser({scope: 'invalid', extend: {strong: ['ex-attr']}});
  const invalidString = '*Wait* ! You are **beautiful**{ ex-attr="true" onload="qdss" pss="NOK" } !';
  const {contents} = renderExtended(invalidString);
  t.deepEqual(parse(contents), parse('<p><em>Wait</em> ! You are <strong ex-attr="true">beautiful</strong> !</p>'));
});

test('invalid-scope', t => {
  const renderExtended = generateExtendParser({extend: 'exAttr'});
  const invalidString = ' *Wait* ! I **love**{ exAttr="true" onload="qdss" pss="NOK" } you !';
  const {contents} = renderExtended(invalidString);
  t.deepEqual(parse(contents), parse('<p> <em>Wait</em> ! I <strong>love</strong> you !</p>'));
});

test('invalid-extend', t => {
  const renderExtended = generateExtendParser({extend: 'exAttr'});
  const invalidString = ' *Wait* ! I **love**{ exAttr="true" onload="qdss" attr="NOK" style="color: red;"} you!';
  const {contents} = renderExtended(invalidString);
  t.deepEqual(parse(contents), parse('<p> <em>Wait</em> ! I <strong style="color: red;">love</strong> you!</p>'));
});

test('global-aria', t => {
  const invalidString = ' *Wait* ! I **love**{ style="color: pink;" aria-love="true" } you!';
  const {contents} = renderDefault(invalidString);
  t.deepEqual(parse(contents), parse('<p> <em>Wait</em> ! I <strong style="color: pink;" aria-love="true">love</strong> you!</p>'));
});

test('fenced code', t => {
  const fencedCodeString = `~~~lang info=string
This is an awesome code

~~~
`;
  const {contents} = render(fencedCodeString);
  t.deepEqual(parse(contents), parse(`<pre><code class="language-lang" info="string">This is an awesome code

</code></pre>`));
});

test('fenced code brackets', t => {
  const fencedCodeString = `~~~lang {info=string}
This is an awesome code

~~~
`;
  const {contents} = render(fencedCodeString);
  t.deepEqual(parse(contents), parse(`<pre><code class="language-lang" info="string">This is an awesome code

</code></pre>`));
});

test('fenced code brackets and spaces', t => {
  const fencedCodeString = `~~~lang   {info=string}
This is an awesome code

~~~
`;
  const {contents} = render(fencedCodeString);
  t.deepEqual(parse(contents), parse(`<pre><code class="language-lang" info="string">This is an awesome code

</code></pre>`));
});

test('global custom attribute', t => {
  const renderExtended = generateExtendParser({extends: {image: ['quality']}});
  const extentedString = `*Wait* !
This is a test image : ![test](img.jpg){data-id=2}
`;
  const {contents} = renderExtended(extentedString);
  t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test" data-id="2"></p>`));

  t.notDeepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test"></p>`));
});

test('global custom attributes 2', t => {
  const renderExtended = generateExtendParser({extends: {image: ['quality']}});
  const extentedString = `*Wait* !
This is a test image : ![test](img.jpg){data-id-node=2}
`;
  const {contents} = renderExtended(extentedString);
  t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test" data-id-node="2"></p>`));

  t.notDeepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test"></p>`));
});

test('global custom attributes 3', t => {
  const renderExtended = generateExtendParser({extends: {image: ['quality']}});
  const extentedString = `*Wait* !
This is a test image : ![test](img.jpg){data--id=2}
`;
  const {contents} = renderExtended(extentedString);
  t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test"></p>`));
});

test('global custom attributes 4', t => {
  const renderExtended = generateExtendParser({extends: {image: ['quality']}});
  const extentedString = `*Wait* !
This is a test image : ![test](img.jpg){data-i=2}
`;
  const {contents} = renderExtended(extentedString);
  t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test" data-i=2></p>`));

  t.notDeepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test"></p>`));
});