Comparing version 1.1.5 to 1.1.6
48
index.js
@@ -24,12 +24,12 @@ var through = require('through3') | ||
/** | ||
* Parse line-delimited JSON to commonmark AST. | ||
* Parse line-delimited JSON to vanilla objects. | ||
* | ||
* When a callback function is given it is added as a listener for | ||
* the error and eof events on the deserializer stream. | ||
* the `error` and `finish` events on the parser stream. | ||
* | ||
* @function deserialize | ||
* @function parser | ||
* @param {Object} stream input stream. | ||
* @param {Function} [cb] callback function. | ||
* | ||
* @returns the deserializer stream. | ||
* @returns the parser stream. | ||
*/ | ||
@@ -46,5 +46,3 @@ function parser(stream, cb) { | ||
.once('error', cb) | ||
.once('finish', function(doc) { | ||
cb(null, doc); | ||
}); | ||
.once('finish', cb); | ||
} | ||
@@ -55,7 +53,10 @@ return parser; | ||
/** | ||
* Deserialize line-delimited JSON to commonmark AST documents. | ||
* Deserialize line-delimited JSON to commonmark AST nodes. | ||
* | ||
* When a callback function is given it is added as a listener for | ||
* the error and eof events on the deserializer stream. | ||
* the `error` and `eof` events on the deserializer stream. | ||
* | ||
* The `eof` event can fire multiple times so the callback may be called | ||
* multiple times. | ||
* | ||
* @function deserialize | ||
@@ -86,15 +87,30 @@ * @param {Object} stream input stream. | ||
/** | ||
* Serialize a commonmark AST to line-delimited JSON. | ||
* Serialize a commonmark AST node to line-delimited JSON. | ||
* | ||
* When the node is of the `document` type it's direct descendants are | ||
* detached from the document and streamed as independent lines. So that | ||
* consumers of the stream will know when the document ends a node | ||
* with an `eof` type is sent to indicate the end of file (EOF). | ||
* | ||
* When injecting documents into a stream it may be desirable to disable | ||
* this behaviour, to do so use: | ||
* | ||
* ```javascript | ||
* {eof: false} | ||
* ``` | ||
* | ||
* When a callback function is given it is added as a listener for | ||
* the error and finish events on the serializer stream. | ||
* the `error` and `finish` events on the serializer stream. | ||
* | ||
* @function serialize | ||
* @param {Object} buffer input AST. | ||
* @param {Object} node input AST node. | ||
* @param {Object} [opts] processing options. | ||
* @param {Function} [cb] callback function. | ||
* | ||
* @options {Boolean=true} eof disable sending an EOF node. | ||
* @options {Number=0} indent number of spaces to indent JSON. | ||
* | ||
* @returns the serializer stream. | ||
*/ | ||
function serialize(buffer, opts, cb) { | ||
function serialize(node, opts, cb) { | ||
if(typeof opts === 'function') { | ||
@@ -113,3 +129,3 @@ cb = opts; | ||
if(cb) { | ||
ast | ||
serializer | ||
.once('error', cb) | ||
@@ -121,3 +137,3 @@ .once('finish', cb); | ||
process.nextTick(function() { | ||
ast.end(buffer); | ||
ast.end(node); | ||
}) | ||
@@ -132,4 +148,4 @@ | ||
parser: parser, | ||
Node: require('commonmark/lib/node'), | ||
Node: require('./lib/node'), | ||
Parser: require('commonmark').Parser | ||
} |
var through = require('through3') | ||
, Node = require('commonmark/lib/node') | ||
, Node = require('./node') | ||
, attach = require('./attach'); | ||
@@ -26,6 +26,14 @@ | ||
function transform(ast, encoding, cb) { | ||
// create root document | ||
if(ast && ast._type === 'document') { | ||
// maintain link references | ||
if(ast.refs && this.doc) { | ||
this.doc.refs = this.doc.refs || []; | ||
this.doc.refs = this.doc.refs.concat(ast.refs); | ||
} | ||
if(!this.doc) { | ||
this.doc = new Node('document', ast._sourcepos); | ||
this.doc = Node.createDocument(); | ||
// append nested document | ||
@@ -41,4 +49,4 @@ }else{ | ||
this.doc.appendChild(attach(ast)); | ||
// got EOF | ||
}else{ | ||
// got EOF with a valid doc, flush it | ||
}else if(this.doc && ast._type === 'eof') { | ||
this.push(this.doc); | ||
@@ -45,0 +53,0 @@ this.emit('eof', this.doc); |
{ | ||
"name": "mkast", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"description": "Commonmark AST transformer", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,4 +8,4 @@ Table of Contents | ||
* [API](#api) | ||
* [parser](#parser) | ||
* [deserialize](#deserialize) | ||
* [deserialize](#deserialize-1) | ||
* [serialize](#serialize) | ||
@@ -45,14 +45,14 @@ * [License](#license) | ||
### deserialize | ||
### parser | ||
```javascript | ||
deserialize(stream[, cb]) | ||
parser(stream[, cb]) | ||
``` | ||
Parse line-delimited JSON to commonmark AST. | ||
Parse line-delimited JSON to vanilla objects. | ||
When a callback function is given it is added as a listener for | ||
the error and eof events on the deserializer stream. | ||
the `error` and `finish` events on the parser stream. | ||
Returns the deserializer stream. | ||
Returns the parser stream. | ||
@@ -68,7 +68,10 @@ * `stream` Object input stream. | ||
Deserialize line-delimited JSON to commonmark AST documents. | ||
Deserialize line-delimited JSON to commonmark AST nodes. | ||
When a callback function is given it is added as a listener for | ||
the error and eof events on the deserializer stream. | ||
the `error` and `eof` events on the deserializer stream. | ||
The `eof` event can fire multiple times so the callback may be called | ||
multiple times. | ||
Returns the deserializer stream. | ||
@@ -82,13 +85,26 @@ | ||
```javascript | ||
serialize(buffer[, cb]) | ||
serialize(node[, opts][, cb]) | ||
``` | ||
Serialize a commonmark AST to line-delimited JSON. | ||
Serialize a commonmark AST node to line-delimited JSON. | ||
When the node is of the `document` type it's direct descendants are | ||
detached from the document and streamed as independent lines. So that | ||
consumers of the stream will know when the document ends a node | ||
with an `eof` type is sent to indicate the end of file (EOF). | ||
When injecting documents into a stream it may be desirable to disable | ||
this behaviour, to do so use: | ||
```javascript | ||
{eof: false} | ||
``` | ||
When a callback function is given it is added as a listener for | ||
the error and finish events on the serializer stream. | ||
the `error` and `finish` events on the serializer stream. | ||
Returns the serializer stream. | ||
* `buffer` Object input AST. | ||
* `node` Object input AST node. | ||
* `opts` Object processing options. | ||
* `cb` Function callback function. | ||
@@ -95,0 +111,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21818
25
535
117