1 | const marked = require('./marked');
|
2 | const babel = require('babel-core');
|
3 | const renderer = new marked.Renderer();
|
4 |
|
5 | let res = '';
|
6 |
|
7 | const id = 'comp' + Math.random();
|
8 |
|
9 | const oldCode = renderer.code;
|
10 |
|
11 | function escape(html, encode) {
|
12 | return html
|
13 | .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
|
14 | .replace(/</g, '<')
|
15 | .replace(/>/g, '>')
|
16 | .replace(/"/g, '"')
|
17 | .replace(/'/g, ''');
|
18 | }
|
19 |
|
20 | function unescape(html) {
|
21 |
|
22 | return html.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/g, function(_, n) {
|
23 | n = n.toLowerCase();
|
24 | if (n === 'colon') return ':';
|
25 | if (n.charAt(0) === '#') {
|
26 | return n.charAt(1) === 'x'
|
27 | ? String.fromCharCode(parseInt(n.substring(2), 16))
|
28 | : String.fromCharCode(+n.substring(1));
|
29 | }
|
30 | return '';
|
31 | });
|
32 | }
|
33 |
|
34 | renderer.code = (code, lang, escaped) => {
|
35 | res = code;
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | return `
|
47 | <div>
|
48 | <div id=${id}></div>
|
49 | ${escape(code)}
|
50 | </div>`;
|
51 | }
|
52 |
|
53 | function process(source) {
|
54 | source = marked(source, {renderer: renderer});
|
55 | const res = JSON.stringify(source);
|
56 | return res;
|
57 | }
|
58 |
|
59 |
|
60 | module.exports = function(source, map){
|
61 | this.cacheable && this.cacheable();
|
62 |
|
63 | var md = process(source);
|
64 | res += `\n export const md=${md}; \n export const id="${id}"`
|
65 | console.log('----------------')
|
66 | console.log(res);
|
67 | console.log('--------------')
|
68 |
|
69 | this.callback(null, res, map);
|
70 |
|
71 | } |
\ | No newline at end of file |