Skip to content

Commit d5e705a

Browse files
authored
Merge pull request #20 from vue-styleguidist/docs-show-code-copy
docs: show how to use the copy plugin when wanted
2 parents 84af7fa + fe9920a commit d5e705a

File tree

5 files changed

+125
-70
lines changed

5 files changed

+125
-70
lines changed

README.md

+51-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ yarn add -D vuepress-plugin-live
1919
//.vuepress/config.js
2020
module.exports = {
2121
//...
22-
plugins: [
23-
["live"],
24-
]
25-
}
22+
plugins: [["live"]],
23+
};
2624
```
2725

2826
## Usage
@@ -33,3 +31,52 @@ In your markdown file just add a `live` flag to your fenced code blocks.
3331
<button>example</button>
3432
```
3533
</code></pre>
34+
35+
## Options
36+
37+
### noSsr
38+
39+
Avoid server side rendering the components in components if they are not ssr ready. Remember that vuepress build pre-compiles the html pages you need.
40+
41+
#### default
42+
43+
`false`
44+
45+
#### example
46+
47+
```js
48+
//.vuepress/config.js
49+
module.exports = {
50+
//...
51+
plugins: [["live", { noSsr: true }]],
52+
};
53+
```
54+
55+
### liveFilter
56+
57+
Allows users of theis plugin to say what fenced blocks will be rendered with vue-live.
58+
59+
#### default
60+
61+
```js
62+
(lang) => / live$/.test(lang) && / live /.test(lang);
63+
```
64+
65+
#### example
66+
67+
```js
68+
//.vuepress/config.js
69+
module.exports = {
70+
//...
71+
plugins: [
72+
[
73+
"live",
74+
{
75+
liveFilter(lang) {
76+
return ["vue", "js", "jsx"].includes(lang);
77+
},
78+
},
79+
],
80+
],
81+
};
82+
```

docs/.vuepress/config.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,28 @@ module.exports = {
88
editLinks: true,
99
docsDir: "docs",
1010
search: false,
11-
sidebar: ["/", "/Install.md"]
11+
sidebar: ["/", "/Install.md"],
1212
},
1313
plugins: [
14+
["code-copy", { selector: `div:not(.editor)[class*="language-"] pre` }],
1415
[
1516
require("../../index"),
1617
{
1718
// uncomment next line to use a custom layout
1819
// layout: path.resolve(__dirname, "./components/custom-layout.vue"),
19-
noSsr: true
20-
}
20+
noSsr: true,
21+
},
2122
],
2223
[
2324
"@vuepress/register-components",
2425
{
2526
components: [
2627
{
2728
name: "vue-slider",
28-
path: path.resolve(__dirname, "../vue-slider")
29-
}
30-
]
31-
}
32-
]
33-
]
29+
path: path.resolve(__dirname, "../vue-slider"),
30+
},
31+
],
32+
},
33+
],
34+
],
3435
};

markDownPlugin.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@ const { parseComponent } = require("vue-template-compiler");
22
const { isCodeVueSfc } = require("vue-inbrowser-compiler");
33
const getImports = require("./getImports");
44

5-
const addVueLive = ({ noSsr }) => md => {
5+
const addVueLive = ({ noSsr, liveFilter }) => (md) => {
66
const fence = md.renderer.rules.fence;
77
md.renderer.rules.fence = (...args) => {
88
const [tokens, idx] = args;
99
const token = tokens[idx];
1010
const lang = token.info.trim();
1111

1212
// if it does not ends with live just use default fence
13-
if (!/ live$/.test(lang) && !/ live /.test(lang)) {
13+
if (
14+
liveFilter
15+
? !liveFilter(lang)
16+
: !/ live$/.test(lang) && !/ live /.test(lang)
17+
) {
1418
return fence(...args);
1519
}
1620

17-
const getScript = code => {
21+
const getScript = (code) => {
1822
// script is at the beginning of a line after a return
1923
// In case we are loading a vue component as an example, extract script tag
2024
if (isCodeVueSfc(code)) {
@@ -32,14 +36,16 @@ const addVueLive = ({ noSsr }) => md => {
3236
// put all requires into a "requires" object
3337
// add this as a prop
3438
const scr = getScript(code);
35-
const requires = getImports(scr).map(mod => `'${mod}': require('${mod}')`);
39+
const requires = getImports(scr).map(
40+
(mod) => `'${mod}': require('${mod}')`
41+
);
3642
const langArray = lang.split(" ");
3743
const langClean = langArray[0];
3844
const codeClean = md.utils
3945
.escapeHtml(code)
4046
.replace(/\`/g, "\\`")
4147
.replace(/\$/g, "\\$");
42-
const editorProps = langArray.find(l => /^\{.+\}$/.test(l));
48+
const editorProps = langArray.find((l) => /^\{.+\}$/.test(l));
4349
const jsx = langArray.length > 2 && langArray[1] === "jsx" ? "jsx " : ""; // to enable jsx, we want ```vue jsx live or ```jsx jsx live
4450
const markdownGenerated = `<vue-live ${jsx}
4551
:layoutProps="{lang:'${langClean}'}"

package-lock.json

+52-52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"devDependencies": {
2929
"@vuepress/plugin-register-components": "^1.0.3",
3030
"lodash": "^4.17.11",
31+
"lodash.throttle": "^4.1.1",
3132
"remark": "^10.0.1",
3233
"unist-util-visit": "^1.4.0",
3334
"validate-commit-msg": "^2.14.0",

0 commit comments

Comments
 (0)