Previously named .emit, this method appends the given value to compiler.output for the given node. Useful when you know what value should be appended advance, regardless of the actual value of node.value.
Used in compiler middleware, this onverts an AST node into an empty text node and deletes node.nodes if it exists. The advantage of this method is that, as opposed to completely removing the node, indices will not need to be re-calculated in sibling nodes, and nothing is appended to the output.
Visit node with the given fn. The built-in .visit method in snapdragon automatically calls registered compilers, this allows you to pass a visitor function.
Node{Function}: (required) Node constructor function from snapdragon-node.
filter{Function}: Optionaly specify a filter function to exclude the node.
returns{Object}: Returns the created opening node.
Example
var Node = require('snapdragon-node');snapdragon.parser.set('brace', function(node) { var match = this.match(/^{/); if (match) { var parent = new Node({type: 'brace'}); utils.addOpen(parent, Node); console.log(parent.nodes[0]): // { type: 'brace.open', value: '' }; // push the parent "brace" node onto the stack this.push(parent); // return the parent node, so it's also added to the AST return brace; }});
Node{Function}: (required) Node constructor function from snapdragon-node.
filter{Function}: Optionaly specify a filter function to exclude the node.
returns{Object}: Returns the created closing node.
Example
var Node = require('snapdragon-node');snapdragon.parser.set('brace', function(node) { var match = this.match(/^}/); if (match) { var parent = this.parent(); if (parent.type !== 'brace') { throw new Error('missing opening: ' + '}'); } utils.addClose(parent, Node); console.log(parent.nodes[parent.nodes.length - 1]): // { type: 'brace.close', value: '' }; // no need to return a node, since the parent // was already added to the AST return; }});
Pop the last node off of parent.nodes. The advantage of using this method is that it checks for node.nodes and works with any version of snapdragon-node.
returns{Number|Undefined}: Returns the length of node.nodes or undefined.
Example
var parent = new Node({type: 'foo'});utils.pushNode(parent, new Node({type: 'foo'}));utils.pushNode(parent, new Node({type: 'bar'}));utils.pushNode(parent, new Node({type: 'baz'}));console.log(parent.nodes.length); //=> 3utils.popNode(parent);console.log(parent.nodes.length); //=> 2
Shift the first node off of parent.nodes. The advantage of using this method is that it checks for node.nodes and works with any version of snapdragon-node.
returns{Number|Undefined}: Returns the length of node.nodes or undefined.
Example
var parent = new Node({type: 'foo'});utils.pushNode(parent, new Node({type: 'foo'}));utils.pushNode(parent, new Node({type: 'bar'}));utils.pushNode(parent, new Node({type: 'baz'}));console.log(parent.nodes.length); //=> 3utils.shiftNode(parent);console.log(parent.nodes.length); //=> 2
returns{Object|undefined}: Returns the removed node, if successful, or undefined if it does not exist on parent.nodes.
Example
var parent = new Node({type: 'abc'});var foo = new Node({type: 'foo'});utils.pushNode(parent, foo);utils.pushNode(parent, new Node({type: 'bar'}));utils.pushNode(parent, new Node({type: 'baz'}));console.log(parent.nodes.length); //=> 3utils.removeNode(parent, foo);console.log(parent.nodes.length); //=> 2
var Node = require('snapdragon-node');var brace = new Node({type: 'brace'});var open = new Node({type: 'brace.open'});var close = new Node({type: 'brace.close'});console.log(utils.isOpen(brace)); // falseconsole.log(utils.isOpen(open)); // trueconsole.log(utils.isOpen(close)); // false
var Node = require('snapdragon-node');var brace = new Node({type: 'brace'});var open = new Node({type: 'brace.open'});var close = new Node({type: 'brace.close'});console.log(utils.isClose(brace)); // falseconsole.log(utils.isClose(open)); // falseconsole.log(utils.isClose(close)); // true
var Node = require('snapdragon-node');var brace = new Node({type: 'brace'});var open = new Node({type: 'brace.open', value: '{'});var inner = new Node({type: 'text', value: 'a,b,c'});var close = new Node({type: 'brace.close', value: '}'});brace.push(open);brace.push(inner);brace.push(close);console.log(utils.isBlock(brace)); // true
var Node = require('snapdragon-node');var brace = new Node({ type: 'brace', nodes: []});var open = new Node({type: 'brace.open'});var close = new Node({type: 'brace.close'});console.log(utils.hasOpen(brace)); // falseconsole.log(utils.hasClose(brace)); // falsebrace.pushNode(open);brace.pushNode(close);console.log(utils.hasOpen(brace)); // trueconsole.log(utils.hasClose(brace)); // true
var state = { inside: {}};var node = new Node({type: 'brace'});var open = new Node({type: 'brace.open'});console.log(utils.isInside(state, open, 'brace')); //=> falseutils.pushNode(node, open);console.log(utils.isInside(state, open, 'brace')); //=> true
Ensure that the given value is a string and call .trim() on it,
or return an empty string.
Params
str{String}
returns{String}
About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the contributing guide for advice on opening issues, pull requests, and coding standards.
Running Tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command: