|
1
|
import { |
|
2
|
ArrayExpression, |
|
3
|
Estree, |
|
4
|
ExpressionJsVar, |
|
5
|
Identifier, |
|
6
|
JSXElementExpression, |
|
7
|
JSXExpressionContainer, |
|
8
|
LiteralExpression, |
|
9
|
UnistNode, |
|
10
|
UnistTree, |
|
11
|
} from "types" |
|
12
|
import type { Transformer } from "unified" |
|
13
|
import { estreeToJs, expressionToJs } from "docs-utils" |
|
14
|
|
|
15
|
type Options = { |
|
16
|
verbose?: boolean |
|
17
|
} |
|
18
|
|
|
19
|
export function validateHighlightsPlugin(options?: Options): Transformer { |
|
20
|
const { verbose } = options || {} |
|
21
|
return async (tree) => { |
|
22
|
const { visit } = await import("unist-util-visit") |
|
23
|
const highlightConsts = new Map<string, string[][]>() |
|
24
|
|
|
25
|
visit(tree as UnistTree, ["mdxjsEsm", "element"], (node: UnistNode) => { |
|
26
|
if (node.type === "mdxjsEsm") { |
|
27
|
if (node.data && "estree" in node.data && node.data.estree) { |
|
28
|
const highlightsJsVar = estreeToJs(node.data.estree) |
|
29
|
|
|
30
|
if ( |
|
31
|
!highlightsJsVar || |
|
32
|
!("name" in highlightsJsVar) || |
|
33
|
typeof highlightsJsVar.name !== "string" || |
|
34
|
!Array.isArray(highlightsJsVar.value) |
|
35
|
) { |
|
36
|
return |
|
37
|
} |
|
38
|
|
|
39
|
const highlights = getHighlightsFromExpressionJsVar( |
|
40
|
highlightsJsVar.value |
|
41
|
) |
|
42
|
if (highlights) { |
|
43
|
highlightConsts.set(highlightsJsVar.name, highlights) |
|
44
|
} |
|
45
|
} |
|
46
|
return |
|
47
|
} |
|
48
|
|
|
49
|
if ( |
|
50
|
node.tagName !== "pre" || |
|
51
|
!node.children?.length || |
|
52
|
!node.children[0].data || |
|
53
|
!("estree" in node.children[0].data) || |
|
54
|
!node.children[0].data.estree |
|
55
|
) { |
|
56
|
return |
|
57
|
} |
|
58
|
|
|
59
|
const body = (node.children[0].data.estree as Estree).body?.[0] |
|
60
|
|
|
61
|
if ( |
|
62
|
!body || |
|
63
|
body.type !== "ExpressionStatement" || |
|
64
|
!body.expression || |
|
65
|
body.expression.type !== "JSXElement" |
|
66
|
) { |
|
67
|
return |
|
68
|
} |
|
69
|
|
|
70
|
const bodyExpression = body.expression as JSXElementExpression |
|
71
|
|
|
72
|
if ( |
|
73
|
!bodyExpression.children.length || |
|
74
|
bodyExpression.children[0].type !== "JSXExpressionContainer" |
|
75
|
) { |
|
76
|
return |
|
77
|
} |
|
78
|
|
|
79
|
const codeExpression = ( |
|
80
|
bodyExpression.children[0] as JSXExpressionContainer |
|
81
|
).expression |
|
82
|
|
|
83
|
if (codeExpression.type !== "Literal") { |
|
84
|
return |
|
85
|
} |
|
86
|
|
|
87
|
const highlightsAttr = bodyExpression.openingElement?.attributes.find( |
|
88
|
(attr) => attr.name.name === "highlights" |
|
89
|
) |
|
90
|
|
|
91
|
if (!highlightsAttr) { |
|
92
|
return |
|
93
|
} |
|
94
|
|
|
95
|
let highlights: string[][] | undefined |
|
96
|
if ( |
|
97
|
highlightsAttr.value.expression.type !== "Identifier" && |
|
98
|
highlightsAttr.value.expression.type === "ArrayExpression" |
|
99
|
) { |
|
100
|
const highlightsJs = getExpressionJsVarFromArrExpression( |
|
101
|
highlightsAttr.value.expression as ArrayExpression |
|
102
|
) |
|
103
|
if (!highlightsJs || !Array.isArray(highlightsJs)) { |
|
104
|
return |
|
105
|
} |
|
106
|
highlights = getHighlightsFromExpressionJsVar(highlightsJs) |
|
107
|
|
|
108
|
if (!highlights) { |
|
109
|
return |
|
110
|
} |
|
111
|
} else { |
|
112
|
if (highlightsAttr.value.expression.type !== "Identifier") { |
|
113
|
return |
|
114
|
} |
|
115
|
const name = (highlightsAttr.value.expression as Identifier).name |
|
116
|
highlights = highlightConsts.get(name) |
|
117
|
|
|
118
|
if (!highlights) { |
|
119
|
return |
|
120
|
} |
|
121
|
} |
|
122
|
|
|
123
|
const code = (codeExpression as LiteralExpression).value as string |
|
124
|
|
|
125
|
validateCodeHighlights({ |
|
126
|
highlights, |
|
127
|
code, |
|
128
|
verbose, |
|
129
|
}) |
|
130
|
}) |
|
131
|
} |
|
132
|
} |
|
133
|
|
|
134
|
const getExpressionJsVarFromArrExpression = ( |
|
135
|
expression: ArrayExpression |
|
136
|
): ExpressionJsVar[][] => { |
|
137
|
if (!Array.isArray(expression.elements)) { |
|
138
|
return [] |
|
139
|
} |
|
140
|
return expression.elements.map((elm) => { |
|
141
|
const elmJsVar = expressionToJs(elm) |
|
142
|
if (!elmJsVar || !Array.isArray(elmJsVar)) { |
|
143
|
return [] |
|
144
|
} |
|
145
|
return elmJsVar |
|
146
|
}) |
|
147
|
} |
|
148
|
|
|
149
|
const getHighlightsFromExpressionJsVar = ( |
|
150
|
expression: ExpressionJsVar[] | ExpressionJsVar[][] |
|
151
|
): string[][] | undefined => { |
|
152
|
const highlights: string[][] = [] |
|
153
|
const shouldExtractHighlights = !Array.isArray(expression[0]) |
|
154
|
// the expression is an array where each three items are a highlight |
|
155
|
// so we need to split the expression into groups of three items first |
|
156
|
const highlightExpressions = shouldExtractHighlights |
|
157
|
? (expression as ExpressionJsVar[]).reduce((acc, item, index) => { |
|
158
|
if (index % 3 === 0) { |
|
159
|
acc.push([]) |
|
160
|
} |
|
161
|
acc[acc.length - 1].push(item) |
|
162
|
return acc |
|
163
|
}, [] as ExpressionJsVar[][]) |
|
164
|
: (expression as ExpressionJsVar[][]) |
|
165
|
|
|
166
|
if (!highlightExpressions.length) { |
|
167
|
return undefined |
|
168
|
} |
|
169
|
|
|
170
|
for (const highlightExpression of highlightExpressions) { |
|
171
|
if (highlightExpression.length !== 3) { |
|
172
|
continue |
|
173
|
} |
|
174
|
|
|
175
|
const highlight = [] |
|
176
|
for (const item of highlightExpression) { |
|
177
|
if (typeof item.data !== "string") { |
|
178
|
continue |
|
179
|
} |
|
180
|
highlight.push(item.data) |
|
181
|
} |
|
182
|
highlights.push(highlight) |
|
183
|
} |
|
184
|
|
|
185
|
return !highlights.length ? undefined : highlights |
|
186
|
} |
|
187
|
|
|
188
|
/** |
|
189
|
* Validates that the highlights of each code block are correct. It validates: |
|
190
|
* |
|
191
|
* 1. The highlight's first parameter is a string number of a valid line number in the code block. |
|
192
|
* 2. The highlight's second parameter is a string that is a valid text in the line of the code block. |
|
193
|
*/ |
|
194
|
const validateCodeHighlights = ({ |
|
195
|
highlights, |
|
196
|
code, |
|
197
|
verbose, |
|
198
|
}: { |
|
199
|
highlights: string[][] |
|
200
|
code: string |
|
201
|
verbose?: boolean |
|
202
|
}): void => { |
|
203
|
try { |
|
204
|
const lines = code.trim().split("\n") |
|
205
|
for (const highlight of highlights) { |
|
206
|
if (highlight.length < 2) { |
|
207
|
throw new Error(`Highlight array must have at least 2 elements`) |
|
208
|
} |
|
209
|
|
|
210
|
const lineNumber = highlight[0] |
|
211
|
const text = highlight[1] |
|
212
|
|
|
213
|
if (typeof lineNumber !== "string" || typeof text !== "string") { |
|
214
|
throw new Error(`Highlight array must have at least 2 elements`) |
|
215
|
} |
|
216
|
|
|
217
|
if (isNaN(Number(lineNumber))) { |
|
218
|
throw new Error(`Highlight line number must be a number`) |
|
219
|
} |
|
220
|
|
|
221
|
const line = lines[Number(lineNumber) - 1]?.trim() |
|
222
|
if (line === undefined) { |
|
223
|
throw new Error(`Highlight line number ${lineNumber} not found in code`) |
|
224
|
} |
|
225
|
|
|
226
|
if (text.length && !line.includes(text)) { |
|
227
|
throw new Error( |
|
228
|
`Highlight text ${text} not found in line ${lineNumber}` |
|
229
|
) |
|
230
|
} |
|
231
|
} |
|
232
|
} catch (err) { |
|
233
|
throw new Error( |
|
234
|
formatError({ |
|
235
|
message: (err as Error).message, |
|
236
|
code, |
|
237
|
verbose, |
|
238
|
}) |
|
239
|
) |
|
240
|
} |
|
241
|
} |
|
242
|
|
|
243
|
const formatError = ({ |
|
244
|
message, |
|
245
|
code, |
|
246
|
verbose, |
|
247
|
}: { |
|
248
|
message: string |
|
249
|
code: string |
|
250
|
verbose?: boolean |
|
251
|
}): string => { |
|
252
|
return `${message}${verbose ? `:\n${code}` : ""}` |
|
253
|
} |