返回 slidev
snippet.test.ts
根目录 / packages / slidev / node / syntax / snippet.test.ts
1 import MarkdownExit from 'markdown-exit'
2 import path from 'pathe'
3 import { expect, it } from 'vitest'
4 import { monacoWriterWhitelist } from '../vite/monacoWrite'
5 import MarkdownItSnippet, { resolveSnippetImport } from './snippet'
6
7 const fixturesRoot = path.join(__dirname, '../../../../test/fixtures/')
8
9 const options = {
10 userRoot: fixturesRoot,
11 userWorkspaceRoot: fixturesRoot,
12 roots: [fixturesRoot],
13 data: {
14 watchFiles: {},
15 slides: [
16 {
17 index: 0,
18 source: {
19 filepath: path.join(fixturesRoot, 'test.md'),
20 },
21 },
22 ],
23 },
24 } as any
25
26 it('snippet import', async () => {
27 const md = MarkdownExit()
28 md.use(MarkdownItSnippet, options)
29
30 const result = await md.renderAsync('<<< @/snippets/snippet.ts#snippet', { id: 'slides.md__slidev_1.md' })
31
32 expect(result).toMatchInlineSnapshot(`
33 "<pre><code class="language-ts">function _foo() {
34 // ...
35 }
36 </code></pre>
37 "
38 `)
39 })
40
41 it('snippet in indented block', async () => {
42 const md = MarkdownExit()
43 md.use(MarkdownItSnippet, options)
44
45 const result = await md.renderAsync('- item\n <<< @/snippets/snippet.ts#snippet', { id: 'slides.md__slidev_1.md' })
46
47 expect(result).toMatchInlineSnapshot(`
48 "<ul>
49 <li>item<pre><code class="language-ts">function _foo() {
50 // ...
51 }
52 </code></pre>
53 </li>
54 </ul>
55 "
56 `)
57 })
58
59 it('not transform in code block', async () => {
60 const md = MarkdownExit()
61 md.use(MarkdownItSnippet, options)
62
63 const result = await md.renderAsync('```\n<<< @/snippets/snippet.ts\n```', { id: 'slides.md__slidev_1.md' })
64
65 expect(result).toMatchInlineSnapshot(`
66 "<pre><code>&lt;&lt;&lt; @/snippets/snippet.ts
67 </code></pre>
68 "
69 `)
70 })
71
72 it('resolves a snippet path that stays inside the allowed roots', () => {
73 const slide = {
74 source: {
75 filepath: path.join(fixturesRoot, 'test.md'),
76 },
77 } as any
78 const result = resolveSnippetImport('<<< @/snippets/snippet.ts#snippet', fixturesRoot, slide, [fixturesRoot])
79 expect(result?.src).toBe(path.join(fixturesRoot, 'snippets/snippet.ts').replaceAll('\\', '/'))
80 })
81
82 it('throws when a snippet path escapes the allowed roots', () => {
83 const slide = {
84 source: {
85 filepath: path.join(fixturesRoot, 'test.md'),
86 },
87 } as any
88 expect(() => resolveSnippetImport('<<< ../../../../../outside.ts', fixturesRoot, slide, [fixturesRoot]))
89 .toThrow(/escapes the project root/)
90 })
91
92 it('whitelists a {monaco-write} snippet under the path the writer resolves', async () => {
93 monacoWriterWhitelist.clear()
94 const md = MarkdownExit()
95 md.use(MarkdownItSnippet, options)
96
97 const result = await md.renderAsync('<<< @/snippets/snippet.ts {monaco-write}', { id: 'slides.md__slidev_1.md' })
98
99 // createMonacoWriterPlugin does path.resolve(userRoot, file), so the value it
100 // is handed has to resolve back to the file that was read.
101 expect([...monacoWriterWhitelist]).toEqual(['snippets/snippet.ts'])
102 expect(result).toContain('writable="snippets/snippet.ts"')
103 // pathe normalises separators, so this is the writer's path.resolve verbatim.
104 expect(path.resolve(fixturesRoot, [...monacoWriterWhitelist][0]))
105 .toBe(path.join(fixturesRoot, 'snippets/snippet.ts'))
106 })
107
108 it('registers the snippet in the current watchFiles after the data is reloaded', async () => {
109 const data = {
110 watchFiles: {} as Record<string, Set<number>>,
111 slides: options.data.slides,
112 }
113 const md = MarkdownExit()
114 md.use(MarkdownItSnippet, { ...options, data })
115
116 // The dev server swaps in fresh objects when it reloads the slides.
117 data.watchFiles = {}
118 data.slides = [
119 {
120 index: 0,
121 source: {
122 filepath: path.join(fixturesRoot, 'test.md'),
123 },
124 },
125 ]
126
127 await md.renderAsync('<<< @/snippets/snippet.ts', { id: 'slides.md__slidev_1.md' })
128
129 expect(data.watchFiles).toEqual({
130 [path.join(fixturesRoot, 'snippets/snippet.ts')]: new Set([0]),
131 })
132 })
133
133 lines TYPESCRIPT