-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.ts
25 lines (20 loc) · 755 Bytes
/
print.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { Nodes } from "npm:@types/mdast";
import { visitParents } from "npm:unist-util-visit-parents";
const nodeToStr = (node: Nodes) => {
const props = Object.entries(node)
.filter(([k]) => !["type", "children", "position"].includes(k))
.map(([k, v]): string => `${k}: ${JSON.stringify(v)}`)
.join(", ");
return node.type + (props ? ` { ${props} }` : " {}");
};
const nodeWithChildrenToStr = (node: Nodes) => {
const result: string[] = [];
visitParents(node, (node, ancesstors): void => {
const prefix = "".padStart(ancesstors.length * 4, " ");
result.push([prefix, nodeToStr(node)].join(""));
});
return result.join("\n");
};
export const print = (nodes: Nodes) => {
console.log(nodeWithChildrenToStr(nodes));
};