-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (37 loc) · 1.07 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
'use strict'
const remark = require('remark')
const html = require('remark-html')
const extend = require('extend-shallow')
exports.name = 'remark'
exports.inputFormats = ['markdown', 'md', 'remark']
exports.outputFormat = 'html'
function prepareRemark(options, locals) {
const processor = remark()
const opts = extend({}, options, locals)
const plugins = (opts.plugins && Array.isArray(opts.plugins)) ? opts.plugins : []
plugins.push(html)
for (const plugin of plugins) {
if (plugin) {
if (typeof plugin === 'string' || plugin instanceof String) {
processor.use(require(plugin))
} else {
processor.use(plugin)
}
}
}
return processor
}
exports.render = function (str, options, locals) {
return prepareRemark(options, locals).processSync(str).toString()
}
exports.renderAsync = function (str, options, locals) {
return new Promise((resolve, reject) => {
prepareRemark(options, locals)
.process(str, (err, file) => {
if (err) {
return reject(err)
}
resolve(file.toString())
})
})
}