add extract API

This commit is contained in:
marihachi 2021-03-28 21:14:33 +09:00
parent 909d16c85d
commit a8c2304a2b

View file

@ -33,6 +33,29 @@ export function inspect(tree: MfmNode[], action: (node: MfmNode) => void): void
}
}
export function extract(nodes: MfmNode[], type: (MfmNode['type'] | MfmNode['type'][])): MfmNode[] {
function predicate(node: MfmNode, type: (MfmNode['type'] | MfmNode['type'][])): boolean {
if (Array.isArray(type)) {
return (type.some(i => i == node.type));
}
else {
return (type == node.type);
}
}
const dest = [] as MfmNode[];
for (const node of nodes) {
if (predicate(node, type)) {
dest.push(node);
}
if (node.children != null) {
dest.push(...extract(node.children, type));
}
}
return dest;
}
export {
MfmNode,
MfmBlock,