Skip to content

Commit

Permalink
Add processSync, runSync
Browse files Browse the repository at this point in the history
* Remove sync behaviour of `process`, `run`;
* Add `processSync`, `runSync` instead;
* Return promises if no callback is given to `process`, `run`.

Closes GH-8.
  • Loading branch information
wooorm committed Feb 12, 2017
1 parent f0427f4 commit 56882b0
Show file tree
Hide file tree
Showing 7 changed files with 919 additions and 337 deletions.
119 changes: 85 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ function unified() {
processor.parse = parse;
processor.stringify = stringify;
processor.run = run;
processor.runSync = runSync;
processor.process = process;
processor.processSync = processSync;

/* Expose. */
return processor;
Expand Down Expand Up @@ -220,31 +222,57 @@ function unified() {
}

/* Run transforms on a Unist node representation of a file
* (in string or VFile representation). */
function run(node, file, done) {
var complete = false;
var result;

* (in string or VFile representation), async. */
function run(node, file, cb) {
assertConcrete('run', concrete);
assertNode(node);

result = node;

if (!done && func(file)) {
done = file;
if (!cb && func(file)) {
cb = file;
file = null;
}

transformers.run(node, vfile(file), function (err, tree, file) {
complete = true;
result = tree || node;
if (!cb) {
return new Promise(executor);
}

(done || bail)(err, tree, file);
});
executor(null, cb);

function executor(resolve, reject) {
transformers.run(node, vfile(file), done);

function done(err, tree, file) {
tree = tree || node;
if (err) {
reject(err);
} else if (resolve) {
resolve(tree);
} else {
cb(null, tree, file);
}
}
}
}

/* Run transforms on a Unist node representation of a file
* (in string or VFile representation), sync. */
function runSync(node, file) {
var complete = false;
var result;

assertDone('run', complete, done);
assertConcrete('runSync', concrete);

run(node, file, done);

assertDone('runSync', 'run', complete);

return result;

function done(err, tree) {
complete = true;
bail(err);
result = tree;
}
}

/* Stringify a Unist node representation of a file
Expand All @@ -270,29 +298,55 @@ function unified() {
* then run transforms on that node, and compile the
* resulting node using the `Compiler` on the processor,
* and store that result on the VFile. */
function process(doc, done) {
var complete = false;
var file;

function process(doc, cb) {
assertConcrete('process', concrete);
assertParser('process', processor.Parser);
assertCompiler('process', processor.Compiler);

file = vfile(doc);
if (!cb) {
return new Promise(executor);
}

pipeline.run(processor, {file: file}, function (err) {
complete = true;
executor(null, cb);

if (done) {
done(err, file);
} else {
bail(err);
function executor(resolve, reject) {
var file = vfile(doc);

pipeline.run(processor, {file: file}, done);

function done(err) {
if (err) {
reject(err);
} else if (resolve) {
resolve(file);
} else {
cb(null, file);
}
}
});
}
}

/* Process the given document (in string or VFile
* representation), sync. */
function processSync(doc) {
var complete = false;
var file;

assertConcrete('processSync', concrete);
assertParser('processSync', processor.Parser);
assertCompiler('processSync', processor.Compiler);
file = vfile(doc);

assertDone('process', complete, done);
process(file, done);

assertDone('processSync', 'process', complete);

return file;

function done(err) {
complete = true;
bail(err);
}
}
}

Expand Down Expand Up @@ -349,11 +403,8 @@ function assertNode(node) {

/* Assert, if no `done` is given, that `complete` is
* `true`. */
function assertDone(name, complete, done) {
if (!complete && !done) {
throw new Error(
'Expected `done` to be given to `' + name + '` ' +
'as async plug-ins are used'
);
function assertDone(name, asyncName, complete) {
if (!complete) {
throw new Error('`' + name + '` finished async. Use `' + asyncName + '` instead');
}
}
131 changes: 120 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ no issues found
* [processor.parse(file|value)](#processorparsefilevalue)
* [processor.stringify(node\[, file\])](#processorstringifynode-file)
* [processor.run(node\[, file\]\[, done\])](#processorrunnode-file-done)
* [processor.runSync(node\[, file\])](#processorrunsyncnode-file)
* [processor.process(file|value\[, done\])](#processorprocessfilevalue-done)
* [processor.processSync(file|value)](#processorprocesssyncfilevalue)
* [processor.data(key\[, value\])](#processordatakey-value)
* [processor.abstract()](#processorabstract)
* [License](#license)
Expand Down Expand Up @@ -229,7 +231,7 @@ var remark = require('remark');
var concat = require('concat-stream');

process.stdin.pipe(concat(function (buf) {
process.stdout.write(remark().process(buf))
process.stdout.write(remark().processSync(buf))
}));
```

Expand Down Expand Up @@ -393,9 +395,6 @@ representation of the given syntax tree.

Transform a syntax tree by applying [**plug-in**][plugin]s to it.

If asynchronous [**plug-in**][plugin]s are configured, an error
is thrown if [`done`][run-done] is not supplied.

###### Parameters

* `node` ([**Node**][node]);
Expand All @@ -405,7 +404,8 @@ is thrown if [`done`][run-done] is not supplied.

###### Returns

[**Node**][node] — The given syntax tree.
[**Promise**][promise], if `done` is not given. Rejected with an error,
or resolved with the resulting syntax tree.

##### `function done(err[, node, file])`

Expand All @@ -418,15 +418,28 @@ error, or a syntax tree and a file.
* `node` ([**Node**][node]);
* `file` ([**VFile**][file]).

### `processor.runSync(node[, file])`

Transform a syntax tree by applying [**plug-in**][plugin]s to it.

If asynchronous [**plug-in**][plugin]s are configured, an error is thrown.

###### Parameters

* `node` ([**Node**][node]);
* `file` ([**VFile**][file], optional);
— Or anything which can be given to `vfile()`.

###### Returns

[**Node**][node] — The given syntax tree.

### `processor.process(file|value[, done])`

Process the given representation of a file as configured on the
processor. The process invokes `parse`, `run`, and `stringify`
internally.

If asynchronous [**plug-in**][plugin]s are configured, an error
is thrown if [`done`][process-done] is not supplied.

###### Parameters

* `file` ([**VFile**][file]);
Expand All @@ -435,7 +448,8 @@ is thrown if [`done`][process-done] is not supplied.

###### Returns

[**VFile**][file] — Virtual file with modified [`contents`][vfile-contents].
[**Promise**][promise], if `done` is not given. Rejected with an error,
or resolved with the resulting file.

#### `function done(err, file)`

Expand All @@ -447,6 +461,99 @@ any, and the [**VFile**][file].
* `err` (`Error`, optional) — Fatal error;
* `file` ([**VFile**][file]).

###### Example

```js
var unified = require('unified');
var markdown = require('remark-parse');
var remark2rehype = require('remark-rehype');
var document = require('rehype-document');
var format = require('rehype-format');
var html = require('rehype-stringify');
var report = require('vfile-reporter');

unified()
.use(markdown)
.use(remark2rehype)
.use(document)
.use(format)
.use(html)
.process('# Hello world!')
.then(function (file) {
console.log(String(file));
}, function (err) {
console.error(String(err));
})
```

Yields:

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
```

### `processor.processSync(file|value)`

Process the given representation of a file as configured on the
processor. The process invokes `parse`, `run`, and `stringify`
internally.

If asynchronous [**plug-in**][plugin]s are configured, an error is thrown.

###### Parameters

* `file` ([**VFile**][file]);
* `value` (`string`) — String representation of a file;

###### Returns

[**VFile**][file] — Virtual file with modified [`contents`][vfile-contents].

###### Example

```js
var unified = require('unified');
var markdown = require('remark-parse');
var remark2rehype = require('remark-rehype');
var document = require('rehype-document');
var format = require('rehype-format');
var html = require('rehype-stringify');
var report = require('vfile-reporter');

var processor = unified()
.use(markdown)
.use(remark2rehype)
.use(document)
.use(format)
.use(html);

console.log(processor.processSync('# Hello world!').toString());
```

Yields:

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
```

### `processor.data(key[, value])`

Get or set information in an in-memory key-value store accessible to
Expand Down Expand Up @@ -516,7 +623,7 @@ var rehype = require('rehype');
var concat = require('concat-stream');

process.stdin.pipe(concat(function (buf) {
process.stdout.write(rehype().process(buf))
process.stdout.write(rehype().processSync(buf))
}));
```

Expand All @@ -530,7 +637,7 @@ var rehype = require('rehype');
var concat = require('concat-stream');

process.stdin.pipe(concat(function (buf) {
process.stdout.write(rehype.process(buf));
process.stdout.write(rehype.processSync(buf));
}));
```

Expand Down Expand Up @@ -640,3 +747,5 @@ To make the processor concrete, invoke it: use `processor()` instead of `process
[process-done]: #function-doneerr-file

[trough]: https://github.com/wooorm/trough#function-fninput-next

[promise]: https://developer.mozilla.org/Web/JavaScript/Reference/Global_Objects/Promise
Loading

0 comments on commit 56882b0

Please sign in to comment.