UNPKG

2.66 kBJavaScriptView Raw
1const marked = require('./marked');
2const babel = require('babel-core');
3const renderer = new marked.Renderer();
4const yamlFront = require('yaml-front-matter')
5
6const highlightjs = require('highlight.js');
7
8
9let res;
10const id = 'comp' + Math.random();
11
12const oldCode = renderer.code;
13
14// marked.setOptions({
15// highlight: function (code) {
16// return require('highlight.js').highlightAuto(code).value;
17// }
18// });
19
20function escape(html, encode) {
21 return html
22 .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
23 .replace(/</g, '&lt;')
24 .replace(/>/g, '&gt;')
25 .replace(/"/g, '&quot;')
26 .replace(/'/g, '&#39;');
27}
28
29function unescape(html) {
30 // explicitly match decimal, hex, and named HTML entities
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
43renderer.code = (code, language, escaped) => {
44 res = code;
45
46 return `<code>${code}</code>`
47
48 const validLang = !!(language && highlightjs.getLanguage(language));
49 // Highlight only if the language is valid.
50 const highlighted = validLang ? highlightjs.highlight(language, code).value : code;
51 // Render the highlighted code with `hljs` class.
52 return `<pre><code class="hljs ${language}">${highlighted}</code></pre>`;
53}
54
55renderer.table = function (header, body) {
56 return '<table class="table table-striped">'+header+body+'</table>'
57}
58
59renderer.list = function(body, ordered) {
60 var type = ordered ? 'ol' : 'ul';
61 var styles = ordered ? 'upper-roman' : 'disc';
62 return '<' + type + ' style="padding-left:40px; list-style-type:'+ styles + ';" >\n' + body + '</' + type + '>\n';
63}
64
65function process(source) {
66 source = marked(source, {
67 renderer: renderer,
68 gfm: true,
69 tables: true,
70 breaks: true,
71 pedantic: false,
72 sanitize: true,
73 smartLists: true,
74 smartypants: false
75 });
76
77 const res = JSON.stringify(source);
78 return res;
79}
80
81
82module.exports = function(source, map){
83 this.cacheable && this.cacheable();
84
85 res = '';
86 const yaml = yamlFront.loadFront(source, 'content');
87 //对source进行解析
88 var md = process(yaml.content);
89 res += `\n export const md=${md}; \n export const id="${id}";`
90 console.log('----------------')
91 console.log(res);
92 console.log('--------------')
93
94 this.callback(null, res, map);
95 // return "module.exports = " + '{}';
96}
\No newline at end of file