Comparing version 1.1.7 to 1.1.8
@@ -14,2 +14,3 @@ var through = require('through3') | ||
this.doc = null; | ||
this.open = 0; | ||
} | ||
@@ -22,22 +23,37 @@ | ||
* @member AstDeserialize | ||
* @param {Array} ast input AST document. | ||
* | ||
* @param {Array} node input AST node. | ||
* @param {String} encoding character encoding. | ||
* @param {Function} callback function. | ||
*/ | ||
function transform(ast, encoding, cb) { | ||
function transform(chunk, encoding, cb) { | ||
// create root document | ||
if(ast && ast._type === 'document') { | ||
// bad input | ||
if(!chunk) { | ||
return cb(); | ||
} | ||
// maintain link references | ||
if(ast.refs && this.doc) { | ||
this.doc.refs = this.doc.refs || []; | ||
this.doc.refs = this.doc.refs.concat(ast.refs); | ||
} | ||
var node = Node.createNode(chunk._type, chunk) | ||
, isDocument = node.is(Node.DOCUMENT) | ||
, isEof = node.is(Node.EOF) | ||
, isMeta = isEof; | ||
if(isDocument) { | ||
this.open++; | ||
} | ||
if(isEof) { | ||
this.open--; | ||
} | ||
// handle documet nodes | ||
if(isDocument) { | ||
// create root document | ||
if(!this.doc) { | ||
this.doc = Node.createDocument(); | ||
// append nested document | ||
// append children of nested document | ||
}else{ | ||
var next = ast._firstChild; | ||
var next = chunk._firstChild; | ||
while(next) { | ||
@@ -48,10 +64,28 @@ this.doc.appendChild(attach(next)); | ||
} | ||
}else if(this.doc && ast._type !== 'eof') { | ||
this.doc.appendChild(attach(ast)); | ||
// got EOF with a valid doc, flush it | ||
}else if(this.doc && ast._type === 'eof') { | ||
// maintain link references for nested documents | ||
if(chunk.refs && this.doc) { | ||
this.doc.refs = this.doc.refs || []; | ||
this.doc.refs = this.doc.refs.concat(chunk.refs); | ||
} | ||
// attach to current doc | ||
}else if(this.doc && !isEof) { | ||
this.doc.appendChild(attach(chunk)); | ||
// got EOF with a valid root doc, flush it | ||
}else if(this.doc && isEof && this.open === 0) { | ||
this.push(this.doc); | ||
this.emit('eof', this.doc); | ||
this.emit(Node.EOF, this.doc); | ||
this.doc = null; | ||
// concrete node (non-meta node) that does not | ||
// have a parent | ||
}else if(!isMeta) { | ||
console.error('dropped node %s', chunk._type); | ||
console.error(chunk); | ||
throw new Error('node without parent document'); | ||
} | ||
cb(); | ||
@@ -58,0 +92,0 @@ } |
@@ -36,2 +36,5 @@ var Node = require('commonmark/lib/node') | ||
} | ||
if(!k) { | ||
return; | ||
} | ||
return o; | ||
@@ -38,0 +41,0 @@ } |
{ | ||
"name": "mkast", | ||
"version": "1.1.7", | ||
"version": "1.1.8", | ||
"description": "Commonmark AST transformer", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,4 +6,3 @@ var expect = require('chai').expect | ||
, ast = require('../../index') | ||
, Walk = require('../../lib/walk') | ||
, ParserStream = require('../../lib/parser'); | ||
, Walk = require('../../lib/walk'); | ||
@@ -85,14 +84,2 @@ describe('mkast:', function() { | ||
it('should error on bad json', function(done) { | ||
var parser = new ParserStream(); | ||
parser.once('error', function(err) { | ||
function fn() { | ||
throw err; | ||
} | ||
expect(fn).throws(Error); | ||
done(); | ||
}); | ||
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
22956
26
581