| 1 | import type { SlideInfo } from '@slidev/types' |
| 2 | import { toKeyedTokens } from '@shikijs/magic-move/core' |
| 3 | import { defineCodeblockTransformer } from '@slidev/types' |
| 4 | import lz from 'lz-string' |
| 5 | import { resolveSnippetImport } from '../snippet' |
| 6 | import { normalizeRangeStr } from '../utils' |
| 7 | |
| 8 | const RE_MAGIC_MOVE_INFO = /^(?:md|markdown) magic-move\s*(?:\[([^\]]*)\])?\s*(\{[^}]*\})?/ |
| 9 | // eslint-disable-next-line regexp/no-super-linear-backtracking |
| 10 | const RE_CODE_BLOCK = /^```([\w'#+-]+)?(?:[ \t]*|[ \t][ \w\t'-]*)(?:\[([^\]]*)\])?[ \t]*(?:\{([\w*,|-]+)\}[ \t]*(\{[^}]*\})?([^\r\n]*))?\r?\n((?:(?!^```)[\s\S])*?)^```$/gm |
| 11 | const RE_INNER_CODE_FENCE = /^```/ |
| 12 | const RE_LINES_TRUE = /\blines: *true\b/ |
| 13 | const RE_LINES_FALSE = /\blines: *false\b/ |
| 14 | |
| 15 | function parseLineNumbersOption(options: string) { |
| 16 | return RE_LINES_TRUE.test(options) |
| 17 | ? true |
| 18 | : RE_LINES_FALSE.test(options) |
| 19 | ? false |
| 20 | : undefined |
| 21 | } |
| 22 | |
| 23 | function resolveMagicMoveSnippetImports(code: string, userRoot: string, slide: SlideInfo, watchFiles: Record<string, Set<number>>) { |
| 24 | let inCodeBlock = false |
| 25 | |
| 26 | return code.split(/\r?\n/).map((line) => { |
| 27 | if (RE_INNER_CODE_FENCE.test(line)) { |
| 28 | inCodeBlock = !inCodeBlock |
| 29 | return line |
| 30 | } |
| 31 | |
| 32 | if (inCodeBlock) |
| 33 | return line |
| 34 | |
| 35 | const snippet = resolveSnippetImport(line, userRoot, slide) |
| 36 | if (!snippet) |
| 37 | return line |
| 38 | |
| 39 | watchFiles[snippet.src] ??= new Set() |
| 40 | watchFiles[snippet.src].add(slide.index) |
| 41 | |
| 42 | const info = `${snippet.lang} ${snippet.meta}`.trim() |
| 43 | const content = snippet.content.endsWith('\n') |
| 44 | ? snippet.content |
| 45 | : `${snippet.content}\n` |
| 46 | return `\`\`\`${info}\n${content}\`\`\`` |
| 47 | }).join('\n') |
| 48 | } |
| 49 | |
| 50 | export default defineCodeblockTransformer(async ({ info, fence, code, slide, options: { userRoot, data: { config, watchFiles }, utils: { shikiOptions, shiki } } }) => { |
| 51 | if (fence !== 4) |
| 52 | return |
| 53 | const match = info.match(RE_MAGIC_MOVE_INFO) |
| 54 | if (!match) |
| 55 | return |
| 56 | const [, title = '', options = '{}'] = match |
| 57 | const defaultLineNumbers = parseLineNumbersOption(options) ?? config.lineNumbers |
| 58 | const resolvedCode = slide |
| 59 | ? resolveMagicMoveSnippetImports(code, userRoot, slide, watchFiles) |
| 60 | : code |
| 61 | const matches = Array.from(resolvedCode.matchAll(RE_CODE_BLOCK)) |
| 62 | if (!matches.length) |
| 63 | throw new Error('Magic Move block must contain at least one code block') |
| 64 | |
| 65 | const ranges = matches.map(i => normalizeRangeStr(i[3])) |
| 66 | const steps = await Promise.all(matches.map(async (i) => { |
| 67 | const lang = i[1] |
| 68 | const lineNumbers = parseLineNumbersOption(i[4]) ?? defaultLineNumbers |
| 69 | const code = i[6].trimEnd() |
| 70 | const options = { |
| 71 | ...shikiOptions, |
| 72 | lang, |
| 73 | } |
| 74 | const { tokens, bg, fg, rootStyle, themeName } = await shiki.codeToTokens(code, options) |
| 75 | return { |
| 76 | ...toKeyedTokens(code, tokens, JSON.stringify([ |
| 77 | lang, |
| 78 | 'themes' in options |
| 79 | ? options.themes |
| 80 | : options.theme, |
| 81 | ]), lineNumbers), |
| 82 | bg, |
| 83 | fg, |
| 84 | rootStyle, |
| 85 | themeName, |
| 86 | lang, |
| 87 | } |
| 88 | })) |
| 89 | const compressed = lz.compressToBase64(JSON.stringify(steps)) |
| 90 | return `<ShikiMagicMove v-bind="${options}" steps-lz="${compressed}" :title='${JSON.stringify(title)}' :step-ranges='${JSON.stringify(ranges)}' />` |
| 91 | }) |
| 92 |