UNPKG

2.36 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 });
71
72 const res = JSON.stringify(source);
73 return res;
74}
75
76
77module.exports = function(source, map){
78 this.cacheable && this.cacheable();
79
80 res = '';
81 const yaml = yamlFront.loadFront(source, 'content');
82 //对source进行解析
83 var md = process(yaml.content);
84 res += `\n export const md=${md}; \n export const id="${id}";`
85 console.log('----------------')
86 console.log(res);
87 console.log('--------------')
88
89 this.callback(null, res, map);
90 // return "module.exports = " + '{}';
91}
\No newline at end of file