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 | const highlightjs = require('highlight.js');
|
7 |
|
8 |
|
9 | let res;
|
10 | const id = 'comp' + Math.random();
|
11 |
|
12 | const oldCode = renderer.code;
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function escape(html, encode) {
|
21 | return html
|
22 | .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
|
23 | .replace(/</g, '<')
|
24 | .replace(/>/g, '>')
|
25 | .replace(/"/g, '"')
|
26 | .replace(/'/g, ''');
|
27 | }
|
28 |
|
29 | function unescape(html) {
|
30 |
|
31 | return html.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/g, function(_, n) {
|
32 | n = n.toLowerCase();
|
33 | if (n === 'colon') return ':';
|
34 | if (n.charAt(0) === '#') {
|
35 | return n.charAt(1) === 'x'
|
36 | ? String.fromCharCode(parseInt(n.substring(2), 16))
|
37 | : String.fromCharCode(+n.substring(1));
|
38 | }
|
39 | return '';
|
40 | });
|
41 | }
|
42 |
|
43 | renderer.code = (code, language, escaped) => {
|
44 | res = code;
|
45 |
|
46 | const validLang = !!(language && highlightjs.getLanguage(language));
|
47 |
|
48 | const highlighted = validLang ? highlightjs.highlight(language, code).value : code;
|
49 |
|
50 | return `<pre><code class="hljs ${language}">${highlighted}</code></pre>`;
|
51 | }
|
52 |
|
53 | renderer.table = function (header, body) {
|
54 | return '<table class="table table-striped">'+header+body+'</table>'
|
55 | }
|
56 |
|
57 | renderer.list = function(body, ordered) {
|
58 | var type = ordered ? 'ol' : 'ul';
|
59 | var styles = ordered ? 'upper-roman' : 'disc';
|
60 | return '<' + type + ' style="padding-left:40px; list-style-type:'+ styles + ';" >\n' + body + '</' + type + '>\n';
|
61 | }
|
62 |
|
63 | function process(source) {
|
64 | source = marked(source, {
|
65 | renderer: renderer,
|
66 | gfm: true,
|
67 | tables: true,
|
68 | breaks: true,
|
69 | pedantic: false,
|
70 | sanitize: true,
|
71 | smartLists: true,
|
72 | smartypants: false
|
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 |