|
1 | 1 | import { container } from '@mdit/plugin-container'
|
| 2 | +import type { MarkdownItDemoOptions } from '@mdit/plugin-demo' |
2 | 3 | import { demo } from '@mdit/plugin-demo'
|
3 | 4 | import { encodeData } from '@vuepress/helper'
|
4 | 5 | import type { PluginSimple } from 'markdown-it'
|
| 6 | +import type MarkdownIt from 'markdown-it' |
5 | 7 | import type Token from 'markdown-it/lib/token.mjs'
|
6 | 8 | import { escapeHtml } from './utils.js'
|
7 | 9 |
|
8 |
| -const getPlugin = |
9 |
| - (name: string): PluginSimple => |
10 |
| - (md) => { |
11 |
| - container(md, { |
12 |
| - name, |
13 |
| - openRender: (tokens: Token[], index: number): string => { |
14 |
| - const title = tokens[index].info.trimStart().slice(name.length).trim() |
| 10 | +const registerVPDemo = ( |
| 11 | + md: MarkdownIt, |
| 12 | + name: string, |
| 13 | + demoType: string, |
| 14 | +): void => { |
| 15 | + container(md, { |
| 16 | + name, |
| 17 | + openRender: (tokens: Token[], index: number): string => { |
| 18 | + const title = tokens[index].info.trimStart().slice(name.length).trim() |
15 | 19 |
|
16 |
| - let config = '' |
17 |
| - const code: Record<string, string> = {} |
| 20 | + let config = '' |
| 21 | + const code: Record<string, string> = {} |
18 | 22 |
|
19 |
| - for (let i = index; i < tokens.length; i++) { |
20 |
| - const { type, content, info } = tokens[i] |
21 |
| - const language = info |
22 |
| - ? (/^([^ :[{]+)/.exec(md.utils.unescapeAll(info).trim())?.[1] ?? |
23 |
| - 'text') |
24 |
| - : '' |
| 23 | + for (let i = index; i < tokens.length; i++) { |
| 24 | + const { type, content, info } = tokens[i] |
| 25 | + const language = info |
| 26 | + ? (/^([^ :[{]+)/.exec(md.utils.unescapeAll(info).trim())?.[1] ?? |
| 27 | + 'text') |
| 28 | + : '' |
25 | 29 |
|
26 |
| - if (type === `container_${name}_close`) break |
27 |
| - if (!content) continue |
28 |
| - if (type === 'fence') |
29 |
| - if (language === 'json') config = encodeData(content) |
30 |
| - else code[language] = content |
31 |
| - } |
| 30 | + if (type === `container_${name}_close`) break |
| 31 | + if (!content) continue |
| 32 | + if (type === 'fence') |
| 33 | + if (language === 'json') config = encodeData(content) |
| 34 | + else code[language] = content |
| 35 | + } |
32 | 36 |
|
33 |
| - return `<VPDemo id="vp-demo-${index}" type="${name.split('-')[0]}"${ |
34 |
| - title ? ` title="${encodeURIComponent(title)}"` : '' |
35 |
| - }${config ? ` config="${config}"` : ''} code="${encodeData( |
36 |
| - JSON.stringify(code), |
37 |
| - )}">\n` |
38 |
| - }, |
39 |
| - closeRender: () => `</VPDemo>\n`, |
40 |
| - }) |
41 |
| - } |
| 37 | + return `<VPDemo id="vp-demo-${index}" type="${demoType}"${ |
| 38 | + title ? ` title="${encodeURIComponent(title)}"` : '' |
| 39 | + }${config ? ` config="${config}"` : ''} code="${encodeData( |
| 40 | + JSON.stringify(code), |
| 41 | + )}">\n` |
| 42 | + }, |
| 43 | + closeRender: () => `</VPDemo>\n`, |
| 44 | + }) |
| 45 | +} |
42 | 46 |
|
43 | 47 | export const markdownDemo: PluginSimple = (md) => {
|
44 |
| - md.use(demo, { |
45 |
| - name: 'md-demo', |
| 48 | + const demoOptions: MarkdownItDemoOptions = { |
46 | 49 | openRender: (tokens, index) =>
|
47 | 50 | `<MdDemo title="${escapeHtml(
|
48 | 51 | tokens[index].info,
|
@@ -73,11 +76,27 @@ export const markdownDemo: PluginSimple = (md) => {
|
73 | 76 | contentOpenRender: () => `<template #default>\n`,
|
74 | 77 | contentCloseRender: () => `</template>\n`,
|
75 | 78 | closeRender: () => '</MdDemo>\n',
|
| 79 | + } |
| 80 | + |
| 81 | + md.use(demo, { |
| 82 | + name: 'md-demo', |
| 83 | + ...demoOptions, |
| 84 | + }) |
| 85 | + md.use(demo, { |
| 86 | + name: 'markdown-demo', |
| 87 | + ...demoOptions, |
76 | 88 | })
|
77 | 89 | }
|
78 | 90 |
|
79 |
| -export const normalDemo: PluginSimple = getPlugin('demo') |
| 91 | +export const normalDemo: PluginSimple = (md) => { |
| 92 | + registerVPDemo(md, 'demo', 'normal') |
| 93 | + registerVPDemo(md, 'normal-demo', 'normal') |
| 94 | +} |
80 | 95 |
|
81 |
| -export const vueDemo: PluginSimple = getPlugin('vue-demo') |
| 96 | +export const vueDemo: PluginSimple = (md) => { |
| 97 | + registerVPDemo(md, 'vue-demo', 'vue') |
| 98 | +} |
82 | 99 |
|
83 |
| -export const reactDemo: PluginSimple = getPlugin('react-demo') |
| 100 | +export const reactDemo: PluginSimple = (md) => { |
| 101 | + registerVPDemo(md, 'react-demo', 'react') |
| 102 | +} |
0 commit comments