Skip to content

Commit fe9920a

Browse files
author
Bart Ledoux
committed
feat: add liveFilter predicate to replace live
2 parents 2062a9b + 12e6fab commit fe9920a

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

README.md

Lines changed: 51 additions & 4 deletions
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+
```

markDownPlugin.js

Lines changed: 11 additions & 5 deletions
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}'}"

0 commit comments

Comments
 (0)