Skip to content

Commit ead8efd

Browse files
committed
feat: add code demo
1 parent 877a4fe commit ead8efd

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"svgo": "^1.3.2",
7575
"svgo-loader": "^2.2.2",
7676
"vuepress": "^1.8.2",
77+
"vuepress-plugin-demo-container-v2": "^2.3.4",
7778
"vuepress-plugin-flowchart": "^1.4.3",
7879
"vuepress-plugin-redirect": "^1.2.5"
7980
}

src/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
'use strict';
22

33
module.exports = [
4-
require('./extends'),
5-
require('./commands'),
6-
];
4+
'extends',
5+
'commands',
6+
].map(name => {
7+
const item = require(`./${name}`);
8+
if (!item.configuration) {
9+
item.configuration = {};
10+
}
11+
if (!item.configuration.alias) {
12+
item.configuration.alias = `${name.replace(/\//, '_')}`;
13+
}
14+
return item;
15+
});

theme/plugins/markdown/mdCodeResult.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,29 @@
44
// https://github.com/ravenq/markdown-it-vue/blob/master/src/markdown-it-plugin-echarts.js
55

66
module.exports = md => {
7+
78
const fence = md.renderer.rules.fence.bind(md.renderer.rules);
89
md.renderer.rules.fence = (...args) => {
9-
const [ tokens, idx ] = args;
10+
const [tokens, idx] = args;
1011
const token = tokens[idx];
11-
const rawCode = fence(...args);
12-
const lang = token.info.trim();
1312
const code = token.content;
13+
14+
const lang = token.info.trim();
15+
// 判断该 fence 是否在 :::demo 内
16+
const prevToken = tokens[idx - 1];
17+
const isInDemoContainer = prevToken && prevToken.nesting === 1 && prevToken.info.trim().match(/^demo\s*(.*)$/);
18+
19+
const rawCode = fence(...args);
1420
let mdMyCode = '';
15-
if ([ 'vue', 'html' ].includes(lang)) {
21+
if (['vue', 'html'].includes(lang) && !isInDemoContainer) {
1622
mdMyCode = `<CodeResult lang="${lang}" code="${encodeURIComponent(code.trim())}">${code.trim()}</CodeResult>`;
1723
} else {
1824
mdMyCode = `<CodeResult lang="${lang}" code="${encodeURIComponent(code.trim())}"></CodeResult>`;
1925
}
26+
27+
if (['vue', 'html'].includes(lang) && isInDemoContainer) {
28+
mdMyCode = `<template slot="highlight">${mdMyCode}}</template>`;
29+
}
2030
return rawCode.replace('<!--beforeend-->', `${mdMyCode}<!--beforeend-->`);
2131
};
2232
};

theme/plugins/register.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ function registerPlugins(ctx) {
4141
'/zh/': '警告',
4242
},
4343
}],
44+
45+
// vue-demo
46+
'demo-container-v2',
4447
];
4548

4649
const siteConfig = ctx.siteConfig || {};

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12462,6 +12462,11 @@ [email protected]:
1246212462
dependencies:
1246312463
"@vuepress/shared-utils" "^1.2.0"
1246412464

12465+
vuepress-plugin-demo-container-v2@^2.3.4:
12466+
version "2.3.4"
12467+
resolved "https://registry.yarnpkg.com/vuepress-plugin-demo-container-v2/-/vuepress-plugin-demo-container-v2-2.3.4.tgz#80daef15aca102666fb5429a41dadaf7a0d4fa79"
12468+
integrity sha512-xyPocP7U8J0BVlStj8fkeT9Z9c/ScUiPgARg+NyPMFrjxC9I3Ae1DfnHs6cJC4zFB1DeyVLzeWuIaP7Q2d9wng==
12469+
1246512470
vuepress-plugin-disqus@^0.2.0:
1246612471
version "0.2.0"
1246712472
resolved "https://registry.yarnpkg.com/vuepress-plugin-disqus/-/vuepress-plugin-disqus-0.2.0.tgz#0a5bde424f81e0185eda36fba23d7ac4cae6ff07"

0 commit comments

Comments
 (0)