-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
53 lines (45 loc) · 1.22 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
'use strict';
var hljs = require('highlight.js');
var utils = require('handlebars-utils');
var Remarkable = require('remarkable');
var defaults = { html: true, breaks: true, highlight: highlight };
module.exports = function(config) {
if (typeof config === 'string' || utils.isOptions(config)) {
return markdown.apply(defaults, arguments);
}
function markdown(str, locals, options) {
if (typeof str !== 'string') {
options = locals;
locals = str;
str = true;
}
if (utils.isOptions(locals)) {
options = locals;
locals = {};
}
var ctx = utils.context(this, locals, options);
var opts = utils.options(this, locals, options);
opts = Object.assign({}, defaults, config, opts);
if (opts.hasOwnProperty('lang')) {
opts.langPrefix = opts.lang;
}
var md = new Remarkable(opts);
var val = utils.value(str, ctx, options);
return md.render(val);
}
return markdown;
};
function highlight(code, lang) {
try {
try {
return hljs.highlight(lang, code).value;
} catch (err) {
if (!/Unknown language/i.test(err.message)) {
throw err;
}
return hljs.highlightAuto(code).value;
}
} catch (err) {
return code;
}
}