-
-
Notifications
You must be signed in to change notification settings - Fork 320
/
hast.test.ts
95 lines (81 loc) · 3.92 KB
/
hast.test.ts
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
/* eslint-disable style/no-tabs */
import { afterEach, describe, expect, it, vi } from 'vitest'
import { toHtml } from 'hast-util-to-html'
import { codeToHtml, getHighlighter } from '../src'
afterEach(() => {
vi.restoreAllMocks
})
describe('should', () => {
it('works', async () => {
const shiki = await getHighlighter({
themes: ['vitesse-light'],
langs: ['javascript'],
})
const hast = shiki.codeToHast('console.log\nfoo.bar', { lang: 'js', theme: 'vitesse-light' })
expect(toHtml(hast))
.toMatchInlineSnapshot(`
"<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34" tabindex="0"><code><span class="line"><span style="color:#B07D48">console</span><span style="color:#999999">.</span><span style="color:#B07D48">log</span></span>
<span class="line"><span style="color:#B07D48">foo</span><span style="color:#999999">.</span><span style="color:#B07D48">bar</span></span></code></pre>"
`)
})
it('structure inline', async () => {
const shiki = await getHighlighter({
themes: ['vitesse-light'],
langs: ['javascript'],
})
const hast = shiki.codeToHast('console.log\nfoo.bar', {
lang: 'js',
theme: 'vitesse-light',
structure: 'inline',
})
expect(toHtml(hast))
.toMatchInlineSnapshot(`"<span style="color:#B07D48">console</span><span style="color:#999999">.</span><span style="color:#B07D48">log</span><br><span style="color:#B07D48">foo</span><span style="color:#999999">.</span><span style="color:#B07D48">bar</span>"`)
})
})
it('hasfocus support', async () => {
const snippet = '$foo = "bar";\n'
+ '$test = "owo"; // [!code focus]\n'
+ '$bar = "baz";'
const code = await codeToHtml(snippet, {
lang: 'php',
theme: 'vitesse-light',
transformers: [
{
code(node) {
node.properties.class = 'language-php'
},
span(node, line, col, parent) {
node.children.forEach((child) => {
if (child.type === 'text' && child.value.includes('[!code focus]')) {
parent.properties['data-has-focus'] = 'true'
node.children.splice(node.children.indexOf(child), 1)
}
})
},
},
],
})
expect(code)
.toMatchInlineSnapshot(`
"<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34" tabindex="0"><code class="language-php"><span class="line"><span style="color:#999999">$</span><span style="color:#B07D48">foo</span><span style="color:#999999"> =</span><span style="color:#B5695999"> "</span><span style="color:#B56959">bar</span><span style="color:#B5695999">"</span><span style="color:#999999">;</span></span>
<span class="line" data-has-focus="true"><span style="color:#999999">$</span><span style="color:#B07D48">test</span><span style="color:#999999"> =</span><span style="color:#B5695999"> "</span><span style="color:#B56959">owo</span><span style="color:#B5695999">"</span><span style="color:#999999">;</span><span style="color:#A0ADA0"></span></span>
<span class="line"><span style="color:#999999">$</span><span style="color:#B07D48">bar</span><span style="color:#999999"> =</span><span style="color:#B5695999"> "</span><span style="color:#B56959">baz</span><span style="color:#B5695999">"</span><span style="color:#999999">;</span></span></code></pre>"
`)
})
it('render whitespace', async () => {
const snippet = [
' space()',
'\t\ttab()',
].join('\n')
const code = await codeToHtml(snippet, {
lang: 'js',
theme: 'vitesse-light',
transformers: [
],
})
expect(code)
.toMatchInlineSnapshot(`
"<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34" tabindex="0"><code><span class="line"><span style="color:#59873A"> space</span><span style="color:#999999">()</span></span>
<span class="line"><span style="color:#59873A"> tab</span><span style="color:#999999">()</span></span></code></pre>"
`)
})