Skip to content

Commit 659656f

Browse files
Bart LedouxBart Ledoux
authored andcommitted
fix: use remark syntax instead of markdownit
1 parent 6a4dcc3 commit 659656f

File tree

3 files changed

+115
-53
lines changed

3 files changed

+115
-53
lines changed

index.js

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,51 @@
1-
module.exports = function getPlugin({ noSsr, liveFilter }) {
2-
return function addVueLiveToFencedCode(md) {
3-
const fence = md.renderer.rules.fence;
4-
md.renderer.rules.fence = (...args) => {
5-
const [tokens, idx] = args;
6-
const token = tokens[idx];
7-
const lang = token.info.trim();
8-
9-
// if it does not ends with live just use default fence
10-
if (
11-
liveFilter
12-
? !liveFilter(lang)
13-
: !/ live$/.test(lang) && !/ live /.test(lang)
14-
) {
15-
return fence(...args);
1+
const visit = require("unist-util-visit");
2+
const { parseComponent } = require("vue-template-compiler");
3+
const { isCodeVueSfc } = require("vue-inbrowser-compiler-utils");
4+
5+
export default function attacher({ liveFilter } = {}) {
6+
return (ast) => visit(ast, "code", visitor);
7+
8+
function visitor(node) {
9+
let { lang } = node;
10+
11+
if (
12+
liveFilter
13+
? !liveFilter(lang)
14+
: !/ live$/.test(lang) && !/ live /.test(lang)
15+
) {
16+
return;
17+
}
18+
19+
const getScript = (code) => {
20+
// script is at the beginning of a line after a return
21+
// In case we are loading a vue component as an example, extract script tag
22+
if (isCodeVueSfc(code)) {
23+
const parts = parseComponent(code);
24+
return parts && parts.script ? parts.script.content : "";
1625
}
1726

18-
const getScript = (code) => {
19-
// script is at the beginning of a line after a return
20-
// In case we are loading a vue component as an example, extract script tag
21-
if (isCodeVueSfc(code)) {
22-
const parts = parseComponent(code);
23-
return parts && parts.script ? parts.script.content : "";
24-
}
25-
26-
//else it could be the weird almost jsx of vue-styleguidist
27-
return code.split(/\n[\t ]*</)[0];
28-
};
29-
30-
const code = token.content;
31-
32-
// analyze code to find requires
33-
// put all requires into a "requires" object
34-
// add this as a prop
35-
const scr = getScript(code);
36-
const requires = getImports(scr).map(
37-
(mod) => `'${mod}': require('${mod}')`
38-
);
39-
const langArray = lang.split(" ");
40-
const langClean = langArray[0];
41-
const codeClean = md.utils
42-
.escapeHtml(code)
43-
.replace(/\`/g, "\\`")
44-
.replace(/\$/g, "\\$");
45-
const editorProps = langArray.find((l) => /^\{.+\}$/.test(l));
46-
const jsx = langArray.length > 2 && langArray[1] === "jsx" ? "jsx " : ""; // to enable jsx, we want ```vue jsx live or ```jsx jsx live
47-
const markdownGenerated = `<vue-live ${jsx}
27+
//else it could be the weird almost jsx of vue-styleguidist
28+
return code.split(/\n[\t ]*</)[0];
29+
};
30+
31+
const code = node.value;
32+
33+
// analyze code to find requires
34+
// put all requires into a "requires" object
35+
// add this as a prop
36+
const scr = getScript(code);
37+
38+
const langArray = lang.split(" ");
39+
const langClean = langArray[0];
40+
const codeClean = code.replace(/\`/g, "\\`").replace(/\$/g, "\\$");
41+
const editorProps = langArray.find((l) => /^\{.+\}$/.test(l));
42+
const jsx = langArray.length > 2 && langArray[1] === "jsx" ? "jsx " : ""; // to enable jsx, we want ```vue jsx live or ```jsx jsx live
43+
const markdownGenerated = `<vue-live ${jsx}
4844
:layoutProps="{lang:'${langClean}'}"
4945
:code="\`${codeClean}\`"
50-
:requires="{${requires.join(",")}}"
5146
${editorProps ? ` :editorProps="${editorProps}"` : ""}
5247
/>`;
53-
return noSsr
54-
? `<no-ssr>${markdownGenerated}</no-ssr>`
55-
: markdownGenerated;
56-
};
57-
};
58-
};
48+
49+
node.value = markdownGenerated;
50+
}
51+
}

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,13 @@
1818
"bugs": {
1919
"url": "https://github.com/vue-styleguidist/remark-plugin-vue-live/issues"
2020
},
21-
"homepage": "https://github.com/vue-styleguidist/remark-plugin-vue-live#readme"
21+
"homepage": "https://github.com/vue-styleguidist/remark-plugin-vue-live#readme",
22+
"dependencies": {
23+
"unist-util-visit": "^2.0.3",
24+
"vue-inbrowser-compiler-utils": "^4.32.1",
25+
"vue-template-compiler": "^2.6.12"
26+
},
27+
"peerDependencies": {
28+
"vue-live": "^1.14.0"
29+
}
2230
}

pnpm-lock.yaml

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)