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 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function escape(html, encode) {
|
18 | return html
|
19 | .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
|
20 | .replace(/</g, '<')
|
21 | .replace(/>/g, '>')
|
22 | .replace(/"/g, '"')
|
23 | .replace(/'/g, ''');
|
24 | }
|
25 |
|
26 | function unescape(html) {
|
27 |
|
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 |
|
40 | renderer.code = (code, lang, escaped) => {
|
41 | res = code;
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | return `
|
53 | <div>
|
54 | <div id=${id}></div>
|
55 | <pre><code>${escape(code)}</code></pre>
|
56 | </div>`;
|
57 | }
|
58 |
|
59 | renderer.table = function (header, body) {
|
60 | return '<table class="table table-striped">'+header+body+'</table>'
|
61 | }
|
62 |
|
63 | renderer.list = function(body, ordered) {
|
64 | var type = ordered ? 'ol' : 'ul';
|
65 | return '<' + type + ' style="list-style-type:circle;" >\n' + body + '</' + type + '>\n';
|
66 | }
|
67 |
|
68 | function process(source) {
|
69 | source = marked(source, {
|
70 | renderer: renderer,
|
71 | gfm: true,
|
72 | tables: true,
|
73 | breaks: true,
|
74 | pedantic: false,
|
75 | sanitize: true,
|
76 | smartLists: true,
|
77 | smartypants: false
|
78 | });
|
79 |
|
80 | const res = JSON.stringify(source);
|
81 | return res;
|
82 | }
|
83 |
|
84 |
|
85 | module.exports = function(source, map){
|
86 | this.cacheable && this.cacheable();
|
87 |
|
88 | var md = process(source);
|
89 | res += `\n export const md=${md}; \n export const id="${id}"`
|
90 | console.log('----------------')
|
91 | console.log(res);
|
92 | console.log('--------------')
|
93 |
|
94 | this.callback(null, res, map);
|
95 |
|
96 | } |
\ | No newline at end of file |