UNPKG

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