// TypeScript Version: 3.4 import {Node} from 'unist' import {VFile, VFileContents, VFileOptions, VFileCompatible} from 'vfile' import vfile = require('vfile') declare namespace unified { /** * Processor allows plugins, parsers, and compilers to be chained together to transform content. * * @typeParam P Processor settings. Useful when packaging unified with a preset parser and compiler. */ interface Processor
{ /** * Clone current processor * * @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. */ (): Processor
/**
* Configure the processor to use a plugin and optionally configure that plugin with options.
*
* @param plugin unified plugin
* @param settings Configuration for plugin
* @typeParam S Plugin settings
* @returns The processor on which use is invoked
*/
use(
plugin: Plugin,
...settings: S
): Processor
/**
* Configure the processor with a preset to use
*
* @param preset `Object` with an plugins (set to list), and/or an optional settings object
*/
use(preset: Preset): Processor
/**
* Configure using a tuple of plugin and setting(s)
*
* @param pluginTuple pairs, plugin and settings in an array
* @typeParam S Plugin settings
*/
use(
pluginTuple: PluginTuple
): Processor
/** * A list of plugins and presets to be applied to processor * * @param list List of plugins, presets, and pairs */ use(list: PluggableList
): Processor
/** * Configuration passed to a frozen processor * * @param processorSettings Settings passed to processor */ use(processorSettings: ProcessorSettings
): Processor
/**
* Parse text to a syntax tree.
*
* @param file VFile or anything which can be given to vfile()
* @returns Syntax tree representation of input.
*/
parse(file: VFileCompatible): Node
/**
* Function handling the parsing of text to a syntax tree.
* Used in the parse phase in the process and invoked with a `string` and `VFile` representation of the document to parse.
*
* `Parser` can be a normal function in which case it must return a `Node`: the syntax tree representation of the given file.
*
* `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`.
*/
Parser: ParserConstructor | ParserFunction
/**
* Compile a syntax tree to text.
*
* @param node unist node
* @param file `VFile` or anything which can be given to `vfile()`
* @returns String representation of the syntax tree file
*/
stringify(node: Node, file?: VFileCompatible): string
/**
* Function handling the compilation of syntax tree to a text.
* Used in the stringify phase in the process and invoked with a `Node` and `VFile` representation of the document to stringify.
*
* `Compiler` can be a normal function in which case it must return a `string`: the text representation of the given syntax tree.
*
* `Compiler` 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 `compile` method which is invoked without arguments and must return a `string`.
*/
Compiler: CompilerConstructor | CompilerFunction
/**
* Transform a syntax tree by applying plugins to it.
*
* @param node Node to transform
* @returns `Promise` if `done` is not given. Rejected with an error, or resolved with the resulting syntax tree.
*/
run(node: Node): Promise
/**
* Freeze a processor. Frozen processors are meant to be extended and not to be configured or processed directly.
*
* Once a processor is frozen it cannot be unfrozen. New processors functioning just like it can be created by invoking the processor.
*
* It’s possible to freeze processors explicitly, by calling `.freeze()`, but `.parse()`, `.run()`, `.stringify()`, and `.process()` call `.freeze()` to freeze a processor too.
*
* @returns The processor on which freeze is invoked.
*/
freeze(): Processor
}
/**
* A Plugin (Attacher) is the thing passed to `use`.
* It configures the processor and in turn can receive options.
*
* 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.
*
* @param settings Configuration
* @typeParam S Plugin settings
* @typeParam P Processor settings
* @returns Optional Transformer.
*/
type Plugin
settings?: Settings
}
/**
* Settings can be passed directly to the processor
*
* @typeParam P Settings applied to a processor. Useful when packaging unified with a preset parser and compiler.
*/
interface ProcessorSettings {
settings: P
}
/**
* A pairing of a plugin with its settings
*
* @typeParam S Plugin settings
* @typeParam P Processor settings
*/
type PluginTuple = Array ,
...settings: S
) => Transformer | void
/**
* 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.
* If an error occurs (either because it’s thrown, returned, rejected, or passed to `next`), the process stops.
*
* The transformation process in unified is handled by `trough`, see it’s documentation for the exact semantics of transformers.
*
* @param node Node or tree to be transformed
* @param file File associated with node or tree
* @param next If the signature of a transformer includes `next` (third argument), the function may finish asynchronous, and must invoke `next()`.
* @returns
* - `void` — If nothing is returned, the next transformer keeps using same tree.
* - `Error` — Can be returned to stop the process
* - `Node` — Can be returned and results in further transformations and `stringify`s to be performed on the new tree
* - `Promise` — If a promise is returned, the function is asynchronous, and must be resolved (optionally with a `Node`) or rejected (optionally with an `Error`)
*/
type Transformer = (
node: Node,
file: VFile,
next?: (
error: Error | null,
tree: Node,
file: VFile
) => Record (): unified.Processor
export = unified
= Attacher
/**
* Configuration passed to a Plugin or Processor
*/
interface Settings {
[key: string]: unknown
}
/**
* Presets provide a potentially sharable way to configure processors.
* They can contain multiple plugins and optionally settings as well.
*
* @typeParam P Processor settings
*/
interface Preset {
plugins: PluggableList = [
Plugin,
/**
* NOTE: ideally this would be S instead of any[]
* As of TypeScript 3.5.2 generic tuples cannot be spread
* See: https://github.com/microsoft/TypeScript/issues/26113
*/
...any[]
]
/**
* A union of the different ways to add plugins to unified
*
* @typeParam S Plugin settings
* @typeParam P Processor settings
*/
type Pluggable =
| Plugin
| Preset
| PluginTuple
/**
* A list of plugins and presets
*
* @typeParam P Processor settings
*/
type PluggableList = (
this: Processor