UNPKG

14.3 kBTypeScriptView Raw
1// TypeScript Version: 3.4
2
3import {Node} from 'unist'
4import {VFile, VFileContents, VFileOptions} from 'vfile'
5import vfile = require('vfile')
6
7declare namespace unified {
8 /**
9 * Processor allows plugins, parsers, and compilers to be chained together to transform content.
10 *
11 * @typeParam P Processor settings. Useful when packaging unified with a preset parser and compiler.
12 */
13 interface Processor<P = Settings> {
14 /**
15 * Clone current processor
16 *
17 * @returns New unfrozen processor which is configured to function the same as its ancestor. But when the descendant processor is configured in the future it does not affect the ancestral processor.
18 */
19 (): Processor<P>
20
21 /**
22 * Configure the processor to use a plugin and optionally configure that plugin with options.
23 *
24 * @param plugin unified plugin
25 * @param settings Configuration for plugin
26 * @typeParam S Plugin settings
27 * @returns The processor on which use is invoked
28 */
29 use<S extends any[] = [Settings?]>(
30 plugin: Plugin<S, P>,
31 ...settings: S
32 ): Processor<P>
33
34 /**
35 * Configure the processor with a preset to use
36 *
37 * @param preset `Object` with an plugins (set to list), and/or an optional settings object
38 */
39 use<S extends any[] = [Settings?]>(preset: Preset<S, P>): Processor<P>
40
41 /**
42 * Configure using a tuple of plugin and setting(s)
43 *
44 * @param pluginTuple pairs, plugin and settings in an array
45 * @typeParam S Plugin settings
46 */
47 use<S extends any[] = [Settings?]>(
48 pluginTuple: PluginTuple<S, P>
49 ): Processor<P>
50
51 /**
52 * A list of plugins and presets to be applied to processor
53 *
54 * @param list List of plugins, presets, and pairs
55 */
56 use(list: PluggableList<P>): Processor<P>
57
58 /**
59 * Configuration passed to a frozen processor
60 *
61 * @param processorSettings Settings passed to processor
62 */
63 use(processorSettings: ProcessorSettings<P>): Processor<P>
64
65 /**
66 * Parse text to a syntax tree.
67 *
68 * @param file VFile or anything which can be given to vfile()
69 * @returns Syntax tree representation of input.
70 */
71 parse(file: VFileCompatible): Node
72
73 /**
74 * Function handling the parsing of text to a syntax tree.
75 * Used in the parse phase in the process and invoked with a `string` and `VFile` representation of the document to parse.
76 *
77 * `Parser` can be a normal function in which case it must return a `Node`: the syntax tree representation of the given file.
78 *
79 * `Parser` can also be a constructor function (a function with keys in its `prototype`) in which case it’s invoked with `new`. Instances must have a parse method which is invoked without arguments and must return a `Node`.
80 */
81 Parser: ParserConstructor | ParserFunction
82
83 /**
84 * Compile a syntax tree to text.
85 *
86 * @param node
87 * @param file `VFile` or anything which can be given to `vfile()`
88 * @returns String representation of the syntax tree file
89 */
90 stringify(node: Node, file?: VFileCompatible): string
91
92 /**
93 * Function handling the compilation of syntax tree to a text.
94 * Used in the stringify phase in the process and invoked with a `Node` and `VFile` representation of the document to stringify.
95 *
96 * `Compiler` can be a normal function in which case it must return a `string`: the text representation of the given syntax tree.
97 *
98 * `Compiler` can also be a constructor function (a function with keys in its `prototype`) in which case it’s invoked with `new`.
99 * Instances must have a `compile` method which is invoked without arguments and must return a `string`.
100 */
101 Compiler: CompilerConstructor | CompilerFunction
102
103 /**
104 * Transform a syntax tree by applying plugins to it.
105 *
106 * @param node Node to transform
107 * @returns `Promise` if `done` is not given. Rejected with an error, or resolved with the resulting syntax tree.
108 */
109 run(node: Node): Promise<Node>
110
111 /**
112 * Transform a syntax tree by applying plugins to it.
113 *
114 * @param node Node to transform
115 * @param file `VFile` or anything which can be given to `vfile()`
116 * @returns `Promise` if `done` is not given. Rejected with an error, or resolved with the resulting syntax tree.
117 */
118 run(node: Node, file: VFileCompatible): Promise<Node>
119
120 /**
121 * Transform a syntax tree by applying plugins to it.
122 *
123 * @param node Node to transform
124 * @param done Invoked when transformation is complete.
125 */
126 run(node: Node, done: RunCallback): void
127
128 /**
129 * Transform a syntax tree by applying plugins to it.
130 *
131 * @param node Node to transform
132 * @param file `VFile` or anything which can be given to `vfile()`
133 * @param done Invoked when transformation is complete.
134 */
135 run(node: Node, file: VFileCompatible, done: RunCallback): void
136
137 /**
138 * Transform a syntax tree by applying plugins to it.
139 *
140 * If asynchronous plugins are configured an error is thrown.
141 *
142 * @param node Node to transform
143 * @param file `VFile` or anything which can be given to `vfile()`
144 * @returns The given syntax tree.
145 */
146 runSync(node: Node, file?: VFileCompatible): Node
147
148 /**
149 * Process the given representation of a file as configured on the processor. The process invokes `parse`, `run`, and `stringify` internally.
150 * @param file `VFile` or anything which can be given to `vfile()`
151 * @returns `Promise` if `done` is not given.
152 * Rejected with an error or resolved with the resulting file.
153 */
154 process(file: VFileCompatible): Promise<VFile>
155
156 /**
157 * Process the given representation of a file as configured on the processor. The process invokes `parse`, `run`, and `stringify` internally.
158 * @param file `VFile` or anything which can be given to `vfile()`
159 * @param done Invoked when the process is complete. Invoked with a fatal error, if any, and the VFile.
160 */
161 process(file: VFileCompatible, done: ProcessCallback): void
162
163 /**
164 * Process the given representation of a file as configured on the processor. The process invokes `parse`, `run`, and `stringify` internally.
165 *
166 * If asynchronous plugins are configured an error is thrown.
167 *
168 * @param file `VFile` or anything which can be given to `vfile()`
169 * @returns Virtual file with modified contents.
170 */
171 processSync(file: VFileCompatible): VFile
172
173 /**
174 * Get or set information in an in-memory key-value store accessible to all phases of the process.
175 * An example is a list of HTML elements which are self-closing, which is needed when parsing, transforming, and compiling HTML.
176 *
177 * @returns key-value store object
178 */
179 data(): {[key: string]: unknown}
180
181 /**
182 * @param key Identifier
183 * @returns If getting, the value at key
184 */
185 data(key: string): unknown
186
187 /**
188 * @param value Value to set. Omit if getting key
189 * @returns If setting, the processor on which data is invoked
190 */
191 data(key: string, value: any): Processor<P>
192
193 /**
194 * Freeze a processor. Frozen processors are meant to be extended and not to be configured or processed directly.
195 *
196 * Once a processor is frozen it cannot be unfrozen. New processors functioning just like it can be created by invoking the processor.
197 *
198 * It’s possible to freeze processors explicitly, by calling `.freeze()`, but `.parse()`, `.run()`, `.stringify()`, and `.process()` call `.freeze()` to freeze a processor too.
199 *
200 * @returns The processor on which freeze is invoked.
201 */
202 freeze(): Processor<P>
203 }
204
205 /**
206 * A Plugin (Attacher) is the thing passed to `use`.
207 * It configures the processor and in turn can receive options.
208 *
209 * Attachers can configure processors, such as by interacting with parsers and compilers, linking them to other processors, or by specifying how the syntax tree is handled.
210 *
211 * @this Processor context object is set to the invoked on processor.
212 * @param settings Configuration
213 * @typeParam S Plugin settings
214 * @typeParam P Processor settings
215 * @returns Optional Transformer.
216 */
217 type Plugin<S extends any[] = [Settings?], P = Settings> = Attacher<S, P>
218
219 /**
220 * Configuration passed to a Plugin or Processor
221 */
222 type Settings = {
223 [key: string]: unknown
224 }
225
226 /**
227 * Presets provide a potentially sharable way to configure processors.
228 * They can contain multiple plugins and optionally settings as well.
229 *
230 * @typeParam P Processor settings
231 */
232 interface Preset<S = Settings, P = Settings> {
233 plugins: PluggableList<P>
234 settings?: Settings
235 }
236
237 /**
238 * Settings can be passed directly to the processor
239 *
240 * @typeParam P Settings applied to a processor. Useful when packaging unified with a preset parser and compiler.
241 */
242 interface ProcessorSettings<P = Settings> {
243 settings: P
244 }
245
246 /**
247 * A pairing of a plugin with its settings
248 *
249 * @typeParam S Plugin settings
250 * @typeParam P Processor settings
251 */
252 type PluginTuple<S extends any[] = [Settings?], P = Settings> = [
253 Plugin<S, P>,
254 /**
255 * NOTE: ideally this would be S instead of any[]
256 * As of TypeScript 3.5.2 generic tuples cannot be spread
257 * See: https://github.com/microsoft/TypeScript/issues/26113
258 */
259 ...any[]
260 ]
261
262 /**
263 * A union of the different ways to add plugins to unified
264 *
265 * @typeParam S Plugin settings
266 * @typeParam P Processor settings
267 */
268 type Pluggable<S extends any[] = [Settings?], P = Settings> =
269 | Plugin<S, P>
270 | Preset<S, P>
271 | PluginTuple<S, P>
272
273 /**
274 * A list of plugins and presets
275 *
276 * @typeParam P Processor settings
277 */
278 type PluggableList<P = Settings> = Array<Pluggable<[any?], P>>
279
280 /**
281 * An attacher is the thing passed to `use`.
282 * It configures the processor and in turn can receive options.
283 *
284 * Attachers can configure processors, such as by interacting with parsers and compilers, linking them to other processors, or by specifying how the syntax tree is handled.
285 *
286 * @this Processor context object is set to the invoked on processor.
287 * @param settings Configuration
288 * @typeParam S Plugin settings
289 * @typeParam P Processor settings
290 * @returns Optional Transformer.
291 */
292 interface Attacher<S extends any[] = [Settings?], P = Settings> {
293 (this: Processor<P>, ...settings: S): Transformer | void
294 }
295
296 /**
297 * Transformers modify the syntax tree or metadata of a file. A transformer is a function which is invoked each time a file is passed through the transform phase.
298 * If an error occurs (either because it’s thrown, returned, rejected, or passed to `next`), the process stops.
299 *
300 * The transformation process in unified is handled by `trough`, see it’s documentation for the exact semantics of transformers.
301 *
302 * @param node Node or tree to be transformed
303 * @param file File associated with node or tree
304 * @param next If the signature of a transformer includes `next` (third argument), the function may finish asynchronous, and must invoke `next()`.
305 * @returns
306 * - `Error` — Can be returned to stop the process
307 * - `Node` — Can be returned and results in further transformations and `stringify`s to be performed on the new tree
308 * - `Promise` — If a promise is returned, the function is asynchronous, and must be resolved (optionally with a `Node`) or rejected (optionally with an `Error`)
309 */
310 interface Transformer {
311 (
312 node: Node,
313 file: VFile,
314 next?: (error: Error | null, tree: Node, file: VFile) => {}
315 ): Error | Node | Promise<Node>
316 }
317
318 /**
319 * Transform file contents into an AST
320 */
321 interface Parser {
322 /**
323 * Transform file contents into an AST
324 *
325 * @returns Parsed AST node/tree
326 */
327 parse(): Node
328 }
329
330 /**
331 * A constructor function (a function with keys in its `prototype`) or class that implements a
332 * `parse` method.
333 */
334 interface ParserConstructor {
335 /**
336 * Creates a Parser
337 *
338 * @param text Text to transform into AST node(s)
339 * @param file File associated with text
340 */
341 new (text: string, file: VFile): Parser
342 }
343
344 /**
345 * Transform file contents into an AST
346 *
347 * @param text Text to transform into AST node(s)
348 * @param file File associated with text
349 * @returns Parsed AST node/tree
350 */
351 type ParserFunction = (text: string, file: VFile) => Node
352
353 /**
354 * Transform an AST node/tree into text
355 */
356 interface Compiler {
357 /**
358 * Transform an AST node/tree into text
359 *
360 * @returns Compiled text
361 */
362 compile(): string
363 }
364
365 /**
366 * A constructor function (a function with keys in its `prototype`) or class that implements a
367 * `compile` method.
368 */
369 interface CompilerConstructor {
370 /**
371 * Creates a Compiler
372 *
373 * @param node Node/tree to be stringified
374 * @param file File associated with node
375 */
376 new (node: Node, file: VFile): Compiler
377 }
378
379 /**
380 * Transform an AST node/tree into text
381 *
382 * @param node Node/tree to be stringified
383 * @param file File associated with node
384 * @returns Compiled text
385 */
386 type CompilerFunction = (node: Node, file: VFile) => string
387
388 /**
389 * Access results from transforms
390 *
391 * @param error Error if any occurred
392 * @param node Transformed AST tree/node
393 * @param vfile File associated with node
394 */
395 type RunCallback = (error: Error | null, node: Node, file: VFile) => void
396
397 /**
398 * Access results from transforms
399 *
400 * @param error Error if any occurred
401 * @param vfile File with updated content
402 */
403 type ProcessCallback = (error: Error | null, file: VFile) => void
404
405 /**
406 * A union of the VFile types unified supports
407 */
408 type VFileCompatible = VFile | VFileOptions | VFileContents
409}
410
411/**
412 * Unified processor allows plugins, parsers, and compilers to be chained together to transform content.
413 *
414 * @typeParam P Processor settings. Useful when packaging unified with a preset parser and compiler.
415 */
416declare function unified<P = unified.Settings>(): unified.Processor<P>
417export = unified
418
\No newline at end of file