MCPcopy
hub / github.com/posthtml/posthtml / process

Method process

lib/index.js:136–243  ·  view source on GitHub ↗

* @param {String} html - Input (HTML) * @param {?Object} options - PostHTML Options * @returns {Object<{html: String, tree: PostHTMLTree}>} - Sync Mode * @returns {Promise<{html: String, tree: PostHTMLTree}>} - Async Mode (default) * * **Usage** * * **Sync** * ```js *

(tree, options = {})

Source from the content-addressed store, hash-verified

134 * ```
135 */
136 process (tree, options = {}) {
137 /**
138 * ## PostHTML Options
139 *
140 * @type {Object}
141 * @prop {?Boolean} options.sync - enables sync mode, plugins will run synchronously, throws an error when used with async plugins
142 * @prop {?Function} options.parser - use custom parser, replaces default (posthtml-parser)
143 * @prop {?Function} options.render - use custom render, replaces default (posthtml-render)
144 * @prop {?Boolean} options.skipParse - disable parsing
145 * @prop {?Array} options.directives - Adds processing of custom [directives](https://github.com/posthtml/posthtml-parser#directives).
146 */
147 this.options = options
148 this.source = tree
149
150 if (options.parser) parser = this.parser = options.parser
151 if (options.render) render = this.render = options.render
152
153 tree = options.skipParse
154 ? tree || []
155 : parser(tree, options)
156
157 tree = [].concat(tree)
158
159 // sync mode
160 if (options.sync === true) {
161 this.plugins.forEach((plugin, index) => {
162 _treeExtendApi(tree, this)
163
164 let result
165
166 if (plugin.length === 2 || isPromise(result = plugin(tree))) {
167 throw new Error(
168 `Can’t process contents in sync mode because of async plugin: ${plugin.name}`
169 )
170 }
171
172 // clearing the tree of options
173 if (index !== this.plugins.length - 1 && !options.skipParse) {
174 tree = [].concat(tree)
175 }
176
177 // return the previous tree unless result is fulfilled
178 tree = result || tree
179 })
180
181 return lazyResult(render, tree)
182 }
183
184 // async mode
185 let i = 0
186
187 const next = (result, cb) => {
188 _treeExtendApi(result, this)
189
190 // all plugins called
191 if (this.plugins.length <= i) {
192 cb(null, result)
193 return

Callers

nothing calls this directly

Calls 4

_treeExtendApiFunction · 0.85
isPromiseFunction · 0.85
lazyResultFunction · 0.85
pluginFunction · 0.50

Tested by

no test coverage detected