Created
June 12, 2016 06:30
-
-
Save typoerr/9165a2e9651ef6239c8b12600fcf208d to your computer and use it in GitHub Desktop.
ASTを再帰的に探索してnodeDepthをつけるremark plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 再帰的にchildren nodeを探索してnodeにnodeDepthを付ける | |
* | |
* @param {Array} children - node chldren | |
* @param {Number} depth - childrenのnodeDepth数 | |
*/ | |
function attachNodeDepth(children, depth) { | |
for (const node of children) { | |
node.nodeDepth = depth; | |
if (node.children) attachNodeDepth(node.children, depth + 1); | |
} | |
} | |
function transformer(node) { | |
const depth = 0; | |
if (node.type === 'root') node.nodeDepth = depth; | |
attachNodeDepth(node.children, depth + 1); | |
} | |
module.exports = function attacher() { | |
return transformer; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment