/
core.test.ts
182 lines (151 loc) · 7.45 KB
/
core.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
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
import { describe, expect, it } from 'vitest'
import { getHighlighterCore } from '../src/core'
import js from '../src/assets/langs/javascript'
import ts from '../src/assets/langs/typescript'
import nord from '../src/assets/themes/nord'
import mtp from '../src/assets/themes/material-theme-palenight'
// eslint-disable-next-line antfu/no-import-dist
import { wasmBinary } from '../../core/dist/wasm-inlined.mjs'
describe('should', () => {
it('works', async () => {
const shiki = await getHighlighterCore({
themes: [nord],
langs: [js as any],
loadWasm: {
instantiator: obj => WebAssembly.instantiate(wasmBinary, obj),
},
})
expect(shiki.codeToHtml('console.log("Hi")', { lang: 'javascript', theme: 'nord' }))
.toMatchInlineSnapshot(`"<pre class="shiki nord" style="background-color:#2e3440ff;color:#d8dee9ff" tabindex="0"><code><span class="line"><span style="color:#D8DEE9">console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">"</span><span style="color:#A3BE8C">Hi</span><span style="color:#ECEFF4">"</span><span style="color:#D8DEE9FF">)</span></span></code></pre>"`)
})
it('dynamic load theme and lang', async () => {
const shiki = await getHighlighterCore({
themes: [nord],
langs: [
js,
import('../src/assets/langs/c'),
],
loadWasm: {
// https://github.com/WebAssembly/esm-integration/tree/main/proposals/esm-integration
instantiator: obj => WebAssembly.instantiate(wasmBinary, obj).then(r => r.instance.exports),
},
})
await shiki.loadLanguage(() => import('../src/assets/langs/python'))
await shiki.loadTheme(() => import('../dist/themes/vitesse-light.mjs').then(m => m.default))
expect(shiki.getLoadedLanguages())
.toMatchInlineSnapshot(`
[
"javascript",
"c",
"python",
"js",
"py",
]
`)
expect(shiki.getLoadedThemes())
.toMatchInlineSnapshot(`
[
"nord",
"vitesse-light",
]
`)
expect(shiki.codeToHtml('print 1', { lang: 'python', theme: 'vitesse-light' }))
.toMatchInlineSnapshot(`"<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34" tabindex="0"><code><span class="line"><span style="color:#998418">print</span><span style="color:#2F798A"> 1</span></span></code></pre>"`)
})
it('requires nested lang', async () => {
const shiki = await getHighlighterCore({
themes: [nord],
langs: [
import('../src/assets/langs/cpp'),
],
})
expect(shiki.getLoadedLanguages())
.toMatchInlineSnapshot(`
[
"sql",
"c",
"glsl",
"cpp-macro",
"cpp",
"c++",
]
`)
})
it('works without no initial langs and themes', async () => {
const shiki = await getHighlighterCore()
await shiki.loadLanguage(js)
await shiki.loadTheme(nord)
const code = shiki.codeToHtml('console.log("Hi")', { lang: 'javascript', theme: 'nord' })
expect(code).toMatchInlineSnapshot(`"<pre class="shiki nord" style="background-color:#2e3440ff;color:#d8dee9ff" tabindex="0"><code><span class="line"><span style="color:#D8DEE9">console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">"</span><span style="color:#A3BE8C">Hi</span><span style="color:#ECEFF4">"</span><span style="color:#D8DEE9FF">)</span></span></code></pre>"`)
})
it('works with alias', async () => {
const shiki = await getHighlighterCore({
langAlias: {
mylang: 'javascript',
mylang2: 'js', // nested alias
},
})
await shiki.loadLanguage(js)
await shiki.loadTheme(nord)
const code = shiki.codeToHtml('console.log("Hi")', { lang: 'mylang', theme: 'nord' })
const code2 = shiki.codeToHtml('console.log("Hi")', { lang: 'mylang2', theme: 'nord' })
expect(code).toBe(code2)
expect(code).toMatchInlineSnapshot(`"<pre class="shiki nord" style="background-color:#2e3440ff;color:#d8dee9ff" tabindex="0"><code><span class="line"><span style="color:#D8DEE9">console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">"</span><span style="color:#A3BE8C">Hi</span><span style="color:#ECEFF4">"</span><span style="color:#D8DEE9FF">)</span></span></code></pre>"`)
})
it('works with alias override', async () => {
const shiki = await getHighlighterCore({
langAlias: {
js: 'typescript',
},
})
await shiki.loadLanguage(ts)
await shiki.loadTheme(nord)
const code = shiki.codeToHtml('const a: Foo = 1', { lang: 'js', theme: 'nord' })
expect(code).toMatchInlineSnapshot(`"<pre class="shiki nord" style="background-color:#2e3440ff;color:#d8dee9ff" tabindex="0"><code><span class="line"><span style="color:#81A1C1">const</span><span style="color:#D8DEE9"> a</span><span style="color:#81A1C1">:</span><span style="color:#8FBCBB"> Foo</span><span style="color:#81A1C1"> =</span><span style="color:#B48EAD"> 1</span></span></code></pre>"`)
})
})
describe('errors', () => {
it('throw on invalid theme', async () => {
const shiki = await getHighlighterCore({
themes: [nord],
langs: [js as any],
})
await expect(() => shiki.codeToHtml('console.log("Hi")', { lang: 'javascript', theme: 'invalid' }))
.toThrowErrorMatchingInlineSnapshot(`[ShikiError: Theme \`invalid\` not found, you may need to load it first]`)
})
it('throw on invalid lang', async () => {
const shiki = await getHighlighterCore({
themes: [nord],
langs: [js as any],
})
await expect(() => shiki.codeToHtml('console.log("Hi")', { lang: 'abc', theme: 'nord' }))
.toThrowErrorMatchingInlineSnapshot(`[ShikiError: Language \`abc\` not found, you may need to load it first]`)
})
it('highlight with raw theme registation', async () => {
const shiki = await getHighlighterCore({
themes: [nord],
langs: [js as any],
loadWasm: {
instantiator: obj => WebAssembly.instantiate(wasmBinary, obj),
},
})
const code = shiki.codeToHtml('console.log("Hi")', { lang: 'javascript', theme: mtp })
expect.soft(code)
.toMatchInlineSnapshot(`"<pre class="shiki material-theme-palenight" style="background-color:#292D3E;color:#babed8" tabindex="0"><code><span class="line"><span style="color:#BABED8">console</span><span style="color:#89DDFF">.</span><span style="color:#82AAFF">log</span><span style="color:#BABED8">(</span><span style="color:#89DDFF">"</span><span style="color:#C3E88D">Hi</span><span style="color:#89DDFF">"</span><span style="color:#BABED8">)</span></span></code></pre>"`)
expect.soft(shiki.getLoadedThemes()).toContain('material-theme-palenight')
const code2 = shiki.codeToHtml('console.log("Hi")', { lang: 'javascript', theme: 'material-theme-palenight' })
expect.soft(code2).toBe(code)
})
it('throw on circular alias', async () => {
const shiki = await getHighlighterCore({
langAlias: {
mylang: 'mylang2',
mylang2: 'mylang',
},
})
await shiki.loadLanguage(js)
await shiki.loadTheme(nord)
await expect(() => shiki.codeToHtml('console.log("Hi")', { lang: 'mylang', theme: 'nord' }))
.toThrowErrorMatchingInlineSnapshot(`[ShikiError: Circular alias \`mylang -> mylang2 -> mylang\`]`)
})
})