Skip to content

Commit 9ac8e0f

Browse files
committed
chore: tweaks
1 parent ec42a7e commit 9ac8e0f

File tree

1 file changed

+54
-35
lines changed
  • plugins/markdown/plugin-demo/src/node

1 file changed

+54
-35
lines changed
Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,51 @@
11
import { container } from '@mdit/plugin-container'
2+
import type { MarkdownItDemoOptions } from '@mdit/plugin-demo'
23
import { demo } from '@mdit/plugin-demo'
34
import { encodeData } from '@vuepress/helper'
45
import type { PluginSimple } from 'markdown-it'
6+
import type MarkdownIt from 'markdown-it'
57
import type Token from 'markdown-it/lib/token.mjs'
68
import { escapeHtml } from './utils.js'
79

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()
1519

16-
let config = ''
17-
const code: Record<string, string> = {}
20+
let config = ''
21+
const code: Record<string, string> = {}
1822

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+
: ''
2529

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+
}
3236

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+
}
4246

4347
export const markdownDemo: PluginSimple = (md) => {
44-
md.use(demo, {
45-
name: 'md-demo',
48+
const demoOptions: MarkdownItDemoOptions = {
4649
openRender: (tokens, index) =>
4750
`<MdDemo title="${escapeHtml(
4851
tokens[index].info,
@@ -73,11 +76,27 @@ export const markdownDemo: PluginSimple = (md) => {
7376
contentOpenRender: () => `<template #default>\n`,
7477
contentCloseRender: () => `</template>\n`,
7578
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,
7688
})
7789
}
7890

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+
}
8095

81-
export const vueDemo: PluginSimple = getPlugin('vue-demo')
96+
export const vueDemo: PluginSimple = (md) => {
97+
registerVPDemo(md, 'vue-demo', 'vue')
98+
}
8299

83-
export const reactDemo: PluginSimple = getPlugin('react-demo')
100+
export const reactDemo: PluginSimple = (md) => {
101+
registerVPDemo(md, 'react-demo', 'react')
102+
}

0 commit comments

Comments
 (0)