Skip to content

Commit

Permalink
Add plugin input/output type parameters
Browse files Browse the repository at this point in the history
This commit adds support for tracking the parse tree, current tree,
compile tree, and compile result as configured on unified processors.

Plugins can now configure what input and output they receive and yield:

```ts
// A parse plugin, that configures parser, defines what it receives as `string`
// and what it yields as a specific node.
const remarkParse: Plugin<void[], string, MdastRoot> = () => {}

// A transform plugin, that transforms a certain tree, defines what it receives
// (and yields) as a specific node.
const remarkTransformPlugin: Plugin<void[], MdastRoot> = () => {}

// A bridge plugin, that transforms a certain tree to another, defines what it
// receives as a specific node and yields as another node.
const remarkRehype: Plugin<void[], MdastRoot, HastRoot> = () => {}

// A compile plugin, that configures a compiler, defines what it receives as a
// specific node and yields as a non-node value (typically string, but could be
// a React node).
const rehypeStringify: Plugin<void[], HastRoot, string> = () => {}
```

Assuming the above plugins are used:

```js
const processor = unified()
  .use(remarkParse)
  .use(remarkTransformPlugin)
  .use(remarkRehype)
  .use(rehypeStringify)
```

Affects what the processor functions receive and yield:

```js
import {expectType} from 'tsd'

const tree: MdastRoot = {type: 'root', children: []}

expectType<MdastRoot>(processor.parse(''))
expectType<string>(processor.stringify(tree))
expectType<HastRoot>(processor.runSync(tree))
```

Closes GH-156.

Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
  • Loading branch information
wooorm committed Jul 30, 2021
1 parent cf8653b commit 134ecad
Show file tree
Hide file tree
Showing 5 changed files with 663 additions and 78 deletions.

0 comments on commit 134ecad

Please sign in to comment.