Skip to content

Commit acd041a

Browse files
committed
Refactor to externalise utilities
1 parent f2bdc52 commit acd041a

File tree

5 files changed

+138
-57
lines changed

5 files changed

+138
-57
lines changed

component.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"marker",
1313
"mdast"
1414
],
15+
"dependencies": {
16+
"wooorm/mdast-util-visit": "^0.1.1"
17+
},
1518
"repository": "wooorm/mdast-zone",
1619
"scripts": [
1720
"index.js"

index.js

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
'use strict';
22

3+
/*
4+
* Dependencies.
5+
*/
6+
7+
var visit = require('mdast-util-visit');
8+
39
/*
410
* Methods.
511
*/
@@ -83,32 +89,6 @@ function marker(name) {
8389
);
8490
}
8591

86-
/**
87-
* Visit.
88-
*
89-
* @param {Node} tree
90-
* @param {function(node, parent)} callback
91-
*/
92-
function visit(tree, callback) {
93-
/**
94-
* Visit one node.
95-
*
96-
* @param {Node} node
97-
* @param {number} index
98-
*/
99-
function one(node, index) {
100-
var parent = this || null;
101-
102-
callback(node, parent, index);
103-
104-
if (node.children) {
105-
node.children.forEach(one, node);
106-
}
107-
}
108-
109-
one(tree);
110-
}
111-
11292
/**
11393
* Parse `value` into an object.
11494
*
@@ -266,10 +246,10 @@ function run(settings) {
266246
* Passed intto `visit`.
267247
*
268248
* @param {Node} node
269-
* @param {Node} parent
270249
* @param {number} index
250+
* @param {Node} parent
271251
*/
272-
function gather(node, parent, index) {
252+
function gather(node, index, parent) {
273253
var result = test(node);
274254
var type = result && result.type;
275255

mdast-zone.js

Lines changed: 123 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mdastZone = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
22
'use strict';
33

4+
/*
5+
* Dependencies.
6+
*/
7+
8+
var visit = require('mdast-util-visit');
9+
410
/*
511
* Methods.
612
*/
@@ -84,32 +90,6 @@ function marker(name) {
8490
);
8591
}
8692

87-
/**
88-
* Visit.
89-
*
90-
* @param {Node} tree
91-
* @param {function(node, parent)} callback
92-
*/
93-
function visit(tree, callback) {
94-
/**
95-
* Visit one node.
96-
*
97-
* @param {Node} node
98-
* @param {number} index
99-
*/
100-
function one(node, index) {
101-
var parent = this || null;
102-
103-
callback(node, parent, index);
104-
105-
if (node.children) {
106-
node.children.forEach(one, node);
107-
}
108-
}
109-
110-
one(tree);
111-
}
112-
11393
/**
11494
* Parse `value` into an object.
11595
*
@@ -267,10 +247,10 @@ function run(settings) {
267247
* Passed intto `visit`.
268248
*
269249
* @param {Node} node
270-
* @param {Node} parent
271250
* @param {number} index
251+
* @param {Node} parent
272252
*/
273-
function gather(node, parent, index) {
253+
function gather(node, index, parent) {
274254
var result = test(node);
275255
var type = result && result.type;
276256

@@ -377,5 +357,120 @@ function wrapper(options) {
377357

378358
module.exports = wrapper;
379359

360+
},{"mdast-util-visit":2}],2:[function(require,module,exports){
361+
/**
362+
* @author Titus Wormer
363+
* @copyright 2015 Titus Wormer. All rights reserved.
364+
* @module mdast-util-visit
365+
* @fileoverview Utility to recursively walk over mdast nodes.
366+
*/
367+
368+
'use strict';
369+
370+
/**
371+
* Walk forwards.
372+
*
373+
* @param {Array.<*>} values - Things to iterate over,
374+
* forwards.
375+
* @param {function(*, number): boolean} callback - Function
376+
* to invoke.
377+
* @return {boolean} - False if iteration stopped.
378+
*/
379+
function forwards(values, callback) {
380+
var index = -1;
381+
var length = values.length;
382+
383+
while (++index < length) {
384+
if (callback(values[index], index) === false) {
385+
return false;
386+
}
387+
}
388+
389+
return true;
390+
}
391+
392+
/**
393+
* Walk backwards.
394+
*
395+
* @param {Array.<*>} values - Things to iterate over,
396+
* backwards.
397+
* @param {function(*, number): boolean} callback - Function
398+
* to invoke.
399+
* @return {boolean} - False if iteration stopped.
400+
*/
401+
function backwards(values, callback) {
402+
var index = values.length;
403+
var length = -1;
404+
405+
while (--index > length) {
406+
if (callback(values[index], index) === false) {
407+
return false;
408+
}
409+
}
410+
411+
return true;
412+
}
413+
414+
/**
415+
* Visit.
416+
*
417+
* @param {Node} tree - Root node
418+
* @param {string} [type] - Node type.
419+
* @param {function(node): boolean?} callback - Invoked
420+
* with each found node. Can return `false` to stop.
421+
* @param {boolean} [reverse] - By default, `visit` will
422+
* walk forwards, when `reverse` is `true`, `visit`
423+
* walks backwards.
424+
*/
425+
function visit(tree, type, callback, reverse) {
426+
var iterate;
427+
var one;
428+
var all;
429+
430+
if (typeof type === 'function') {
431+
reverse = callback;
432+
callback = type;
433+
type = null;
434+
}
435+
436+
iterate = reverse ? backwards : forwards;
437+
438+
/**
439+
* Visit `children` in `parent`.
440+
*/
441+
all = function (children, parent) {
442+
return iterate(children, function (child, index) {
443+
return child && one(child, index, parent);
444+
});
445+
};
446+
447+
/**
448+
* Visit a single node.
449+
*/
450+
one = function (node, index, parent) {
451+
var result;
452+
453+
index = index || (parent ? 0 : null);
454+
455+
if (!type || node.type === type) {
456+
result = callback(node, index, parent || null);
457+
}
458+
459+
if (node.children && result !== false) {
460+
return all(node.children, node);
461+
}
462+
463+
return result;
464+
};
465+
466+
one(tree);
467+
}
468+
469+
/*
470+
* Expose.
471+
*/
472+
473+
module.exports = visit;
474+
380475
},{}]},{},[1])(1)
381476
});

mdast-zone.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"marker",
1313
"mdast"
1414
],
15+
"dependencies": {
16+
"mdast-util-visit": "^0.1.1"
17+
},
1518
"repository": {
1619
"type": "git",
1720
"url": "https://github.com/wooorm/mdast-zone.git"

0 commit comments

Comments
 (0)