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