1 | const marked = require('./marked');
|
2 | const babel = require('babel-core');
|
3 | const renderer = new marked.Renderer();
|
4 |
|
5 | let res = '';
|
6 |
|
7 | const oldCode = renderer.code;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function escape(html, encode) {
|
16 | return html
|
17 | .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
|
18 | .replace(/</g, '<')
|
19 | .replace(/>/g, '>')
|
20 | .replace(/"/g, '"')
|
21 | .replace(/'/g, ''');
|
22 | }
|
23 |
|
24 | function unescape(html) {
|
25 |
|
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 |
|
38 | renderer.code = (code, lang, escaped) => {
|
39 |
|
40 | const id = 'comp' + Math.random();
|
41 |
|
42 | res += code + '\n';
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | return `
|
54 | <div>
|
55 | <div id=${id}></div>
|
56 | <pre><code>${escape(code)}</code></pre>
|
57 | </div>`;
|
58 | }
|
59 |
|
60 | renderer.table = function (header, body) {
|
61 | return '<table class="table table-striped">'+header+body+'</table>'
|
62 | }
|
63 |
|
64 | renderer.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 |
|
70 | function 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 |
|
87 | module.exports = function(source, map){
|
88 | this.cacheable && this.cacheable();
|
89 |
|
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 |
|
98 | } |
\ | No newline at end of file |