Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
feat(transformer): enable recursive processing in custom handler func…
Browse files Browse the repository at this point in the history
…tions

The VDOMTransformer's `handler` functions did not allow for recursive processing of child nodes so
far, i.e. when a handler function wanted to process only a single MDAST node, but hand the
processing of all MDAST child nodes back to the original transformer, it had no way of doing so,
making each `handler` function a terminal node. With this change, the signature of `handler`
functions changes to `handler(callback, node, parent, handlechild)` where `handlechild(callback,
childnode, mdastparent, hastparent)` is a callback itself that takes the current HTAST-constructing
`callback`, the MDAST `childnode` that should get transformed, the `mdastparent`, i.e. the current
node from the vantage point of the `handler` function, and with `hastparent` a HAST parent node that
the output of the child processing will be attached to.
  • Loading branch information
trieloff committed Apr 18, 2019
1 parent 73cb0fc commit 0d5193a
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/utils/mdast-to-vdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,23 @@ class VDOMTransformer {
const handlefn = that.matches(node);

// process the node
const result = handlefn(cb, node, parent);

/**
* A function that enables the recursive processing of MDAST child nodes
* in handler functions.
* @param {function} callback the HAST-constructing callback function
* @param {Node} childnode the MDAST child node that should be handled
* @param {Node} mdastparent the MDAST parent node, usually the current MDAST node
* processed by the handler function
* @param {*} hastparent the HAST parent node that the transformed child will be appended to
*/
function handlechild(callback, childnode, mdastparent, hastparent) {
if (hastparent && hastparent.children) {
hastparent.children.push(VDOMTransformer.handle(callback, childnode, mdastparent, that));
}
}

const result = handlefn(cb, node, parent, handlechild);

if (result && typeof result === 'string') {
return VDOMTransformer.toHTAST(result, cb, node);
Expand Down

0 comments on commit 0d5193a

Please sign in to comment.