UNPKG

2.48 kBJavaScriptView Raw
1const marked = require('./marked');
2const babel = require('babel-core');
3const renderer = new marked.Renderer();
4const yamlFront = require('yaml-front-matter')
5
6let res;
7const id = 'comp' + Math.random();
8
9const oldCode = renderer.code;
10
11// marked.setOptions({
12// highlight: function (code) {
13// return require('highlight.js').highlightAuto(code).value;
14// }
15// });
16
17function escape(html, encode) {
18 return html
19 .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
20 .replace(/</g, '&lt;')
21 .replace(/>/g, '&gt;')
22 .replace(/"/g, '&quot;')
23 .replace(/'/g, '&#39;');
24}
25
26function unescape(html) {
27 // explicitly match decimal, hex, and named HTML entities
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
40renderer.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
50renderer.table = function (header, body) {
51 return '<table class="table table-striped">'+header+body+'</table>'
52}
53
54renderer.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
60function 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
80module.exports = function(source, map){
81 this.cacheable && this.cacheable();
82
83 res = '';
84 const yaml = yamlFront.loadFront(source, 'content');
85 //对source进行解析
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 // return "module.exports = " + '{}';
94}
\No newline at end of file