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 | res += code + '\n';
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | return `
|
51 | <div>
|
52 | <div id=${id}></div>
|
53 | <pre><code>${escape(code)}</code></pre>
|
54 | </div>`;
|
55 | }
|
56 |
|
57 | renderer.table = function (header, body) {
|
58 | return '<table class="table table-striped">'+header+body+'</table>'
|
59 | }
|
60 |
|
61 | renderer.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 |
|
67 | function 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 |
|
84 | module.exports = function(source, map){
|
85 | this.cacheable && this.cacheable();
|
86 |
|
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 |
|
95 | } |
\ | No newline at end of file |