forked from mongodb/chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rstToSnootyAst.ts
121 lines (112 loc) · 3.2 KB
/
rstToSnootyAst.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { stripIndent } from "common-tags";
import { SnootyNode } from "./SnootyDataSource";
import { parse, visit, findAll, AnyNode } from "docdoctor";
export const rstToSnootyAst = (rst: string): SnootyNode => {
const node = parse(rst);
// Transform docdoctor/restructured nodes to Snooty AST nodes according to
// type
visit(node, (node) => {
const transform = restructuredToSnootyTypes[node.type];
if (transform === undefined) {
return;
}
const transforms = Array.isArray(transform) ? transform : [transform];
transforms.forEach((transform) => {
Object.assign(
node,
typeof transform === "string"
? { ...node, type: transform }
: transform(node)
);
});
});
return node;
};
type Transform = (node: AnyNode) => AnyNode;
const stripTargets: Transform = (node) => {
visit(node, (node) => {
if (node.children === undefined) {
return;
}
let children = node.children
.map((child: AnyNode) => {
// Restructured leaves the target in the inner text node: remove it.
// e.g. `some ref <some-target>`
return {
...child,
value: child.value?.replace(/\s*<[^>]+>\s*$/, ""),
};
})
.filter((child: AnyNode) => {
// Remove child nodes that are just targets ("<some-target>")
return child.value !== "";
});
// Trim space from last if needed
children = children.map((child: AnyNode, i: number) => {
return i === children.length - 1
? { ...child, value: child.value.replace(/\s+$/, "") }
: child;
});
Object.assign(node, { ...node, children });
});
return node;
};
const restructuredToSnootyTypes: {
// String = quick rename of 'type' property. Function = more involved
// transformation. Array will apply transformations from left to right (a
// string is always 'rename', last one wins).
[k: string]: string | Transform | (string | Transform)[];
} = {
document: "root",
label(node) {
return {
type: "target_identifier",
children: [{ type: "target", children: node.children }],
};
},
directive(nodeIn) {
const name = nodeIn.directive;
const node = { ...nodeIn, name };
if (name === "code-block") {
const textNodes = findAll(node, ({ value }) => value !== undefined);
return {
type: "code",
lang: node.args,
value: stripIndent(
textNodes.map(({ value }: AnyNode) => value).join("\n")
),
directive: undefined,
children: undefined,
};
}
if (/^tabs.*/.test(name)) {
return { ...node, name: "tabs" };
}
if (name === "tab") {
// `argument` becomes the tab label
return { ...node, argument: [{ type: "text", value: node.args }] };
}
return node;
},
title: "heading",
interpreted_text: [stripTargets, "ref_role"],
bullet_list(node) {
return {
...node,
type: "list",
enumtype: "unordered",
};
},
list_item: "listItem",
reference: stripTargets,
enumerated_list(node) {
return {
...node,
type: "list",
enumtype: "arabic",
};
},
text(node) {
return { ...node, value: node.value.replace(/\n$/, " ") };
},
};