| 1 | import { defineCodeblockTransformer } from '@slidev/types' |
| 2 | import lz from 'lz-string' |
| 3 | |
| 4 | // eslint-disable-next-line regexp/no-super-linear-backtracking |
| 5 | const RE_MONACO = /^([\w'-]+)?\s*\{(monaco[\w-]*)\}\s*(\{[^}]*\})?(.*)$/ |
| 6 | const RE_DIFF_SEPARATOR = /^\s*~~~\s*\n/m |
| 7 | |
| 8 | export default defineCodeblockTransformer(async ({ info, code, options: { data: { config }, mode } }) => { |
| 9 | const match = info.match(RE_MONACO) |
| 10 | if (!match) |
| 11 | return |
| 12 | const [, lang = '', monaco, options] = match |
| 13 | |
| 14 | const monacoEnabled = config.monaco === true || config.monaco === mode |
| 15 | if (!monacoEnabled) { |
| 16 | return |
| 17 | } |
| 18 | |
| 19 | let encoded |
| 20 | let diff = '' |
| 21 | if (monaco === 'monaco-diff') { |
| 22 | const [original, modified] = code.split(RE_DIFF_SEPARATOR, 2) |
| 23 | encoded = lz.compressToBase64(original) |
| 24 | diff = modified === undefined ? '' : `diff-lz="${lz.compressToBase64(modified)}"` |
| 25 | } |
| 26 | else { |
| 27 | encoded = lz.compressToBase64(code) |
| 28 | } |
| 29 | |
| 30 | const optionsProp = options ? `v-bind="${options}"` : '' |
| 31 | const runnable = monaco === 'monaco-run' ? 'runnable' : '' |
| 32 | return `<Monaco ${optionsProp} ${runnable} lang="${lang}" code-lz="${encoded}" ${diff} />` |
| 33 | }) |
| 34 |