Comparing version 1.2.0 to 1.2.1
17
index.js
@@ -57,3 +57,3 @@ var through = require('through3') | ||
/** | ||
* Deserialize line-delimited JSON to commonmark AST nodes. | ||
* Deserialize line-delimited JSON to commonmark nodes. | ||
* | ||
@@ -94,2 +94,6 @@ * When a callback function is given it is added as a listener for | ||
} | ||
if(!stream) { | ||
return lines; | ||
} | ||
return deserializer; | ||
@@ -145,6 +149,8 @@ } | ||
// give callers a chance to listen for events | ||
process.nextTick(function() { | ||
ast.end(node); | ||
}) | ||
if(node) { | ||
// give callers a chance to listen for events | ||
process.nextTick(function() { | ||
ast.end(node); | ||
}) | ||
} | ||
@@ -184,3 +190,2 @@ return serializer; | ||
stringify: stringify, | ||
walker: require('./lib/node-walker'), | ||
Node: Node, | ||
@@ -187,0 +192,0 @@ NodeWalker: NodeWalker, |
var through = require('through3') | ||
, Node = require('./node') | ||
, attach = require('./attach'); | ||
, Node = require('./node'); | ||
@@ -29,8 +28,3 @@ /** | ||
// bad input | ||
if(!chunk) { | ||
return cb(); | ||
} | ||
var node = Node.createNode(chunk._type, chunk) | ||
var node = Node.createNode(chunk.type, chunk) | ||
, isDocument = Node.is(node, Node.DOCUMENT) | ||
@@ -55,9 +49,9 @@ , isEof = Node.is(node, Node.EOF) | ||
if(!this.doc) { | ||
this.doc = Node.createDocument(chunk._sourcepos, chunk); | ||
this.doc = Node.createDocument(chunk, chunk.sourcepos); | ||
// append children of nested document | ||
}else{ | ||
var next = chunk._firstChild; | ||
var next = chunk.firstChild; | ||
while(next) { | ||
this.doc.appendChild(attach(next)); | ||
next = next._next; | ||
this.doc.appendChild(Node.deserialize(next)); | ||
next = next.next; | ||
} | ||
@@ -68,3 +62,3 @@ } | ||
}else if(this.doc && !completed) { | ||
this.doc.appendChild(attach(chunk)); | ||
this.doc.appendChild(Node.deserialize(chunk)); | ||
@@ -80,8 +74,6 @@ // got EOF with a valid root doc, flush it | ||
}else if(!isMeta) { | ||
var doc = Node.createDocumentFragment(chunk, {_fragment: true}); | ||
var doc = Node.createDocumentFragment(chunk, {fragment: true}); | ||
this.push(doc); | ||
this.push(Node.createNode(Node.EOF, {_fragment: true})); | ||
this.push(Node.createNode(Node.EOF, {fragment: true})); | ||
this.emit('fragment', doc); | ||
//return cb(new Error('node without parent document: ' + chunk._type)); | ||
} | ||
@@ -88,0 +80,0 @@ |
@@ -11,7 +11,7 @@ var Node = require('./node'); | ||
function walk(node, cb) { | ||
var child = node._firstChild; | ||
var child = node.firstChild; | ||
while(child) { | ||
cb(child, node); | ||
walk(child, cb); | ||
child = child._next; | ||
child = child.next; | ||
} | ||
@@ -18,0 +18,0 @@ } |
@@ -24,6 +24,3 @@ var Node = require('commonmark/lib/node'); | ||
function is(node, type) { | ||
if(node.type) { | ||
return node.type === type; | ||
} | ||
return node._type === type; | ||
return node.type === type; | ||
} | ||
@@ -148,28 +145,2 @@ | ||
* | ||
* @deprecated | ||
* | ||
* @function attach | ||
* @param {Object} res deserialized object. | ||
* | ||
* @return {Node} an AST node. | ||
*/ | ||
function attach(res) { | ||
var n = new AstNode(res._type, res._sourcepos) | ||
, next = res._firstChild; | ||
for(var k in res) { | ||
n[k] = res[k]; | ||
} | ||
while(next) { | ||
n.appendChild(attach(next)); | ||
next = next._next; | ||
} | ||
return n; | ||
} | ||
/** | ||
* Converts a deserialized object to an AST node. | ||
* | ||
* @function deserialize | ||
@@ -193,11 +164,11 @@ * @param {Object} node serialized ast object. | ||
if(node.file) { | ||
n._file = node.file; | ||
n.file = node.file; | ||
} | ||
if(node.cmd) { | ||
n._cmd = node.cmd; | ||
n.cmd = node.cmd; | ||
} | ||
if(node.linkRefs) { | ||
n._linkRefs = node.linkRefs; | ||
n.linkRefs = node.linkRefs; | ||
} | ||
@@ -255,2 +226,7 @@ } | ||
// html | ||
if(node.htmlBlockType) { | ||
n._htmlBlockType = node.htmlBlockType; | ||
} | ||
// iterate children | ||
@@ -287,3 +263,3 @@ while(next) { | ||
if(!(node instanceof Node)) { | ||
throw new TypeError('serialize() expects a Node instance'); | ||
return node; | ||
} | ||
@@ -295,3 +271,5 @@ | ||
obj.type = node._type; | ||
obj.sourcepos = node._sourcepos; | ||
if(node._sourcepos) { | ||
obj.sourcepos = node._sourcepos; | ||
} | ||
if(node._literal) { | ||
@@ -305,12 +283,12 @@ obj.literal = node._literal; | ||
if(node._file) { | ||
obj.file = node._file; | ||
if(node.file) { | ||
obj.file = node.file; | ||
} | ||
if(node._cmd) { | ||
obj.cmd = node._cmd; | ||
if(node.cmd) { | ||
obj.cmd = node.cmd; | ||
} | ||
if(node._linkRefs) { | ||
obj.linkRefs = node._linkRefs; | ||
if(node.linkRefs) { | ||
obj.linkRefs = node.linkRefs; | ||
} | ||
@@ -370,2 +348,7 @@ } | ||
// html | ||
if(node._htmlBlockType) { | ||
obj.htmlBlockType = node._htmlBlockType; | ||
} | ||
if(node._firstChild) { | ||
@@ -385,4 +368,2 @@ obj.firstChild = serialize(node._firstChild); | ||
AstNode.attach = attach; | ||
AstNode.is = is; | ||
@@ -389,0 +370,0 @@ AstNode.createDocument = createDocument; |
var through = require('through3') | ||
, stringify = require('./stringify') | ||
, Node = require('./node') | ||
, EOL = require('os').EOL; | ||
@@ -20,3 +20,5 @@ | ||
function transform(ast, encoding, cb) { | ||
this.push(stringify(ast, this.indent) + EOL); | ||
//console.dir(ast); | ||
var res = Node.serialize(ast); | ||
this.push(JSON.stringify(res, undefined, this.indent) + EOL); | ||
cb(); | ||
@@ -23,0 +25,0 @@ } |
@@ -1,61 +0,9 @@ | ||
var Node = require('commonmark/lib/node') | ||
, defaultNode = new Node(); | ||
var Node = require('./node'); | ||
/** | ||
* Resolves circular references in an AST so it may be serialized | ||
* to JSON. | ||
* | ||
* @function resolve | ||
* | ||
* @returns {Function} a closure to use with `JSON.stringify`. | ||
*/ | ||
function resolve() { | ||
var seen = []; | ||
return function(key, val) { | ||
var invalid = key === '_parent' || key === '_prev' || key === '_lastChild' | ||
|| (val === null && (key !== 'start' && key !== 'delimiter')) | ||
|| (val === undefined) | ||
|| (defaultNode.hasOwnProperty(key) && val === defaultNode[key]) | ||
// NOTE: must do instanceof test or some listData is dropped | ||
|| Boolean(~seen.indexOf(val) && (val instanceof Node)); | ||
if(invalid) { | ||
return; | ||
} | ||
if(Array.isArray(val)) { | ||
return val.slice(); | ||
}else if(val && typeof(val) === 'object') { | ||
var o = {}; | ||
for(var k in val) { | ||
o[k] = val[k]; | ||
} | ||
if(!k) { | ||
return; | ||
} | ||
return o; | ||
} | ||
if(Boolean(~seen.indexOf(val))) { | ||
return; | ||
} | ||
if(!val || typeof(val) !== 'object') { | ||
return val; | ||
} | ||
seen.push(val); | ||
return val; | ||
} | ||
} | ||
/** | ||
* Convert a commonmark AST to JSON removing circular references but | ||
* Convert a commonmark node to JSON removing circular references but | ||
* maintaining the ability to re-create the tree. | ||
* | ||
* @function stringify | ||
* | ||
* @param {Object} ast commonmark AST. | ||
* @param {Object} node the node to stringify. | ||
* @param {Number} indent number of spaces to indent JSON. | ||
@@ -65,8 +13,6 @@ * | ||
*/ | ||
function stringify(ast, indent) { | ||
return JSON.stringify(ast, resolve(), indent); | ||
function stringify(node, indent) { | ||
return JSON.stringify(Node.serialize(node), undefined, indent); | ||
} | ||
stringify.resolve = resolve; | ||
module.exports = stringify; |
@@ -21,15 +21,9 @@ var through = require('through3') | ||
// TODO: error on bad data | ||
if(!chunk) { | ||
return cb(new Error('ast node expected')); | ||
} | ||
// expand child nodes of documents, this creates a pseudo stream | ||
// from the input document | ||
if(chunk._type === Node.DOCUMENT) { | ||
if(chunk.type === Node.DOCUMENT) { | ||
var nodes = []; | ||
var nodes = [] | ||
// push direct descendants | ||
var next = chunk._firstChild; | ||
var next = chunk.firstChild; | ||
while(next) { | ||
@@ -50,3 +44,3 @@ nodes.push(next); | ||
if(this.EOF !== false) { | ||
this.push(Node.createNode(Node.EOF, {_file: chunk._file})); | ||
this.push(Node.createNode(Node.EOF, {file: chunk.file})); | ||
} | ||
@@ -53,0 +47,0 @@ |
{ | ||
"name": "mkast", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Abstract syntax tree transformer", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,3 +7,3 @@ var expect = require('chai').expect | ||
it('should deserialize _file property', function(done) { | ||
it('should deserialize file property', function(done) { | ||
var doc = ast.parse('Text') | ||
@@ -14,3 +14,3 @@ , expected = 'README.md' | ||
doc._file = expected; | ||
doc.file = expected; | ||
obj = Node.serialize(doc); | ||
@@ -22,3 +22,3 @@ res = Node.deserialize(obj); | ||
expect(Node.is(res, Node.DOCUMENT)).to.eql(true); | ||
expect(res._file).to.eql(expected); | ||
expect(res.file).to.eql(expected); | ||
@@ -29,3 +29,3 @@ expect(doc).to.eql(res); | ||
it('should deserialize _cmd property', function(done) { | ||
it('should deserialize cmd property', function(done) { | ||
var doc = ast.parse('Text') | ||
@@ -36,3 +36,3 @@ , expected = 'pwd' | ||
doc._cmd = expected; | ||
doc.cmd = expected; | ||
obj = Node.serialize(doc); | ||
@@ -44,3 +44,3 @@ res = Node.deserialize(obj); | ||
expect(Node.is(res, Node.DOCUMENT)).to.eql(true); | ||
expect(res._cmd).to.eql(expected); | ||
expect(res.cmd).to.eql(expected); | ||
@@ -51,3 +51,3 @@ expect(doc).to.eql(res); | ||
it('should deserialize _linkRefs property', function(done) { | ||
it('should deserialize linkRefs property', function(done) { | ||
var doc = ast.parse('Text') | ||
@@ -58,3 +58,3 @@ , expected = true | ||
doc._linkRefs = expected; | ||
doc.linkRefs = expected; | ||
obj = Node.serialize(doc); | ||
@@ -66,3 +66,3 @@ res = Node.deserialize(obj); | ||
expect(Node.is(res, Node.DOCUMENT)).to.eql(true); | ||
expect(res._linkRefs).to.eql(expected); | ||
expect(res.linkRefs).to.eql(expected); | ||
@@ -160,2 +160,22 @@ expect(doc).to.eql(res); | ||
it('should deserialize html block', function(done) { | ||
var doc = ast.parse('<? @pi ?>') | ||
, obj | ||
, res; | ||
obj = Node.serialize(doc); | ||
res = Node.deserialize(obj); | ||
expect(doc).to.be.an('object'); | ||
expect(res).to.be.an('object'); | ||
expect(Node.is(res, Node.DOCUMENT)).to.eql(true); | ||
expect(Node.is(res.firstChild, Node.HTML_BLOCK)).to.eql(true); | ||
expect(res.firstChild.literal).to.eql('<? @pi ?>'); | ||
expect(doc).to.eql(res); | ||
done(); | ||
}); | ||
it('should deserialize blockquote', function(done) { | ||
@@ -181,3 +201,44 @@ var doc = ast.parse('> Quotation\n\n') | ||
it('should deserialize thematic break', function(done) { | ||
var doc = ast.parse('Foo\n\n---\n\n') | ||
, obj | ||
, res; | ||
obj = Node.serialize(doc); | ||
res = Node.deserialize(obj); | ||
expect(doc).to.be.an('object'); | ||
expect(res).to.be.an('object'); | ||
expect(Node.is(res, Node.DOCUMENT)).to.eql(true); | ||
expect(Node.is(res.firstChild, Node.PARAGRAPH)).to.eql(true); | ||
expect(Node.is(res.firstChild.next, Node.THEMATIC_BREAK)).to.eql(true); | ||
expect(doc).to.eql(res); | ||
done(); | ||
}); | ||
it('should deserialize inlines', function(done) { | ||
var doc = ast.parse('`code`_emph_**strong**') | ||
, obj | ||
, res; | ||
obj = Node.serialize(doc); | ||
res = Node.deserialize(obj); | ||
expect(doc).to.be.an('object'); | ||
expect(res).to.be.an('object'); | ||
expect(Node.is(res, Node.DOCUMENT)).to.eql(true); | ||
expect(Node.is(res.firstChild, Node.PARAGRAPH)).to.eql(true); | ||
expect(Node.is(res.firstChild.firstChild, Node.CODE)).to.eql(true); | ||
expect(Node.is(res.firstChild.firstChild.next, Node.EMPH)).to.eql(true); | ||
expect(Node.is(res.firstChild.firstChild.next.next, Node.STRONG)) | ||
.to.eql(true); | ||
expect(doc).to.eql(res); | ||
done(); | ||
}); | ||
it('should deserialize code block', function(done) { | ||
@@ -184,0 +245,0 @@ var doc = ast.parse('```javascript\nvar foo="bar";\n```') |
var expect = require('chai').expect | ||
, ParserStream = require('../../lib/parser'); | ||
describe('mkast:', function() { | ||
describe('parser stream:', function() { | ||
@@ -6,0 +6,0 @@ it('should error on bad json', function(done) { |
@@ -8,3 +8,3 @@ var expect = require('chai').expect | ||
describe('node-walker:', function() { | ||
describe('node walker:', function() { | ||
@@ -11,0 +11,0 @@ it('should walk all child nodes', function(done) { |
@@ -12,8 +12,2 @@ var expect = require('chai').expect | ||
it('should return is of type (_type)', function(done) { | ||
var node = {_type: Node.DOCUMENT}; | ||
expect(Node.is(node, Node.DOCUMENT)).to.eql(true); | ||
done(); | ||
}); | ||
it('should create default document with no arguments', function(done) { | ||
@@ -20,0 +14,0 @@ var node = Node.createDocument(); |
@@ -5,3 +5,3 @@ var expect = require('chai').expect | ||
describe('mkast:', function() { | ||
describe('parse:', function() { | ||
@@ -8,0 +8,0 @@ it('should return ast from parse()', function(done) { |
@@ -7,7 +7,4 @@ var expect = require('chai').expect | ||
it('should throw error on non-node', function(done) { | ||
function fn() { | ||
Node.serialize({}); | ||
} | ||
expect(fn).throws(/expects a node instance/i); | ||
it('should return value on non-node', function(done) { | ||
expect(Node.serialize({})).to.eql({}); | ||
done(); | ||
@@ -29,3 +26,3 @@ }); | ||
it('should serialize _file property', function(done) { | ||
it('should serialize file property', function(done) { | ||
var doc = ast.parse('Text') | ||
@@ -35,3 +32,3 @@ , expected = 'README.md' | ||
doc._file = expected; | ||
doc.file = expected; | ||
obj = Node.serialize(doc); | ||
@@ -46,3 +43,3 @@ | ||
it('should serialize _cmd property', function(done) { | ||
it('should serialize cmd property', function(done) { | ||
var doc = ast.parse('Text') | ||
@@ -52,3 +49,3 @@ , expected = 'pwd' | ||
doc._cmd = expected; | ||
doc.cmd = expected; | ||
obj = Node.serialize(doc); | ||
@@ -63,3 +60,3 @@ | ||
it('should serialize _linkRefs property', function(done) { | ||
it('should serialize linkRefs property', function(done) { | ||
var doc = ast.parse('Text') | ||
@@ -69,3 +66,3 @@ , expected = true | ||
doc._linkRefs = expected; | ||
doc.linkRefs = expected; | ||
obj = Node.serialize(doc); | ||
@@ -139,2 +136,14 @@ | ||
it('should serialize html block', function(done) { | ||
var doc = ast.parse('<? @pi ?>') | ||
, obj = Node.serialize(doc); | ||
expect(doc).to.be.an('object'); | ||
expect(obj).to.be.an('object'); | ||
expect(Node.is(obj, Node.DOCUMENT)).to.eql(true); | ||
expect(Node.is(obj.firstChild, Node.HTML_BLOCK)).to.eql(true); | ||
expect(obj.firstChild.literal).to.eql('<? @pi ?>'); | ||
done(); | ||
}); | ||
it('should serialize blockquote', function(done) { | ||
@@ -153,2 +162,29 @@ var doc = ast.parse('> Quotation\n\n') | ||
it('should serialize thematic break', function(done) { | ||
var doc = ast.parse('Foo\n\n---\n\n') | ||
, obj = Node.serialize(doc); | ||
expect(doc).to.be.an('object'); | ||
expect(obj).to.be.an('object'); | ||
expect(Node.is(obj, Node.DOCUMENT)).to.eql(true); | ||
expect(Node.is(obj.firstChild, Node.PARAGRAPH)).to.eql(true); | ||
expect(Node.is(obj.firstChild.next, Node.THEMATIC_BREAK)).to.eql(true); | ||
done(); | ||
}); | ||
it('should serialize inlines', function(done) { | ||
var doc = ast.parse('`code`_emph_**strong**') | ||
, obj = Node.serialize(doc); | ||
expect(doc).to.be.an('object'); | ||
expect(obj).to.be.an('object'); | ||
expect(Node.is(obj, Node.DOCUMENT)).to.eql(true); | ||
expect(Node.is(obj.firstChild, Node.PARAGRAPH)).to.eql(true); | ||
expect(Node.is(obj.firstChild.firstChild, Node.CODE)).to.eql(true); | ||
expect(Node.is(obj.firstChild.firstChild.next, Node.EMPH)).to.eql(true); | ||
expect(Node.is(obj.firstChild.firstChild.next.next, Node.STRONG)) | ||
.to.eql(true); | ||
done(); | ||
}); | ||
it('should serialize code block', function(done) { | ||
@@ -155,0 +191,0 @@ var doc = ast.parse('```javascript\nvar foo="bar";\n```') |
@@ -5,3 +5,3 @@ var expect = require('chai').expect | ||
describe('mkast:', function() { | ||
describe('source:', function() { | ||
@@ -8,0 +8,0 @@ it('should return stream from src()', function(done) { |
var expect = require('chai').expect | ||
, ast = require('../../index') | ||
, Serialize = require('../../lib/serialize'); | ||
, Node = ast.Node | ||
, stringify = require('../../lib/stringify'); | ||
describe('mkast:', function() { | ||
describe('stringify:', function() { | ||
it('should return stream from stringify()', function(done) { | ||
expect(ast.stringify()).to.be.instanceof(Serialize); | ||
it('should convert node to json', function(done) { | ||
var doc = ast.parse('> Quotation') | ||
, str = stringify(doc); | ||
expect(JSON.parse(str)).to.eql(Node.serialize(doc)); | ||
done(); | ||
@@ -10,0 +13,0 @@ }); |
@@ -5,3 +5,3 @@ var expect = require('chai').expect | ||
describe('mkast:', function() { | ||
describe('walk:', function() { | ||
@@ -13,2 +13,10 @@ it('should return stream from walk()', function(done) { | ||
it('should walk() with eof false', function(done) { | ||
var walker = ast.walk({eof: false}); | ||
walker.once('finish', done); | ||
walker.end(ast.parse('Text')); | ||
}); | ||
}); |
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
52022
37
1477