UNPKG

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