Skip to content
Snippets Groups Projects
Commit a8c2304a authored by marihachi's avatar marihachi
Browse files

add extract API

parent 909d16c8
No related branches found
No related tags found
No related merge requests found
......@@ -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,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment