-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
53 lines (45 loc) · 1.39 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
var assert = require('assert')
var through = require('through2')
var posthtml = require('posthtml')
module.exports = function (opts) {
var data = ''
opts = opts || {}
if (typeof opts.parser === 'string') {
opts.parser = require(opts.parser)
}
if (typeof opts.render === 'string') {
opts.render = require(opts.render)
}
if (opts.use == null) {
opts.use = []
}
assert(opts.parser == null || typeof opts.parser === 'function', 'posthtmlify: opts.parser must be undefined or a function')
assert(opts.render == null || typeof opts.render === 'function', 'posthtmlify: opts.render must be undefined or a function')
assert(Array.isArray(opts.use), 'posthtmlify: opts.use must be an array of plugins')
var plugins = []
for (var i = 0; i < opts.use.length; i++) {
var p = opts.use[i]
var factory = p
var popts = {}
if (Array.isArray(p)) {
factory = p[0]
popts = p[1]
}
if (typeof factory === 'string') {
factory = require(factory)
}
assert.strictEqual(typeof factory, 'function', 'posthtmlify: plugins must be functions')
plugins.push(factory(popts))
}
var processor = posthtml(plugins)
return through(onwrite, onend)
function onwrite (chunk, enc, next) {
data += chunk
next()
}
function onend (next) {
processor.process(data, opts).then(function (result) {
next(null, result.html)
}, next)
}
}