forked from mapbox/rehype-prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
87 lines (77 loc) · 2.28 KB
/
test.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
'use strict';
const rehype = require('rehype');
const dedent = require('dedent');
const rehypePrism = require('./index');
const svelte = require('@snlab/refractor-svelte');
const processHtml = (html, options) => {
return rehype()
.data('settings', { fragment: true })
.use(rehypePrism, options)
.processSync(html)
.toString();
};
test('copies the language- class to pre tag', () => {
const result = processHtml(dedent`
<pre><code class="language-css"></code></pre>
`);
expect(result).toMatchSnapshot();
});
test('finds code and highlights', () => {
const result = processHtml(dedent`
<div>
<p>foo</p>
<pre><code class="language-css">p { color: red }</code></pre>
</div>
`);
expect(result).toMatchSnapshot();
});
test('handles uppercase languages correctly', () => {
const result = processHtml(dedent`
<div>
<p>foo</p>
<pre><code class="language-CSS">p { color: red }</code></pre>
</div>
`);
expect(result).toMatchSnapshot();
});
test('does nothing to code block without language- class', () => {
const result = processHtml(dedent`
<pre><code>p { color: red }</code></pre>
`);
expect(result).toMatchSnapshot();
});
test('throw error with fake language- class', () => {
expect(() => {
processHtml(dedent`
<pre><code class="language-thisisnotalanguage">p { color: red }</code></pre>
`);
}).toThrow(/Unknown language/);
});
test('with options.ignoreMissing, does nothing to code block with fake language- class', () => {
const html = dedent`
<pre><code class="language-thisisnotalanguage">p { color: red }</code></pre>
`;
const result = processHtml(html, { ignoreMissing: true });
expect(result).toMatchSnapshot();
});
test('throw error with wrongly specified options.registerSyntax', () => {
expect(() => {
processHtml(
dedent`
<pre><code class="language-css"></code></pre>
`,
{ registerSyntax: true }
);
}).toThrow(/should be an array/);
});
test('additional language syntax gets registered and applied correctly', () => {
const html = dedent`
<pre><code class="language-svelte">
{#each items as item, i}
{item.name}
{/each}
</code></pre>
`;
const result = processHtml(html, { registerSyntax: [svelte] });
expect(result).toMatchSnapshot();
});