UNPKG

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