| 1 | import lz from 'lz-string' |
| 2 | import MarkdownExit from 'markdown-exit' |
| 3 | import path from 'pathe' |
| 4 | import * as shiki from 'shiki' |
| 5 | import { expect, it } from 'vitest' |
| 6 | import { MarkdownItCodeblocks } from '.' |
| 7 | |
| 8 | it('resolves snippet imports before magic move validation', async () => { |
| 9 | const md = MarkdownExit({ html: true }) |
| 10 | const userRoot = path.join(__dirname, '../../../../../test/fixtures/') |
| 11 | const watchFiles: Record<string, Set<number>> = {} |
| 12 | |
| 13 | md.use(MarkdownItCodeblocks, { |
| 14 | userRoot, |
| 15 | data: { |
| 16 | watchFiles, |
| 17 | slides: [{ |
| 18 | index: 0, |
| 19 | source: { filepath: path.join(userRoot, 'test.md') }, |
| 20 | }], |
| 21 | config: { lineNumbers: false }, |
| 22 | }, |
| 23 | utils: { |
| 24 | shiki, |
| 25 | shikiOptions: { theme: 'nord' }, |
| 26 | }, |
| 27 | } as any, []) |
| 28 | |
| 29 | const result = await md.renderAsync([ |
| 30 | '````md magic-move', |
| 31 | '<<< @/snippets/snippet.ts#snippet ts', |
| 32 | '<<< @/snippets/snippet.ts ts {1}', |
| 33 | '````', |
| 34 | ].join('\n'), { id: 'slides.md__slidev_1.md' }) |
| 35 | |
| 36 | expect(result).toContain('<ShikiMagicMove ') |
| 37 | expect(result).toContain(':step-ranges=\'[[],["1"]]\'') |
| 38 | |
| 39 | const encodedSteps = result.match(/steps-lz=([^ ]+)/)?.[1]?.slice(1, -1) |
| 40 | expect(encodedSteps).toBeTruthy() |
| 41 | |
| 42 | const steps = JSON.parse(lz.decompressFromBase64(encodedSteps!)!) as Array<{ lang: string }> |
| 43 | expect(steps).toHaveLength(2) |
| 44 | expect(steps.map(step => step.lang)).toEqual(['ts', 'ts']) |
| 45 | |
| 46 | const watched = Object.values(watchFiles) |
| 47 | expect(watched).toHaveLength(1) |
| 48 | expect(watched[0]).toEqual(new Set([0])) |
| 49 | }) |
| 50 |