| 1 | import { defineCodeblockTransformer } from '@slidev/types' |
| 2 | import { escapeVueInCode, normalizeRangeStr } from '../utils' |
| 3 | |
| 4 | // eslint-disable-next-line regexp/no-super-linear-backtracking |
| 5 | const RE_BLOCK_INFO = /^([\w'-]+)?(?:[ \t]*|[ \t][ \w\t'-]*)(?:\[([^\]]*)\])?[ \t]*(?:\{([\w,|\-*]+)\})?[ \t]*(\{[^}]*\})?(.*)$/ |
| 6 | |
| 7 | export default defineCodeblockTransformer(async ({ info, renderHighlighted }) => { |
| 8 | const [, lang = '', title = '', rangeStr = '', options, rest = ''] = info.match(RE_BLOCK_INFO) ?? [] |
| 9 | const ranges = normalizeRangeStr(rangeStr) |
| 10 | const optionsProp = options ? `v-bind="${options}"` : '' |
| 11 | const code = await renderHighlighted({ info: `${lang} ${rest}` }) |
| 12 | return `<CodeBlockWrapper ${optionsProp} title=${JSON.stringify(title)} :ranges='${JSON.stringify(ranges)}'>${escapeVueInCode(code)}</CodeBlockWrapper>` |
| 13 | }) |
| 14 |