Skip to content

Commit 6cc2ec4

Browse files
committed
feat: support custom demo component, use demo、description、source slots
1 parent fe42ba9 commit 6cc2ec4

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

src/DemoBlock.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@mouseleave="hovering = false"
77
>
88
<div class="demo-content">
9-
<slot></slot>
9+
<slot name="demo"></slot>
1010
</div>
1111
<div class="meta" ref="meta">
1212
<div class="description" v-if="$slots.description">

src/common/containers.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
const mdContainer = require('markdown-it-container');
22

3-
module.exports = md => {
4-
md.use(mdContainer, 'demo', {
5-
validate(params) {
6-
return params.trim().match(/^demo\s*(.*)$/);
7-
},
8-
render(tokens, idx) {
9-
const m = tokens[idx].info.trim().match(/^demo\s*(.*)$/);
10-
if (tokens[idx].nesting === 1) {
11-
const description = m && m.length > 1 ? m[1] : '';
12-
const content = tokens[idx + 1].type === 'fence' ? tokens[idx + 1].content : '';
13-
return `<demo-block>
14-
<!--pre-render-demo:${content}:pre-render-demo-->
15-
${description ? `<div slot="description">${md.render(description).html}</div>` : ''}
16-
<template slot="source">
17-
`;
3+
module.exports = options => {
4+
const {
5+
component = 'demo-block'
6+
} = options;
7+
const componentName = component
8+
.replace(/^\S/, s => s.toLowerCase())
9+
.replace(/([A-Z])/g, "-$1").toLowerCase();
10+
return md => {
11+
md.use(mdContainer, 'demo', {
12+
validate(params) {
13+
return params.trim().match(/^demo\s*(.*)$/);
14+
},
15+
render(tokens, idx) {
16+
const m = tokens[idx].info.trim().match(/^demo\s*(.*)$/);
17+
if (tokens[idx].nesting === 1) {
18+
const description = m && m.length > 1 ? m[1] : '';
19+
const content = tokens[idx + 1].type === 'fence' ? tokens[idx + 1].content : '';
20+
return `<${componentName}>
21+
<template slot="demo"><!--pre-render-demo:${content}:pre-render-demo--></template>
22+
${description ? `<div slot="description">${md.render(description).html}</div>` : ''}
23+
<template slot="source">
24+
`;
25+
}
26+
return `</template></${componentName}>`;
1827
}
19-
return '</template></demo-block>';
20-
}
21-
});
22-
};
28+
});
29+
};
30+
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = (options = {}, ctx) => {
99
enhanceAppFiles: path.resolve(__dirname, './enhanceAppFile.js'),
1010
chainMarkdown(config) {
1111
config.plugin('containers')
12-
.use(demoBlockContainers)
12+
.use(demoBlockContainers(options))
1313
.end();
1414
},
1515
extendMarkdown: md => {

0 commit comments

Comments
 (0)