UNPKG

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