-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
57 lines (43 loc) · 1.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"use strict";
var marked = require("marked");
var loaderUtils = require("loader-utils");
var assign = require("object-assign");
var cheerio = require('cheerio');
var hl = require('highlight.js');
var highlightAuto = hl.highlightAuto;
// default option
var options = {
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: true,
smartLists: true,
smartypants: false
};
module.exports = function(markdown) {
var query = loaderUtils.parseQuery(this.query);
var configKey = query.config || "markdownLoader";
var options = assign({}, options, query, this.options[configKey]);
marked.setOptions(options);
var markdownTpl = marked(markdown);
this && this.cacheable && this.cacheable();
var $ = cheerio.load(markdownTpl);
$('pre code').replaceWith(function(i, block) {
var $e = $(block);
var text = $e.text();
var klass = $e.attr('class') || '';
var languageType = klass.split('lang-').filter(id);
if (languageType.length) {
return highlightAuto(text, languageType).value;
} else {
return highlightAuto(text).value;
}
});
$('pre').addClass('hljs');
return $.html();
};
function id(type) {
return type;
}