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