UNPKG

1.75 kBJavaScriptView Raw
1const marked = require('./marked');
2const babel = require('babel-core');
3const renderer = new marked.Renderer();
4
5let res = '';
6
7const id = 'comp' + Math.random();
8
9const oldCode = renderer.code;
10
11function escape(html, encode) {
12 return html
13 .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
14 .replace(/</g, '&lt;')
15 .replace(/>/g, '&gt;')
16 .replace(/"/g, '&quot;')
17 .replace(/'/g, '&#39;');
18}
19
20function unescape(html) {
21 // explicitly match decimal, hex, and named HTML entities
22 return html.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/g, function(_, n) {
23 n = n.toLowerCase();
24 if (n === 'colon') return ':';
25 if (n.charAt(0) === '#') {
26 return n.charAt(1) === 'x'
27 ? String.fromCharCode(parseInt(n.substring(2), 16))
28 : String.fromCharCode(+n.substring(1));
29 }
30 return '';
31 });
32}
33
34renderer.code = (code, lang, escaped) => {
35 res = code;
36 // console.log(babel.transform(code, {
37 // plugins: ["transform-react-jsx"]
38 // }));
39 // const comp = babel.transform(code, {
40 // plugins: ["transform-react-jsx"]
41 // });
42
43 // eval(comp);
44
45
46 return `
47 <div>
48 <div id=${id}></div>
49 ${escape(code)}
50 </div>`;
51}
52
53function process(source) {
54 source = marked(source, {renderer: renderer});
55 const res = JSON.stringify(source);
56 return res;
57}
58
59
60module.exports = function(source, map){
61 this.cacheable && this.cacheable();
62 //对source进行解析
63 var md = process(source);
64 res += `\n export const md=${md}; \n export const id="${id}"`
65 console.log('----------------')
66 console.log(res);
67 console.log('--------------')
68
69 this.callback(null, res, map);
70 // return "module.exports = " + '{}';
71}
\No newline at end of file