|
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 : ""; |
16 | 25 | }
|
17 | 26 |
|
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} |
48 | 44 | :layoutProps="{lang:'${langClean}'}"
|
49 | 45 | :code="\`${codeClean}\`"
|
50 |
| - :requires="{${requires.join(",")}}" |
51 | 46 | ${editorProps ? ` :editorProps="${editorProps}"` : ""}
|
52 | 47 | />`;
|
53 |
| - return noSsr |
54 |
| - ? `<no-ssr>${markdownGenerated}</no-ssr>` |
55 |
| - : markdownGenerated; |
56 |
| - }; |
57 |
| - }; |
58 |
| -}; |
| 48 | + |
| 49 | + node.value = markdownGenerated; |
| 50 | + } |
| 51 | +} |
0 commit comments