| 1 | import { defineCodeblockTransformer } from '@slidev/types' |
| 2 | import { escapeVueInCode, extractBracedExpression, normalizeRangeStr } from '../utils' |
| 3 | |
| 4 | const RE_BLOCK_INFO = /^([\w'#+-]+)?([ \w\t'-]*)(?:\[([^\]]*)\])?[ \t]*(?:\{([\w,|\-*]+)\})?[ \t]*/ |
| 5 | |
| 6 | export default defineCodeblockTransformer(async ({ info, renderHighlighted }) => { |
| 7 | const match = info.match(RE_BLOCK_INFO)! |
| 8 | const [, lang = '', meta = '', title = '', rangeStr = ''] = match |
| 9 | let rest = info.slice(match[0].length) |
| 10 | const options = rest.startsWith('{') |
| 11 | ? extractBracedExpression(rest) |
| 12 | : undefined |
| 13 | if (options) |
| 14 | rest = rest.slice(options.length) |
| 15 | const ranges = normalizeRangeStr(rangeStr) |
| 16 | const optionsProp = options |
| 17 | ? `v-bind="${options}"` |
| 18 | : '' |
| 19 | const code = await renderHighlighted({ info: `${lang} ${meta}${rest}` }) |
| 20 | return `<CodeBlockWrapper ${optionsProp} title=${JSON.stringify(title)} :ranges='${JSON.stringify(ranges)}'>${escapeVueInCode(code)}</CodeBlockWrapper>` |
| 21 | }) |
| 22 |