UNPKG

2.43 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 res += code + '\n';
40 // console.log(babel.transform(code, {
41 // plugins: ["transform-react-jsx"]
42 // }));
43 // const comp = babel.transform(code, {
44 // plugins: ["transform-react-jsx"]
45 // });
46
47 // eval(comp);
48
49
50 return `
51 <div>
52 <div id=${id}></div>
53 <pre><code>${escape(code)}</code></pre>
54 </div>`;
55}
56
57renderer.table = function (header, body) {
58 return '<table class="table table-striped">'+header+body+'</table>'
59}
60
61renderer.list = function(body, ordered) {
62 var type = ordered ? 'ol' : 'ul';
63 var styles = ordered ? 'upper-roman' : 'disc';
64 return '<' + type + ' style="padding-left:40px; list-style-type:'+ styles + ';" >\n' + body + '</' + type + '>\n';
65}
66
67function process(source) {
68 source = marked(source, {
69 renderer: renderer,
70 gfm: true,
71 tables: true,
72 breaks: true,
73 pedantic: false,
74 sanitize: true,
75 smartLists: true,
76 smartypants: false
77 });
78
79 const res = JSON.stringify(source);
80 return res;
81}
82
83
84module.exports = function(source, map){
85 this.cacheable && this.cacheable();
86 //对source进行解析
87 var md = process(source);
88 res += `\n export const md=${md}; \n export const id="${id}";`
89 console.log('----------------')
90 console.log(res);
91 console.log('--------------')
92
93 this.callback(null, res, map);
94 // return "module.exports = " + '{}';
95}
\No newline at end of file