Skip to content

Instantly share code, notes, and snippets.

@typoerr
Created June 12, 2016 06:30
Show Gist options
  • Save typoerr/9165a2e9651ef6239c8b12600fcf208d to your computer and use it in GitHub Desktop.
Save typoerr/9165a2e9651ef6239c8b12600fcf208d to your computer and use it in GitHub Desktop.
ASTを再帰的に探索してnodeDepthをつけるremark plugin
/**
* 再帰的に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