UNPKG

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