UNPKG

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