返回 slidev
wrapper.test.ts
根目录 / packages / slidev / node / syntax / codeblock / wrapper.test.ts
1 import MarkdownExit from 'markdown-exit'
2 import * as shiki from 'shiki'
3 import { beforeAll, describe, expect, it } from 'vitest'
4 import { MarkdownItCodeblocks } from '.'
5 import MarkdownItShiki from '../shiki'
6
7 it('keeps languages containing `#` or `+`', async () => {
8 const md = MarkdownExit({ html: true })
9 const options = {
10 data: { config: {} },
11 mode: 'dev',
12 utils: {
13 shiki,
14 shikiOptions: { theme: 'nord' },
15 },
16 } as any
17
18 md.use(await MarkdownItShiki(options))
19 md.use(MarkdownItCodeblocks, options, [])
20
21 const result = await md.renderAsync([
22 '```c# [Program.cs] {1}',
23 'using System;',
24 '```',
25 ].join('\n'))
26
27 expect(result).toContain('title="Program.cs"')
28 expect(result).toContain(':ranges=\'["1"]\'')
29 expect(result).toContain('<code class="language-c#">')
30 expect(result).toContain('<span style="color:#81A1C1">using</span>')
31 })
32
33 it('keeps nested objects in code block options', async () => {
34 const md = MarkdownExit({ html: true })
35 const options = {
36 data: { config: {} },
37 mode: 'dev',
38 utils: {
39 shiki,
40 shikiOptions: { theme: 'nord' },
41 },
42 } as any
43
44 md.use(await MarkdownItShiki(options))
45 md.use(MarkdownItCodeblocks, options, [])
46
47 const result = await md.renderAsync([
48 '```ts {1} { maxHeight: \'100px\', foo: { bar: \'}\' } }',
49 'const a = 1',
50 '```',
51 ].join('\n'))
52
53 expect(result).toContain('v-bind="{ maxHeight: \'100px\', foo: { bar: \'}\' } }"')
54 expect(result).toContain(':ranges=\'["1"]\'')
55 expect(result).toContain('<code class="language-ts">')
56 })
57
58 describe('twoslash', { timeout: 20_000 }, () => {
59 const md = MarkdownExit({ html: true })
60
61 beforeAll(async () => {
62 const options = {
63 data: {
64 config: { twoslash: true },
65 },
66 mode: 'dev',
67 utils: {
68 shiki,
69 shikiOptions: { theme: 'nord' },
70 },
71 } as any
72
73 md.use(await MarkdownItShiki(options))
74 md.use(MarkdownItCodeblocks, options, [])
75 })
76
77 it.each([
78 ['ts twoslash', '', '[]'],
79 ['ts {} twoslash', '', '[]'],
80 ['ts {2} twoslash', '', '["2"]'],
81 ['ts twoslash {2}', '', '["2"]'],
82 ['ts twoslash [main.ts]', 'main.ts', '[]'],
83 ['ts twoslash [main.ts] {2}', 'main.ts', '["2"]'],
84 ])('keeps twoslash, title and ranges for `%s`', async (info, title, ranges) => {
85 const result = await md.renderAsync([
86 `\`\`\`${info}`,
87 'const a = 1',
88 'const b = a + 1',
89 '// ^?',
90 '```',
91 ].join('\n'))
92
93 expect(result).toContain(`title=${JSON.stringify(title)}`)
94 expect(result).toContain(`:ranges='${ranges}'`)
95 expect(result).toContain('twoslash-hover')
96 expect(result).not.toContain('^?')
97 })
98 })
99
99 lines TYPESCRIPT