You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

452 lines
12 KiB

4 years ago
  1. # snapdragon-node [![NPM version](https://img.shields.io/npm/v/snapdragon-node.svg?style=flat)](https://www.npmjs.com/package/snapdragon-node) [![NPM monthly downloads](https://img.shields.io/npm/dm/snapdragon-node.svg?style=flat)](https://npmjs.org/package/snapdragon-node) [![NPM total downloads](https://img.shields.io/npm/dt/snapdragon-node.svg?style=flat)](https://npmjs.org/package/snapdragon-node) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/snapdragon-node.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/snapdragon-node)
  2. > Snapdragon utility for creating a new AST node in custom code, such as plugins.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/):
  5. ```sh
  6. $ npm install --save snapdragon-node
  7. ```
  8. ## Usage
  9. With [snapdragon](https://github.com/jonschlinkert/snapdragon) v0.9.0 and higher you can use `this.node()` to create a new `Node`, whenever it makes sense.
  10. ```js
  11. var Node = require('snapdragon-node');
  12. var Snapdragon = require('snapdragon');
  13. var snapdragon = new Snapdragon();
  14. // example usage inside a parser visitor function
  15. snapdragon.parser.set('foo', function() {
  16. // get the current "start" position
  17. var pos = this.position();
  18. // returns the match if regex matches the substring
  19. // at the current position on `parser.input`
  20. var match = this.match(/foo/);
  21. if (match) {
  22. // call "pos" on the node, to set the start and end
  23. // positions, and return the node to push it onto the AST
  24. // (snapdragon will push the node onto the correct
  25. // nodes array, based on the stack)
  26. return pos(new Node({type: 'bar', val: match[0]}));
  27. }
  28. });
  29. ```
  30. ## API
  31. ### [Node](index.js#L22)
  32. Create a new AST `Node` with the given `val` and `type`.
  33. **Params**
  34. * `val` **{String|Object}**: Pass a matched substring, or an object to merge onto the node.
  35. * `type` **{String}**: The node type to use when `val` is a string.
  36. * `returns` **{Object}**: node instance
  37. **Example**
  38. ```js
  39. var node = new Node('*', 'Star');
  40. var node = new Node({type: 'star', val: '*'});
  41. ```
  42. ### [.isNode](index.js#L61)
  43. Returns true if the given value is a node.
  44. **Params**
  45. * `node` **{Object}**
  46. * `returns` **{Boolean}**
  47. **Example**
  48. ```js
  49. var Node = require('snapdragon-node');
  50. var node = new Node({type: 'foo'});
  51. console.log(Node.isNode(node)); //=> true
  52. console.log(Node.isNode({})); //=> false
  53. ```
  54. ### [.define](index.js#L80)
  55. Define a non-enumberable property on the node instance. Useful for adding properties that shouldn't be extended or visible during debugging.
  56. **Params**
  57. * `name` **{String}**
  58. * `val` **{any}**
  59. * `returns` **{Object}**: returns the node instance
  60. **Example**
  61. ```js
  62. var node = new Node();
  63. node.define('foo', 'something non-enumerable');
  64. ```
  65. ### [.isEmpty](index.js#L100)
  66. Returns true if `node.val` is an empty string, or `node.nodes` does not contain any non-empty text nodes.
  67. **Params**
  68. * `fn` **{Function}**: (optional) Filter function that is called on `node` and/or child nodes. `isEmpty` will return false immediately when the filter function returns false on any nodes.
  69. * `returns` **{Boolean}**
  70. **Example**
  71. ```js
  72. var node = new Node({type: 'text'});
  73. node.isEmpty(); //=> true
  74. node.val = 'foo';
  75. node.isEmpty(); //=> false
  76. ```
  77. ### [.push](index.js#L118)
  78. Given node `foo` and node `bar`, push node `bar` onto `foo.nodes`, and set `foo` as `bar.parent`.
  79. **Params**
  80. * `node` **{Object}**
  81. * `returns` **{Number}**: Returns the length of `node.nodes`
  82. **Example**
  83. ```js
  84. var foo = new Node({type: 'foo'});
  85. var bar = new Node({type: 'bar'});
  86. foo.push(bar);
  87. ```
  88. ### [.unshift](index.js#L140)
  89. Given node `foo` and node `bar`, unshift node `bar` onto `foo.nodes`, and set `foo` as `bar.parent`.
  90. **Params**
  91. * `node` **{Object}**
  92. * `returns` **{Number}**: Returns the length of `node.nodes`
  93. **Example**
  94. ```js
  95. var foo = new Node({type: 'foo'});
  96. var bar = new Node({type: 'bar'});
  97. foo.unshift(bar);
  98. ```
  99. ### [.pop](index.js#L167)
  100. Pop a node from `node.nodes`.
  101. * `returns` **{Number}**: Returns the popped `node`
  102. **Example**
  103. ```js
  104. var node = new Node({type: 'foo'});
  105. node.push(new Node({type: 'a'}));
  106. node.push(new Node({type: 'b'}));
  107. node.push(new Node({type: 'c'}));
  108. node.push(new Node({type: 'd'}));
  109. console.log(node.nodes.length);
  110. //=> 4
  111. node.pop();
  112. console.log(node.nodes.length);
  113. //=> 3
  114. ```
  115. ### [.shift](index.js#L190)
  116. Shift a node from `node.nodes`.
  117. * `returns` **{Object}**: Returns the shifted `node`
  118. **Example**
  119. ```js
  120. var node = new Node({type: 'foo'});
  121. node.push(new Node({type: 'a'}));
  122. node.push(new Node({type: 'b'}));
  123. node.push(new Node({type: 'c'}));
  124. node.push(new Node({type: 'd'}));
  125. console.log(node.nodes.length);
  126. //=> 4
  127. node.shift();
  128. console.log(node.nodes.length);
  129. //=> 3
  130. ```
  131. ### [.remove](index.js#L205)
  132. Remove `node` from `node.nodes`.
  133. **Params**
  134. * `node` **{Object}**
  135. * `returns` **{Object}**: Returns the removed node.
  136. **Example**
  137. ```js
  138. node.remove(childNode);
  139. ```
  140. ### [.find](index.js#L231)
  141. Get the first child node from `node.nodes` that matches the given `type`. If `type` is a number, the child node at that index is returned.
  142. **Params**
  143. * `type` **{String}**
  144. * `returns` **{Object}**: Returns a child node or undefined.
  145. **Example**
  146. ```js
  147. var child = node.find(1); //<= index of the node to get
  148. var child = node.find('foo'); //<= node.type of a child node
  149. var child = node.find(/^(foo|bar)$/); //<= regex to match node.type
  150. var child = node.find(['foo', 'bar']); //<= array of node.type(s)
  151. ```
  152. ### [.isType](index.js#L249)
  153. Return true if the node is the given `type`.
  154. **Params**
  155. * `type` **{String}**
  156. * `returns` **{Boolean}**
  157. **Example**
  158. ```js
  159. var node = new Node({type: 'bar'});
  160. cosole.log(node.isType('foo')); // false
  161. cosole.log(node.isType(/^(foo|bar)$/)); // true
  162. cosole.log(node.isType(['foo', 'bar'])); // true
  163. ```
  164. ### [.hasType](index.js#L270)
  165. Return true if the `node.nodes` has the given `type`.
  166. **Params**
  167. * `type` **{String}**
  168. * `returns` **{Boolean}**
  169. **Example**
  170. ```js
  171. var foo = new Node({type: 'foo'});
  172. var bar = new Node({type: 'bar'});
  173. foo.push(bar);
  174. cosole.log(foo.hasType('qux')); // false
  175. cosole.log(foo.hasType(/^(qux|bar)$/)); // true
  176. cosole.log(foo.hasType(['qux', 'bar'])); // true
  177. ```
  178. * `returns` **{Array}**
  179. **Example**
  180. ```js
  181. var foo = new Node({type: 'foo'});
  182. var bar = new Node({type: 'bar'});
  183. var baz = new Node({type: 'baz'});
  184. foo.push(bar);
  185. foo.push(baz);
  186. console.log(bar.siblings.length) // 2
  187. console.log(baz.siblings.length) // 2
  188. ```
  189. * `returns` **{Number}**
  190. **Example**
  191. ```js
  192. var foo = new Node({type: 'foo'});
  193. var bar = new Node({type: 'bar'});
  194. var baz = new Node({type: 'baz'});
  195. var qux = new Node({type: 'qux'});
  196. foo.push(bar);
  197. foo.push(baz);
  198. foo.unshift(qux);
  199. console.log(bar.index) // 1
  200. console.log(baz.index) // 2
  201. console.log(qux.index) // 0
  202. ```
  203. * `returns` **{Object}**
  204. **Example**
  205. ```js
  206. var foo = new Node({type: 'foo'});
  207. var bar = new Node({type: 'bar'});
  208. var baz = new Node({type: 'baz'});
  209. foo.push(bar);
  210. foo.push(baz);
  211. console.log(baz.prev.type) // 'bar'
  212. ```
  213. * `returns` **{Object}**
  214. **Example**
  215. ```js
  216. var foo = new Node({type: 'foo'});
  217. var bar = new Node({type: 'bar'});
  218. var baz = new Node({type: 'baz'});
  219. foo.push(bar);
  220. foo.push(baz);
  221. console.log(bar.siblings.length) // 2
  222. console.log(baz.siblings.length) // 2
  223. ```
  224. * `returns` **{Object}**: The first node, or undefiend
  225. **Example**
  226. ```js
  227. var foo = new Node({type: 'foo'});
  228. var bar = new Node({type: 'bar'});
  229. var baz = new Node({type: 'baz'});
  230. var qux = new Node({type: 'qux'});
  231. foo.push(bar);
  232. foo.push(baz);
  233. foo.push(qux);
  234. console.log(foo.first.type) // 'bar'
  235. ```
  236. * `returns` **{Object}**: The last node, or undefiend
  237. **Example**
  238. ```js
  239. var foo = new Node({type: 'foo'});
  240. var bar = new Node({type: 'bar'});
  241. var baz = new Node({type: 'baz'});
  242. var qux = new Node({type: 'qux'});
  243. foo.push(bar);
  244. foo.push(baz);
  245. foo.push(qux);
  246. console.log(foo.last.type) // 'qux'
  247. ```
  248. * `returns` **{Object}**: The last node, or undefiend
  249. **Example**
  250. ```js
  251. var foo = new Node({type: 'foo'});
  252. var bar = new Node({type: 'bar'});
  253. var baz = new Node({type: 'baz'});
  254. var qux = new Node({type: 'qux'});
  255. foo.push(bar);
  256. foo.push(baz);
  257. foo.push(qux);
  258. console.log(foo.last.type) // 'qux'
  259. ```
  260. ## Release history
  261. Changelog entries are classified using the following labels from [keep-a-changelog](https://github.com/olivierlacan/keep-a-changelog):
  262. * `added`: for new features
  263. * `changed`: for changes in existing functionality
  264. * `deprecated`: for once-stable features removed in upcoming releases
  265. * `removed`: for deprecated features removed in this release
  266. * `fixed`: for any bug fixes
  267. Custom labels used in this changelog:
  268. * `dependencies`: bumps dependencies
  269. * `housekeeping`: code re-organization, minor edits, or other changes that don't fit in one of the other categories.
  270. ### [2.0.0] - 2017-05-01
  271. **Changed**
  272. * `.unshiftNode` was renamed to [.unshift](#unshift)
  273. * `.pushNode` was renamed to [.push](#push)
  274. * `.getNode` was renamed to [.find](#find)
  275. **Added**
  276. * [.isNode](#isNode)
  277. * [.isEmpty](#isEmpty)
  278. * [.pop](#pop)
  279. * [.shift](#shift)
  280. * [.remove](#remove)
  281. ### [0.1.0]
  282. First release.
  283. ## About
  284. ### Related projects
  285. * [breakdance](https://www.npmjs.com/package/breakdance): Breakdance is a node.js library for converting HTML to markdown. Highly pluggable, flexible and easy… [more](http://breakdance.io) | [homepage](http://breakdance.io "Breakdance is a node.js library for converting HTML to markdown. Highly pluggable, flexible and easy to use. It's time for your markup to get down.")
  286. * [snapdragon-capture](https://www.npmjs.com/package/snapdragon-capture): Snapdragon plugin that adds a capture method to the parser instance. | [homepage](https://github.com/jonschlinkert/snapdragon-capture "Snapdragon plugin that adds a capture method to the parser instance.")
  287. * [snapdragon-cheerio](https://www.npmjs.com/package/snapdragon-cheerio): Snapdragon plugin for converting a cheerio AST to a snapdragon AST. | [homepage](https://github.com/jonschlinkert/snapdragon-cheerio "Snapdragon plugin for converting a cheerio AST to a snapdragon AST.")
  288. * [snapdragon-util](https://www.npmjs.com/package/snapdragon-util): Utilities for the snapdragon parser/compiler. | [homepage](https://github.com/jonschlinkert/snapdragon-util "Utilities for the snapdragon parser/compiler.")
  289. * [snapdragon](https://www.npmjs.com/package/snapdragon): Easy-to-use plugin system for creating powerful, fast and versatile parsers and compilers, with built-in source-map… [more](https://github.com/jonschlinkert/snapdragon) | [homepage](https://github.com/jonschlinkert/snapdragon "Easy-to-use plugin system for creating powerful, fast and versatile parsers and compilers, with built-in source-map support.")
  290. ### Contributing
  291. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  292. Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards.
  293. ### Building docs
  294. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  295. To generate the readme, run the following command:
  296. ```sh
  297. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  298. ```
  299. ### Running tests
  300. 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:
  301. ```sh
  302. $ npm install && npm test
  303. ```
  304. ### Author
  305. **Jon Schlinkert**
  306. * [github/jonschlinkert](https://github.com/jonschlinkert)
  307. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  308. ### License
  309. Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
  310. Released under the [MIT License](LICENSE).
  311. ***
  312. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on June 25, 2017._