123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- var pkg = require('../package.json')
- var api = require('./api.js')
- var parser = require('posthtml-parser')
- var render = require('posthtml-render')
- function PostHTML (plugins) {
- this.version = pkg.version
- this.name = pkg.name
- this.plugins = typeof plugins === 'function' ? [plugins] : plugins || []
- }
- PostHTML.parser = parser
- PostHTML.render = render
- PostHTML.prototype.use = function () {
- [].push.apply(this.plugins, arguments)
- return this
- }
- PostHTML.prototype.process = function (tree, options) {
-
- options = options || {}
- if (options.parser) parser = options.parser
- if (options.render) render = options.render
- tree = options.skipParse ? tree : parser(tree)
- tree.options = options
- tree.processor = this
-
- if (options.sync === true) {
- this.plugins.forEach(function (plugin) {
- apiExtend(tree)
- var result
- if (plugin.length === 2 || isPromise(result = plugin(tree))) {
- throw new Error(
- 'Can’t process contents in sync mode because of async plugin: ' + plugin.name
- )
- }
-
- tree = result || tree
- })
- return lazyResult(render, tree)
- }
-
- var i = 0
- var next = function (result, cb) {
-
- if (this.plugins.length <= i) {
- cb(null, result)
- return
- }
-
- function _next (res) {
- return next(res || result, cb)
- }
-
- apiExtend(result)
-
- var plugin = this.plugins[i++]
- if (plugin.length === 2) {
- plugin(result, function (err, res) {
- if (err) return cb(err)
- _next(res)
- })
- return
- }
-
- var err = null
- var res = tryCatch(function () {
- return plugin(result)
- }, function (err) {
- return err
- })
- if (err) {
- cb(err)
- return
- }
- if (isPromise(res)) {
- res.then(_next).catch(cb)
- return
- }
- _next(res)
- }.bind(this)
- return new Promise(function (resolve, reject) {
- next(tree, function (err, tree) {
- if (err) reject(err)
- else resolve(lazyResult(render, tree))
- })
- })
- }
- module.exports = function (plugins) {
- return new PostHTML(plugins)
- }
- function isPromise (promise) {
- return !!promise && typeof promise.then === 'function'
- }
- function tryCatch (tryFn, catchFn) {
- try {
- return tryFn()
- } catch (err) {
- catchFn(err)
- }
- }
- function apiExtend (tree) {
- tree.walk = api.walk
- tree.match = api.match
- }
- function lazyResult (render, tree) {
- return {
- get html () {
- return render(tree, tree.options)
- },
- tree: tree
- }
- }
|