1 | const marked = require('./marked');
|
2 | const babel = require('babel-core');
|
3 | const renderer = new marked.Renderer();
|
4 | const yamlFront = require('yaml-front-matter')
|
5 |
|
6 | let res;
|
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 | return `
|
44 | <div>
|
45 | <div id=${id}></div>
|
46 | <pre><code>${escape(code)}</code></pre>
|
47 | </div>`;
|
48 | }
|
49 |
|
50 | renderer.table = function (header, body) {
|
51 | return '<table class="table table-striped">'+header+body+'</table>'
|
52 | }
|
53 |
|
54 | renderer.list = function(body, ordered) {
|
55 | var type = ordered ? 'ol' : 'ul';
|
56 | var styles = ordered ? 'upper-roman' : 'disc';
|
57 | return '<' + type + ' style="padding-left:40px; list-style-type:'+ styles + ';" >\n' + body + '</' + type + '>\n';
|
58 | }
|
59 |
|
60 | function process(source) {
|
61 | source = marked(source, {
|
62 | renderer: renderer,
|
63 | gfm: true,
|
64 | tables: true,
|
65 | breaks: true,
|
66 | pedantic: false,
|
67 | sanitize: true,
|
68 | smartLists: true,
|
69 | smartypants: false,
|
70 | highlight: function (code) {
|
71 | return require('highlight.js').highlightAuto(code).value;
|
72 | }
|
73 | });
|
74 |
|
75 | const res = JSON.stringify(source);
|
76 | return res;
|
77 | }
|
78 |
|
79 |
|
80 | module.exports = function(source, map){
|
81 | this.cacheable && this.cacheable();
|
82 |
|
83 | res = '';
|
84 | const yaml = yamlFront.loadFront(source, 'content');
|
85 |
|
86 | var md = process(yaml.content);
|
87 | res += `\n export const md=${md}; \n export const id="${id}";`
|
88 | console.log('----------------')
|
89 | console.log(res);
|
90 | console.log('--------------')
|
91 |
|
92 | this.callback(null, res, map);
|
93 |
|
94 | } |
\ | No newline at end of file |