|
| 1 | +import type MarkdownIt from 'markdown-it' |
| 2 | +import type { RenderRule } from 'markdown-it/lib/renderer' |
| 3 | +import container from 'markdown-it-container' |
| 4 | + |
| 5 | +export async function containerPlugin(md: MarkdownIt) { |
| 6 | + md |
| 7 | + .use(...createComptContainer( |
| 8 | + 'code-group', |
| 9 | + () => '<CodeGroup>\n', |
| 10 | + () => '</CodeGroup>\n', |
| 11 | + ), |
| 12 | + ) |
| 13 | + .use(...createComptContainer( |
| 14 | + 'code-group-item', |
| 15 | + (info: string) => `<CodeGroupItem title="${info}">\n`, |
| 16 | + () => '</CodeGroupItem>\n', |
| 17 | + ), |
| 18 | + ) |
| 19 | + .use(...createComptContainer( |
| 20 | + 'ul', |
| 21 | + () => '<StepFlow type="ul">\n', |
| 22 | + () => '</StepFlow>\n', |
| 23 | + ), |
| 24 | + ) |
| 25 | + .use(...createComptContainer( |
| 26 | + 'ol', |
| 27 | + () => '<StepFlow type="ol">\n', |
| 28 | + () => '</StepFlow>\n', |
| 29 | + ), |
| 30 | + ) |
| 31 | + .use(...createComptContainer( |
| 32 | + 'li', |
| 33 | + (info: string) => { |
| 34 | + return ` |
| 35 | +<StepFlowItem> |
| 36 | +<template #title> |
| 37 | +${md.renderInline(info)} |
| 38 | +</template> |
| 39 | +` |
| 40 | + }, |
| 41 | + () => '</StepFlowItem>\n', |
| 42 | + ), |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +type ContainerArgs = [typeof container, string, { render: RenderRule }] |
| 47 | +type RenderPlaceFunction = (info: string) => string |
| 48 | + |
| 49 | +function createComptContainer( |
| 50 | + name: string, |
| 51 | + before: RenderPlaceFunction, |
| 52 | + after: RenderPlaceFunction, |
| 53 | +): ContainerArgs { |
| 54 | + return [ |
| 55 | + container, |
| 56 | + name, |
| 57 | + { |
| 58 | + render(tokens, idx) { |
| 59 | + const infoStack: string[] = [] |
| 60 | + const token = tokens[idx] |
| 61 | + if (token.nesting === 1) { |
| 62 | + const info = token.info.trim().slice(name.length).trim() |
| 63 | + infoStack.push(info) |
| 64 | + return before(info) |
| 65 | + } |
| 66 | + else { |
| 67 | + const info = infoStack.pop() || '' |
| 68 | + return after(info) |
| 69 | + } |
| 70 | + }, |
| 71 | + }, |
| 72 | + ] |
| 73 | +} |
| 74 | + |
| 75 | +export default containerPlugin |
0 commit comments