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 | let id;
|
11 |
|
12 | function escape(html, encode) {
|
13 | return html
|
14 | .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
|
15 | .replace(/</g, '<')
|
16 | .replace(/>/g, '>')
|
17 | .replace(/"/g, '"')
|
18 | .replace(/'/g, ''');
|
19 | }
|
20 |
|
21 | function unescape(html) {
|
22 |
|
23 | return html.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/g, function(_, n) {
|
24 | n = n.toLowerCase();
|
25 | if (n === 'colon') return ':';
|
26 | if (n.charAt(0) === '#') {
|
27 | return n.charAt(1) === 'x'
|
28 | ? String.fromCharCode(parseInt(n.substring(2), 16))
|
29 | : String.fromCharCode(+n.substring(1));
|
30 | }
|
31 | return '';
|
32 | });
|
33 | }
|
34 |
|
35 | renderer.code = (code, language, escaped) => {
|
36 | res = code;
|
37 |
|
38 | const validLang = !!(language && highlightjs.getLanguage(language));
|
39 |
|
40 | const highlighted = validLang ? highlightjs.highlight(language, code).value : code;
|
41 |
|
42 | return `<div> <div id=${id}></div><pre><code class="hljs ${language}">${highlighted}</code></pre></div>`;
|
43 | }
|
44 |
|
45 | renderer.table = function (header, body) {
|
46 | return '<table class="table table-striped">'+header+body+'</table>'
|
47 | }
|
48 |
|
49 | renderer.list = function(body, ordered) {
|
50 | var type = ordered ? 'ol' : 'ul';
|
51 | var styles = ordered ? 'upper-roman' : 'disc';
|
52 | return '<' + type + ' style="padding-left:40px; list-style-type:'+ styles + ';" >\n' + body + '</' + type + '>\n';
|
53 | }
|
54 |
|
55 | function process(source) {
|
56 | source = marked(source, {
|
57 | renderer: renderer,
|
58 | gfm: true,
|
59 | tables: true,
|
60 | breaks: true,
|
61 | pedantic: false,
|
62 | sanitize: true,
|
63 | smartLists: true,
|
64 | smartypants: false
|
65 | });
|
66 |
|
67 | const res = JSON.stringify(source);
|
68 | return res;
|
69 | }
|
70 |
|
71 |
|
72 | module.exports = function(source, map){
|
73 | this.cacheable && this.cacheable();
|
74 |
|
75 | res = '';
|
76 | id = 'comp' + Math.random();
|
77 | const yaml = yamlFront.loadFront(source, 'content');
|
78 |
|
79 | var md = process(yaml.content);
|
80 | res += `\n export const md=${md}; \n export const id="${id}";`
|
81 | console.log('----------------')
|
82 | console.log(res);
|
83 | console.log('--------------')
|
84 |
|
85 | this.callback(null, res, map);
|
86 |
|
87 | } |
\ | No newline at end of file |