markdown-code-example-inserter
Version:
Syncs code examples with markdown documentation.
32 lines (31 loc) • 1.1 kB
JavaScript
import { isHtmlNode, parseHtmlNode } from './parse-markdown.js';
/**
* Fire callback on node and all children of node recursively.
*
* @param node The starting node
* @param callback The callback to handle the node. If this returns something truthy, then the
* walking is immediately aborted.
* @returns True is walking was aborted early due to the callback returning true for a node, false
* if it was not aborted.
*/
export function walk(node, language, callback) {
if (isHtmlNode(node)) {
callback(node, 'markdown');
const parsedHtmlNode = parseHtmlNode(node);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (parsedHtmlNode.children) {
return parsedHtmlNode.children.some((htmlChild) => {
return walk(htmlChild, 'html', callback);
});
}
}
if (callback(node, language)) {
return true;
}
else if ('children' in node) {
return node.children.some((child) => {
return walk(child, language, callback);
});
}
return false;
}