UNPKG

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