Comparing version 1.1.2 to 1.1.3
40
index.js
@@ -5,3 +5,4 @@ var through = require('through3') | ||
, Serialize = require('./lib/serialize') | ||
, Deserialize = require('./lib/deserialize'); | ||
, Deserialize = require('./lib/deserialize') | ||
, Parser = require('./lib/parser'); | ||
@@ -21,7 +22,6 @@ /** | ||
var EachStream = through.transform(each); | ||
/** | ||
* Deserialize line-delimited JSON to commonmark AST. | ||
* Parse line-delimited JSON to commonmark AST. | ||
* | ||
@@ -37,2 +37,31 @@ * When a callback function is given it is added as a listener for | ||
*/ | ||
function parser(stream, cb) { | ||
var parser = new Parser(); | ||
stream | ||
.pipe(new LineStream()) | ||
.pipe(new EachStream()) | ||
.pipe(parser); | ||
if(cb) { | ||
parser | ||
.once('error', cb) | ||
.once('finish', function(doc) { | ||
cb(null, doc); | ||
}); | ||
} | ||
return parser; | ||
} | ||
/** | ||
* Deserialize line-delimited JSON to commonmark AST documents. | ||
* | ||
* When a callback function is given it is added as a listener for | ||
* the error and eof events on the deserializer stream. | ||
* | ||
* @function deserialize | ||
* @param {Object} stream input stream. | ||
* @param {Function} [cb] callback function. | ||
* | ||
* @returns the deserializer stream. | ||
*/ | ||
function deserialize(stream, cb) { | ||
@@ -43,2 +72,3 @@ var deserializer = new Deserialize(); | ||
.pipe(new EachStream()) | ||
.pipe(new Parser()) | ||
.pipe(deserializer); | ||
@@ -53,3 +83,2 @@ | ||
} | ||
return deserializer; | ||
@@ -92,3 +121,4 @@ } | ||
serialize: serialize, | ||
deserialize: deserialize | ||
deserialize: deserialize, | ||
parser: parser | ||
} |
@@ -44,19 +44,6 @@ var through = require('through3') | ||
// empty lines | ||
if(!ast) { | ||
return cb(); | ||
} | ||
var res; | ||
try { | ||
res = JSON.parse(ast); | ||
}catch(e) { | ||
return cb(e); | ||
} | ||
if(res && res._type === 'document') { | ||
this.doc = new Node('document', res._sourcepos); | ||
}else if(this.doc && res._type !== 'EOF') { | ||
this.doc.appendChild(attach(res)); | ||
if(ast && ast._type === 'document') { | ||
this.doc = new Node('document', ast._sourcepos); | ||
}else if(this.doc && ast._type !== 'EOF') { | ||
this.doc.appendChild(attach(ast)); | ||
// got EOF | ||
@@ -63,0 +50,0 @@ }else{ |
{ | ||
"name": "mkast", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Commonmark AST transformer", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,2 +9,3 @@ Table of Contents | ||
* [deserialize](#deserialize) | ||
* [deserialize](#deserialize-1) | ||
* [serialize](#serialize) | ||
@@ -50,3 +51,3 @@ * [License](#license) | ||
Deserialize line-delimited JSON to commonmark AST. | ||
Parse line-delimited JSON to commonmark AST. | ||
@@ -61,2 +62,18 @@ When a callback function is given it is added as a listener for | ||
### deserialize | ||
```javascript | ||
deserialize(stream[, cb]) | ||
``` | ||
Deserialize line-delimited JSON to commonmark AST documents. | ||
When a callback function is given it is added as a listener for | ||
the error and eof events on the deserializer stream. | ||
Returns the deserializer stream. | ||
* `stream` Object input stream. | ||
* `cb` Function callback function. | ||
### serialize | ||
@@ -63,0 +80,0 @@ |
@@ -7,3 +7,4 @@ var expect = require('chai').expect | ||
, Walk = require('../../lib/walk') | ||
, Deserialize = require('../../lib/deserialize'); | ||
//, Deserialize = require('../../lib/deserialize') | ||
, ParserStream = require('../../lib/parser'); | ||
@@ -18,3 +19,19 @@ describe('mkast:', function() { | ||
it('should serialize ast and parse result lines w/ callback', | ||
function(done) { | ||
var parser = new Parser() | ||
, buffer = parser.parse('# Title\n<? @include file.md ?>'); | ||
ast.parser(ast.serialize(buffer), done); | ||
} | ||
); | ||
it('should serialize ast and parse result lines w/ listener', | ||
function(done) { | ||
var parser = new Parser() | ||
, buffer = parser.parse('# Title\n<? @include file.md ?>'); | ||
var stream = ast.parser(ast.serialize(buffer)); | ||
stream.once('finish', done); | ||
} | ||
); | ||
it('should serialize and deserialize ast w/ callback', function(done) { | ||
@@ -57,4 +74,4 @@ var parser = new Parser() | ||
it('should error on bad json', function(done) { | ||
var deserializer = new Deserialize(); | ||
deserializer.once('error', function(err) { | ||
var parser = new ParserStream(); | ||
parser.once('error', function(err) { | ||
function fn() { | ||
@@ -66,5 +83,5 @@ throw err; | ||
}); | ||
deserializer.end(new Buffer('{')); | ||
parser.end(new Buffer('{')); | ||
}); | ||
}); |
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
15835
20
341
101