-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
179 lines (144 loc) · 3.82 KB
/
index.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { is } from "unist-util-is";
import { visit } from "unist-util-visit";
const importNodes = {
data: {
estree: {
body: [
{
source: {
raw: "'@theme/Tabs'",
type: "Literal",
value: "@theme/Tabs",
},
specifiers: [
{
local: { name: "Tabs", type: "Identifier" },
type: "ImportDefaultSpecifier",
},
],
type: "ImportDeclaration",
},
{
source: {
raw: "'@theme/TabItem'",
type: "Literal",
value: "@theme/TabItem",
},
specifiers: [
{
local: { name: "TabItem", type: "Identifier" },
type: "ImportDefaultSpecifier",
},
],
type: "ImportDeclaration",
},
],
type: "Program",
},
},
type: "mdxjsEsm",
value:
"import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';",
};
function parseMeta(nodeMeta) {
const parsedMeta = { span: 1 };
const tabTag = nodeMeta.match(/tab(={.+})?/g);
if (tabTag == null) {
return null;
}
const tabMeta = tabTag[0].split("=")[1];
if (tabMeta == null) {
return parsedMeta;
}
return { ...parsedMeta, ...JSON.parse(tabMeta) };
}
function createTabs(tabNodes, { groupId, labels, sync }) {
const attributes = [];
if (sync) {
attributes.push({
name: "groupId",
type: "mdxJsxAttribute",
value: groupId,
});
}
const children = tabNodes.map(([nodes, meta]) => {
const lang = nodes[0].lang;
const label = meta.label ?? labels.get(lang);
const value = meta.label?.toLowerCase().replace(" ", "-") ?? lang;
const attributes = [{ name: "value", type: "mdxJsxAttribute", value }];
if (label != null) {
attributes.push({ name: "label", type: "mdxJsxAttribute", value: label });
}
return {
attributes,
children: nodes,
name: "TabItem",
type: "mdxJsxFlowElement",
};
});
return { attributes, children, name: "Tabs", type: "mdxJsxFlowElement" };
}
function collectTabNodes(parent, index) {
let node = parent.children[index];
const tabNodes = [];
do {
if (is(node, "code") && typeof node.meta === "string") {
const meta = parseMeta(node.meta);
if (meta == null) {
break;
}
const spanItems = [];
while (spanItems.length < meta.span && is(node, "code")) {
spanItems.push(node);
index++;
node = parent.children[index];
}
tabNodes.push([spanItems, meta]);
} else {
break;
}
} while (index <= parent.children.length);
if (tabNodes.length === 0) {
return null;
}
return tabNodes;
}
function resolveConfig(options) {
return {
groupId: options.groupId || "code-examples",
labels: new Map([
["js", "JavaScript"],
["ts", "TypeScript"],
...(options.labels || []),
]),
sync: options.sync ?? true,
};
}
function plugin(options = {}) {
const config = resolveConfig(options);
return function transformer(tree) {
let hasTabs = false;
let includesImportTabs = false;
visit(tree, ["code", "mdxjsEsm"], (node, index, parent) => {
if (is(node, "mdxjsEsm") && node.value.includes("@theme/Tabs")) {
includesImportTabs = true;
return;
}
const tabNodes = collectTabNodes(parent, index);
if (tabNodes == null) {
return;
}
hasTabs = true;
const tabs = createTabs(tabNodes, config);
const replacedCount = tabNodes.reduce(
(nodesCount, [nodes]) => nodesCount + nodes.length,
0,
);
parent.children.splice(index, replacedCount, tabs);
});
if (hasTabs && !includesImportTabs) {
tree.children.unshift(importNodes);
}
};
}
export default plugin;