UNPKG

2.49 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;
10let id;
11
12function escape(html, encode) {
13 return html
14 .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
15 .replace(/</g, '&lt;')
16 .replace(/>/g, '&gt;')
17 .replace(/"/g, '&quot;')
18 .replace(/'/g, '&#39;');
19}
20
21function unescape(html) {
22 // explicitly match decimal, hex, and named HTML entities
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
35renderer.code = (code, language, escaped) => {
36 res = code;
37
38 const validLang = !!(language && highlightjs.getLanguage(language));
39 // Highlight only if the language is valid.
40 const highlighted = validLang ? highlightjs.highlight(language, code).value : code;
41 // Render the highlighted code with `hljs` class.
42 return `<div> <div id=${id}></div><pre><code class="hljs ${language}">${highlighted}</code></pre></div>`;
43}
44
45renderer.table = function (header, body) {
46 return '<table class="table table-striped">'+header+body+'</table>'
47}
48
49renderer.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
55function 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
72module.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 //对source进行解析
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 // return "module.exports = " + '{}';
87}
\No newline at end of file