-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (63 loc) · 2.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const visit = require("unist-util-visit");
const { parse } = require('@vue/compiler-sfc')
const { isCodeVueSfc } = require("vue-inbrowser-compiler-utils");
const getImports = require("./getImports");
module.exports = function attacher({ liveFilter } = {}) {
return (ast) => visit(ast, "code", visitor);
function visitor(node) {
let { lang, meta } = node;
if (
liveFilter
? !liveFilter(lang, meta)
: !/live$/.test(meta) && !/live /.test(meta)
) {
return;
}
const getScript = (code) => {
// script is at the beginning of a line after a return
// In case we are loading a vue component as an example, extract script tag
if (isCodeVueSfc(code)) {
const parts = parse(code);
return parts && parts.descriptor && parts.descriptor.script ? parts.descriptor.script.content : "";
}
//else it could be the weird almost jsx of vue-styleguidist
return code.split(/\n[\t ]*</)[0];
};
const code = node.value;
// analyze code to find requires
// put all requires into a "requires" object
// add this as a prop
const scr = getScript(code);
const requires = getImports(scr).map(
(mod) => `'${mod}': require('${mod}')`
);
const codeClean = code
.replace(/\`/g, "\\`")
.replace(/\$/g, "\\$")
.replace(/"/g, """)
.replace(/</g, "<")
.replace(/>/g, ">");
const editorPropsArray = /\{.+\}/.exec(meta);
const editorProps = editorPropsArray ? editorPropsArray[0] : undefined;
const metaArray = meta ? meta.replace(editorProps, "").split(" ") : [];
const jsx = metaArray.length > 2 && metaArray[1] === "jsx" ? "jsx " : ""; // to enable jsx, we want ```vue jsx live or ```jsx jsx live
const markdownGenerated = `<vue-live ${jsx}
:layout-props="{lang:'${lang}'}"${
requires.length
? `
:requires="{${requires.join(",")}}"`
: ""
}
:code="\`${codeClean}\`" ${
editorProps
? `
:editor-props="${editorProps
.replace(/"/g, """)
.replace(/</g, "<")
.replace(/>/g, ">")}"`
: ""
} ></vue-live>`;
node.type = "html";
node.value = markdownGenerated;
}
};