@scalar/code-highlight 0.3.4 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/chunks/code.js +124 -0
  3. package/dist/chunks/code.js.map +1 -0
  4. package/dist/chunks/languages.js +12720 -0
  5. package/dist/chunks/languages.js.map +1 -0
  6. package/dist/chunks/lib.js +13688 -0
  7. package/dist/chunks/lib.js.map +1 -0
  8. package/dist/chunks/lib2.js +731 -0
  9. package/dist/chunks/lib2.js.map +1 -0
  10. package/dist/chunks/markdown.js +20921 -0
  11. package/dist/chunks/markdown.js.map +1 -0
  12. package/dist/chunks/rehype-alert.js +74 -0
  13. package/dist/chunks/rehype-alert.js.map +1 -0
  14. package/dist/chunks/rehype-highlight.js +2987 -0
  15. package/dist/chunks/rehype-highlight.js.map +1 -0
  16. package/dist/code/index.d.ts +1 -1
  17. package/dist/code/index.js +3 -1
  18. package/dist/index.d.ts +1 -1
  19. package/dist/index.js +6 -5
  20. package/dist/languages/index.d.ts +3 -3
  21. package/dist/languages/index.js +2 -3
  22. package/dist/markdown/index.d.ts +1 -1
  23. package/dist/markdown/index.js +3 -1
  24. package/dist/rehype-alert/index.d.ts +1 -1
  25. package/dist/rehype-alert/index.js +2 -1
  26. package/dist/rehype-highlight/index.d.ts +1 -1
  27. package/dist/rehype-highlight/index.js +2 -1
  28. package/package.json +4 -4
  29. package/dist/code/highlight.js +0 -62
  30. package/dist/code/line-numbers.js +0 -94
  31. package/dist/constants.js +0 -11
  32. package/dist/languages/basic.js +0 -30
  33. package/dist/languages/curl.js +0 -86
  34. package/dist/languages/json-yaml.js +0 -9
  35. package/dist/languages/standard.js +0 -115
  36. package/dist/markdown/markdown.js +0 -228
  37. package/dist/rehype-alert/rehype-alert.js +0 -102
  38. package/dist/rehype-highlight/rehype-highlight.js +0 -98
@@ -0,0 +1,731 @@
1
+ //#region ../../node_modules/.pnpm/unist-util-is@6.0.0/node_modules/unist-util-is/lib/index.js
2
+ /**
3
+ * Generate an assertion from a test.
4
+ *
5
+ * Useful if you’re going to test many nodes, for example when creating a
6
+ * utility where something else passes a compatible test.
7
+ *
8
+ * The created function is a bit faster because it expects valid input only:
9
+ * a `node`, `index`, and `parent`.
10
+ *
11
+ * @param {Test} test
12
+ * * when nullish, checks if `node` is a `Node`.
13
+ * * when `string`, works like passing `(node) => node.type === test`.
14
+ * * when `function` checks if function passed the node is true.
15
+ * * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
16
+ * * when `array`, checks if any one of the subtests pass.
17
+ * @returns {Check}
18
+ * An assertion.
19
+ */
20
+ var convert = (function(test) {
21
+ if (test === null || test === void 0) return ok;
22
+ if (typeof test === "function") return castFactory(test);
23
+ if (typeof test === "object") return Array.isArray(test) ? anyFactory(test) : propsFactory(test);
24
+ if (typeof test === "string") return typeFactory(test);
25
+ throw new Error("Expected function, string, or object as test");
26
+ });
27
+ /**
28
+ * @param {Array<Props | TestFunction | string>} tests
29
+ * @returns {Check}
30
+ */
31
+ function anyFactory(tests) {
32
+ /** @type {Array<Check>} */
33
+ const checks = [];
34
+ let index = -1;
35
+ while (++index < tests.length) checks[index] = convert(tests[index]);
36
+ return castFactory(any);
37
+ /**
38
+ * @this {unknown}
39
+ * @type {TestFunction}
40
+ */
41
+ function any(...parameters) {
42
+ let index = -1;
43
+ while (++index < checks.length) if (checks[index].apply(this, parameters)) return true;
44
+ return false;
45
+ }
46
+ }
47
+ /**
48
+ * Turn an object into a test for a node with a certain fields.
49
+ *
50
+ * @param {Props} check
51
+ * @returns {Check}
52
+ */
53
+ function propsFactory(check) {
54
+ const checkAsRecord = check;
55
+ return castFactory(all);
56
+ /**
57
+ * @param {Node} node
58
+ * @returns {boolean}
59
+ */
60
+ function all(node) {
61
+ const nodeAsRecord = node;
62
+ /** @type {string} */
63
+ let key;
64
+ for (key in check) if (nodeAsRecord[key] !== checkAsRecord[key]) return false;
65
+ return true;
66
+ }
67
+ }
68
+ /**
69
+ * Turn a string into a test for a node with a certain type.
70
+ *
71
+ * @param {string} check
72
+ * @returns {Check}
73
+ */
74
+ function typeFactory(check) {
75
+ return castFactory(type);
76
+ /**
77
+ * @param {Node} node
78
+ */
79
+ function type(node) {
80
+ return node && node.type === check;
81
+ }
82
+ }
83
+ /**
84
+ * Turn a custom test into a test for a node that passes that test.
85
+ *
86
+ * @param {TestFunction} testFunction
87
+ * @returns {Check}
88
+ */
89
+ function castFactory(testFunction) {
90
+ return check;
91
+ /**
92
+ * @this {unknown}
93
+ * @type {Check}
94
+ */
95
+ function check(value, index, parent) {
96
+ return Boolean(looksLikeANode(value) && testFunction.call(this, value, typeof index === "number" ? index : void 0, parent || void 0));
97
+ }
98
+ }
99
+ function ok() {
100
+ return true;
101
+ }
102
+ /**
103
+ * @param {unknown} value
104
+ * @returns {value is Node}
105
+ */
106
+ function looksLikeANode(value) {
107
+ return value !== null && typeof value === "object" && "type" in value;
108
+ }
109
+ //#endregion
110
+ //#region ../../node_modules/.pnpm/unist-util-visit-parents@6.0.2/node_modules/unist-util-visit-parents/lib/color.js
111
+ /**
112
+ * @param {string} d
113
+ * @returns {string}
114
+ */
115
+ function color(d) {
116
+ return d;
117
+ }
118
+ //#endregion
119
+ //#region ../../node_modules/.pnpm/unist-util-visit-parents@6.0.2/node_modules/unist-util-visit-parents/lib/index.js
120
+ /**
121
+ * @import {Node as UnistNode, Parent as UnistParent} from 'unist'
122
+ */
123
+ /**
124
+ * @typedef {Exclude<import('unist-util-is').Test, undefined> | undefined} Test
125
+ * Test from `unist-util-is`.
126
+ *
127
+ * Note: we have remove and add `undefined`, because otherwise when generating
128
+ * automatic `.d.ts` files, TS tries to flatten paths from a local perspective,
129
+ * which doesn’t work when publishing on npm.
130
+ */
131
+ /**
132
+ * @typedef {(
133
+ * Fn extends (value: any) => value is infer Thing
134
+ * ? Thing
135
+ * : Fallback
136
+ * )} Predicate
137
+ * Get the value of a type guard `Fn`.
138
+ * @template Fn
139
+ * Value; typically function that is a type guard (such as `(x): x is Y`).
140
+ * @template Fallback
141
+ * Value to yield if `Fn` is not a type guard.
142
+ */
143
+ /**
144
+ * @typedef {(
145
+ * Check extends null | undefined // No test.
146
+ * ? Value
147
+ * : Value extends {type: Check} // String (type) test.
148
+ * ? Value
149
+ * : Value extends Check // Partial test.
150
+ * ? Value
151
+ * : Check extends Function // Function test.
152
+ * ? Predicate<Check, Value> extends Value
153
+ * ? Predicate<Check, Value>
154
+ * : never
155
+ * : never // Some other test?
156
+ * )} MatchesOne
157
+ * Check whether a node matches a primitive check in the type system.
158
+ * @template Value
159
+ * Value; typically unist `Node`.
160
+ * @template Check
161
+ * Value; typically `unist-util-is`-compatible test, but not arrays.
162
+ */
163
+ /**
164
+ * @typedef {(
165
+ * Check extends ReadonlyArray<infer T>
166
+ * ? MatchesOne<Value, T>
167
+ * : Check extends Array<infer T>
168
+ * ? MatchesOne<Value, T>
169
+ * : MatchesOne<Value, Check>
170
+ * )} Matches
171
+ * Check whether a node matches a check in the type system.
172
+ * @template Value
173
+ * Value; typically unist `Node`.
174
+ * @template Check
175
+ * Value; typically `unist-util-is`-compatible test.
176
+ */
177
+ /**
178
+ * @typedef {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10} Uint
179
+ * Number; capped reasonably.
180
+ */
181
+ /**
182
+ * @typedef {I extends 0 ? 1 : I extends 1 ? 2 : I extends 2 ? 3 : I extends 3 ? 4 : I extends 4 ? 5 : I extends 5 ? 6 : I extends 6 ? 7 : I extends 7 ? 8 : I extends 8 ? 9 : 10} Increment
183
+ * Increment a number in the type system.
184
+ * @template {Uint} [I=0]
185
+ * Index.
186
+ */
187
+ /**
188
+ * @typedef {(
189
+ * Node extends UnistParent
190
+ * ? Node extends {children: Array<infer Children>}
191
+ * ? Child extends Children ? Node : never
192
+ * : never
193
+ * : never
194
+ * )} InternalParent
195
+ * Collect nodes that can be parents of `Child`.
196
+ * @template {UnistNode} Node
197
+ * All node types in a tree.
198
+ * @template {UnistNode} Child
199
+ * Node to search for.
200
+ */
201
+ /**
202
+ * @typedef {InternalParent<InclusiveDescendant<Tree>, Child>} Parent
203
+ * Collect nodes in `Tree` that can be parents of `Child`.
204
+ * @template {UnistNode} Tree
205
+ * All node types in a tree.
206
+ * @template {UnistNode} Child
207
+ * Node to search for.
208
+ */
209
+ /**
210
+ * @typedef {(
211
+ * Depth extends Max
212
+ * ? never
213
+ * :
214
+ * | InternalParent<Node, Child>
215
+ * | InternalAncestor<Node, InternalParent<Node, Child>, Max, Increment<Depth>>
216
+ * )} InternalAncestor
217
+ * Collect nodes in `Tree` that can be ancestors of `Child`.
218
+ * @template {UnistNode} Node
219
+ * All node types in a tree.
220
+ * @template {UnistNode} Child
221
+ * Node to search for.
222
+ * @template {Uint} [Max=10]
223
+ * Max; searches up to this depth.
224
+ * @template {Uint} [Depth=0]
225
+ * Current depth.
226
+ */
227
+ /**
228
+ * @typedef {InternalAncestor<InclusiveDescendant<Tree>, Child>} Ancestor
229
+ * Collect nodes in `Tree` that can be ancestors of `Child`.
230
+ * @template {UnistNode} Tree
231
+ * All node types in a tree.
232
+ * @template {UnistNode} Child
233
+ * Node to search for.
234
+ */
235
+ /**
236
+ * @typedef {(
237
+ * Tree extends UnistParent
238
+ * ? Depth extends Max
239
+ * ? Tree
240
+ * : Tree | InclusiveDescendant<Tree['children'][number], Max, Increment<Depth>>
241
+ * : Tree
242
+ * )} InclusiveDescendant
243
+ * Collect all (inclusive) descendants of `Tree`.
244
+ *
245
+ * > 👉 **Note**: for performance reasons, this seems to be the fastest way to
246
+ * > recurse without actually running into an infinite loop, which the
247
+ * > previous version did.
248
+ * >
249
+ * > Practically, a max of `2` is typically enough assuming a `Root` is
250
+ * > passed, but it doesn’t improve performance.
251
+ * > It gets higher with `List > ListItem > Table > TableRow > TableCell`.
252
+ * > Using up to `10` doesn’t hurt or help either.
253
+ * @template {UnistNode} Tree
254
+ * Tree type.
255
+ * @template {Uint} [Max=10]
256
+ * Max; searches up to this depth.
257
+ * @template {Uint} [Depth=0]
258
+ * Current depth.
259
+ */
260
+ /**
261
+ * @typedef {'skip' | boolean} Action
262
+ * Union of the action types.
263
+ *
264
+ * @typedef {number} Index
265
+ * Move to the sibling at `index` next (after node itself is completely
266
+ * traversed).
267
+ *
268
+ * Useful if mutating the tree, such as removing the node the visitor is
269
+ * currently on, or any of its previous siblings.
270
+ * Results less than 0 or greater than or equal to `children.length` stop
271
+ * traversing the parent.
272
+ *
273
+ * @typedef {[(Action | null | undefined | void)?, (Index | null | undefined)?]} ActionTuple
274
+ * List with one or two values, the first an action, the second an index.
275
+ *
276
+ * @typedef {Action | ActionTuple | Index | null | undefined | void} VisitorResult
277
+ * Any value that can be returned from a visitor.
278
+ */
279
+ /**
280
+ * @callback Visitor
281
+ * Handle a node (matching `test`, if given).
282
+ *
283
+ * Visitors are free to transform `node`.
284
+ * They can also transform the parent of node (the last of `ancestors`).
285
+ *
286
+ * Replacing `node` itself, if `SKIP` is not returned, still causes its
287
+ * descendants to be walked (which is a bug).
288
+ *
289
+ * When adding or removing previous siblings of `node` (or next siblings, in
290
+ * case of reverse), the `Visitor` should return a new `Index` to specify the
291
+ * sibling to traverse after `node` is traversed.
292
+ * Adding or removing next siblings of `node` (or previous siblings, in case
293
+ * of reverse) is handled as expected without needing to return a new `Index`.
294
+ *
295
+ * Removing the children property of an ancestor still results in them being
296
+ * traversed.
297
+ * @param {Visited} node
298
+ * Found node.
299
+ * @param {Array<VisitedParents>} ancestors
300
+ * Ancestors of `node`.
301
+ * @returns {VisitorResult}
302
+ * What to do next.
303
+ *
304
+ * An `Index` is treated as a tuple of `[CONTINUE, Index]`.
305
+ * An `Action` is treated as a tuple of `[Action]`.
306
+ *
307
+ * Passing a tuple back only makes sense if the `Action` is `SKIP`.
308
+ * When the `Action` is `EXIT`, that action can be returned.
309
+ * When the `Action` is `CONTINUE`, `Index` can be returned.
310
+ * @template {UnistNode} [Visited=UnistNode]
311
+ * Visited node type.
312
+ * @template {UnistParent} [VisitedParents=UnistParent]
313
+ * Ancestor type.
314
+ */
315
+ /**
316
+ * @typedef {Visitor<Matches<InclusiveDescendant<Tree>, Check>, Ancestor<Tree, Matches<InclusiveDescendant<Tree>, Check>>>} BuildVisitor
317
+ * Build a typed `Visitor` function from a tree and a test.
318
+ *
319
+ * It will infer which values are passed as `node` and which as `parents`.
320
+ * @template {UnistNode} [Tree=UnistNode]
321
+ * Tree type.
322
+ * @template {Test} [Check=Test]
323
+ * Test type.
324
+ */
325
+ /** @type {Readonly<ActionTuple>} */
326
+ var empty = [];
327
+ /**
328
+ * Do not traverse this node’s children.
329
+ */
330
+ var SKIP = "skip";
331
+ /**
332
+ * Visit nodes, with ancestral information.
333
+ *
334
+ * This algorithm performs *depth-first* *tree traversal* in *preorder*
335
+ * (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).
336
+ *
337
+ * You can choose for which nodes `visitor` is called by passing a `test`.
338
+ * For complex tests, you should test yourself in `visitor`, as it will be
339
+ * faster and will have improved type information.
340
+ *
341
+ * Walking the tree is an intensive task.
342
+ * Make use of the return values of the visitor when possible.
343
+ * Instead of walking a tree multiple times, walk it once, use `unist-util-is`
344
+ * to check if a node matches, and then perform different operations.
345
+ *
346
+ * You can change the tree.
347
+ * See `Visitor` for more info.
348
+ *
349
+ * @overload
350
+ * @param {Tree} tree
351
+ * @param {Check} check
352
+ * @param {BuildVisitor<Tree, Check>} visitor
353
+ * @param {boolean | null | undefined} [reverse]
354
+ * @returns {undefined}
355
+ *
356
+ * @overload
357
+ * @param {Tree} tree
358
+ * @param {BuildVisitor<Tree>} visitor
359
+ * @param {boolean | null | undefined} [reverse]
360
+ * @returns {undefined}
361
+ *
362
+ * @param {UnistNode} tree
363
+ * Tree to traverse.
364
+ * @param {Visitor | Test} test
365
+ * `unist-util-is`-compatible test
366
+ * @param {Visitor | boolean | null | undefined} [visitor]
367
+ * Handle each node.
368
+ * @param {boolean | null | undefined} [reverse]
369
+ * Traverse in reverse preorder (NRL) instead of the default preorder (NLR).
370
+ * @returns {undefined}
371
+ * Nothing.
372
+ *
373
+ * @template {UnistNode} Tree
374
+ * Node type.
375
+ * @template {Test} Check
376
+ * `unist-util-is`-compatible test.
377
+ */
378
+ function visitParents(tree, test, visitor, reverse) {
379
+ /** @type {Test} */
380
+ let check;
381
+ if (typeof test === "function" && typeof visitor !== "function") {
382
+ reverse = visitor;
383
+ visitor = test;
384
+ } else check = test;
385
+ const is = convert(check);
386
+ const step = reverse ? -1 : 1;
387
+ factory(tree, void 0, [])();
388
+ /**
389
+ * @param {UnistNode} node
390
+ * @param {number | undefined} index
391
+ * @param {Array<UnistParent>} parents
392
+ */
393
+ function factory(node, index, parents) {
394
+ const value = node && typeof node === "object" ? node : {};
395
+ if (typeof value.type === "string") {
396
+ const name = typeof value.tagName === "string" ? value.tagName : typeof value.name === "string" ? value.name : void 0;
397
+ Object.defineProperty(visit, "name", { value: "node (" + color(node.type + (name ? "<" + name + ">" : "")) + ")" });
398
+ }
399
+ return visit;
400
+ function visit() {
401
+ /** @type {Readonly<ActionTuple>} */
402
+ let result = empty;
403
+ /** @type {Readonly<ActionTuple>} */
404
+ let subresult;
405
+ /** @type {number} */
406
+ let offset;
407
+ /** @type {Array<UnistParent>} */
408
+ let grandparents;
409
+ if (!test || is(node, index, parents[parents.length - 1] || void 0)) {
410
+ result = toResult(visitor(node, parents));
411
+ if (result[0] === false) return result;
412
+ }
413
+ if ("children" in node && node.children) {
414
+ const nodeAsParent = node;
415
+ if (nodeAsParent.children && result[0] !== "skip") {
416
+ offset = (reverse ? nodeAsParent.children.length : -1) + step;
417
+ grandparents = parents.concat(nodeAsParent);
418
+ while (offset > -1 && offset < nodeAsParent.children.length) {
419
+ const child = nodeAsParent.children[offset];
420
+ subresult = factory(child, offset, grandparents)();
421
+ if (subresult[0] === false) return subresult;
422
+ offset = typeof subresult[1] === "number" ? subresult[1] : offset + step;
423
+ }
424
+ }
425
+ }
426
+ return result;
427
+ }
428
+ }
429
+ }
430
+ /**
431
+ * Turn a return value into a clean result.
432
+ *
433
+ * @param {VisitorResult} value
434
+ * Valid return values from visitors.
435
+ * @returns {Readonly<ActionTuple>}
436
+ * Clean result.
437
+ */
438
+ function toResult(value) {
439
+ if (Array.isArray(value)) return value;
440
+ if (typeof value === "number") return [true, value];
441
+ return value === null || value === void 0 ? empty : [value];
442
+ }
443
+ //#endregion
444
+ //#region ../../node_modules/.pnpm/unist-util-visit@5.1.0/node_modules/unist-util-visit/lib/index.js
445
+ /**
446
+ * @import {Node as UnistNode, Parent as UnistParent} from 'unist'
447
+ * @import {VisitorResult} from 'unist-util-visit-parents'
448
+ */
449
+ /**
450
+ * @typedef {Exclude<import('unist-util-is').Test, undefined> | undefined} Test
451
+ * Test from `unist-util-is`.
452
+ *
453
+ * Note: we have remove and add `undefined`, because otherwise when generating
454
+ * automatic `.d.ts` files, TS tries to flatten paths from a local perspective,
455
+ * which doesn’t work when publishing on npm.
456
+ */
457
+ /**
458
+ * @typedef {(
459
+ * Fn extends (value: any) => value is infer Thing
460
+ * ? Thing
461
+ * : Fallback
462
+ * )} Predicate
463
+ * Get the value of a type guard `Fn`.
464
+ * @template Fn
465
+ * Value; typically function that is a type guard (such as `(x): x is Y`).
466
+ * @template Fallback
467
+ * Value to yield if `Fn` is not a type guard.
468
+ */
469
+ /**
470
+ * @typedef {(
471
+ * Check extends null | undefined // No test.
472
+ * ? Value
473
+ * : Value extends {type: Check} // String (type) test.
474
+ * ? Value
475
+ * : Value extends Check // Partial test.
476
+ * ? Value
477
+ * : Check extends Function // Function test.
478
+ * ? Predicate<Check, Value> extends Value
479
+ * ? Predicate<Check, Value>
480
+ * : never
481
+ * : never // Some other test?
482
+ * )} MatchesOne
483
+ * Check whether a node matches a primitive check in the type system.
484
+ * @template Value
485
+ * Value; typically unist `Node`.
486
+ * @template Check
487
+ * Value; typically `unist-util-is`-compatible test, but not arrays.
488
+ */
489
+ /**
490
+ * @typedef {(
491
+ * Check extends ReadonlyArray<any>
492
+ * ? MatchesOne<Value, Check[number]>
493
+ * : MatchesOne<Value, Check>
494
+ * )} Matches
495
+ * Check whether a node matches a check in the type system.
496
+ * @template Value
497
+ * Value; typically unist `Node`.
498
+ * @template Check
499
+ * Value; typically `unist-util-is`-compatible test.
500
+ */
501
+ /**
502
+ * @typedef {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10} Uint
503
+ * Number; capped reasonably.
504
+ */
505
+ /**
506
+ * @typedef {I extends 0 ? 1 : I extends 1 ? 2 : I extends 2 ? 3 : I extends 3 ? 4 : I extends 4 ? 5 : I extends 5 ? 6 : I extends 6 ? 7 : I extends 7 ? 8 : I extends 8 ? 9 : 10} Increment
507
+ * Increment a number in the type system.
508
+ * @template {Uint} [I=0]
509
+ * Index.
510
+ */
511
+ /**
512
+ * @typedef {(
513
+ * Node extends UnistParent
514
+ * ? Node extends {children: Array<infer Children>}
515
+ * ? Child extends Children ? Node : never
516
+ * : never
517
+ * : never
518
+ * )} InternalParent
519
+ * Collect nodes that can be parents of `Child`.
520
+ * @template {UnistNode} Node
521
+ * All node types in a tree.
522
+ * @template {UnistNode} Child
523
+ * Node to search for.
524
+ */
525
+ /**
526
+ * @typedef {InternalParent<InclusiveDescendant<Tree>, Child>} Parent
527
+ * Collect nodes in `Tree` that can be parents of `Child`.
528
+ * @template {UnistNode} Tree
529
+ * All node types in a tree.
530
+ * @template {UnistNode} Child
531
+ * Node to search for.
532
+ */
533
+ /**
534
+ * @typedef {(
535
+ * Depth extends Max
536
+ * ? never
537
+ * :
538
+ * | InternalParent<Node, Child>
539
+ * | InternalAncestor<Node, InternalParent<Node, Child>, Max, Increment<Depth>>
540
+ * )} InternalAncestor
541
+ * Collect nodes in `Tree` that can be ancestors of `Child`.
542
+ * @template {UnistNode} Node
543
+ * All node types in a tree.
544
+ * @template {UnistNode} Child
545
+ * Node to search for.
546
+ * @template {Uint} [Max=10]
547
+ * Max; searches up to this depth.
548
+ * @template {Uint} [Depth=0]
549
+ * Current depth.
550
+ */
551
+ /**
552
+ * @typedef {(
553
+ * Tree extends UnistParent
554
+ * ? Depth extends Max
555
+ * ? Tree
556
+ * : Tree | InclusiveDescendant<Tree['children'][number], Max, Increment<Depth>>
557
+ * : Tree
558
+ * )} InclusiveDescendant
559
+ * Collect all (inclusive) descendants of `Tree`.
560
+ *
561
+ * > 👉 **Note**: for performance reasons, this seems to be the fastest way to
562
+ * > recurse without actually running into an infinite loop, which the
563
+ * > previous version did.
564
+ * >
565
+ * > Practically, a max of `2` is typically enough assuming a `Root` is
566
+ * > passed, but it doesn’t improve performance.
567
+ * > It gets higher with `List > ListItem > Table > TableRow > TableCell`.
568
+ * > Using up to `10` doesn’t hurt or help either.
569
+ * @template {UnistNode} Tree
570
+ * Tree type.
571
+ * @template {Uint} [Max=10]
572
+ * Max; searches up to this depth.
573
+ * @template {Uint} [Depth=0]
574
+ * Current depth.
575
+ */
576
+ /**
577
+ * @callback Visitor
578
+ * Handle a node (matching `test`, if given).
579
+ *
580
+ * Visitors are free to transform `node`.
581
+ * They can also transform `parent`.
582
+ *
583
+ * Replacing `node` itself, if `SKIP` is not returned, still causes its
584
+ * descendants to be walked (which is a bug).
585
+ *
586
+ * When adding or removing previous siblings of `node` (or next siblings, in
587
+ * case of reverse), the `Visitor` should return a new `Index` to specify the
588
+ * sibling to traverse after `node` is traversed.
589
+ * Adding or removing next siblings of `node` (or previous siblings, in case
590
+ * of reverse) is handled as expected without needing to return a new `Index`.
591
+ *
592
+ * Removing the children property of `parent` still results in them being
593
+ * traversed.
594
+ * @param {Visited} node
595
+ * Found node.
596
+ * @param {Visited extends UnistNode ? number | undefined : never} index
597
+ * Index of `node` in `parent`.
598
+ * @param {Ancestor extends UnistParent ? Ancestor | undefined : never} parent
599
+ * Parent of `node`.
600
+ * @returns {VisitorResult}
601
+ * What to do next.
602
+ *
603
+ * An `Index` is treated as a tuple of `[CONTINUE, Index]`.
604
+ * An `Action` is treated as a tuple of `[Action]`.
605
+ *
606
+ * Passing a tuple back only makes sense if the `Action` is `SKIP`.
607
+ * When the `Action` is `EXIT`, that action can be returned.
608
+ * When the `Action` is `CONTINUE`, `Index` can be returned.
609
+ * @template {UnistNode} [Visited=UnistNode]
610
+ * Visited node type.
611
+ * @template {UnistParent} [Ancestor=UnistParent]
612
+ * Ancestor type.
613
+ */
614
+ /**
615
+ * @typedef {Visitor<Visited, Parent<Ancestor, Visited>>} BuildVisitorFromMatch
616
+ * Build a typed `Visitor` function from a node and all possible parents.
617
+ *
618
+ * It will infer which values are passed as `node` and which as `parent`.
619
+ * @template {UnistNode} Visited
620
+ * Node type.
621
+ * @template {UnistParent} Ancestor
622
+ * Parent type.
623
+ */
624
+ /**
625
+ * @typedef {(
626
+ * BuildVisitorFromMatch<
627
+ * Matches<Descendant, Check>,
628
+ * Extract<Descendant, UnistParent>
629
+ * >
630
+ * )} BuildVisitorFromDescendants
631
+ * Build a typed `Visitor` function from a list of descendants and a test.
632
+ *
633
+ * It will infer which values are passed as `node` and which as `parent`.
634
+ * @template {UnistNode} Descendant
635
+ * Node type.
636
+ * @template {Test} Check
637
+ * Test type.
638
+ */
639
+ /**
640
+ * @typedef {(
641
+ * BuildVisitorFromDescendants<
642
+ * InclusiveDescendant<Tree>,
643
+ * Check
644
+ * >
645
+ * )} BuildVisitor
646
+ * Build a typed `Visitor` function from a tree and a test.
647
+ *
648
+ * It will infer which values are passed as `node` and which as `parent`.
649
+ * @template {UnistNode} [Tree=UnistNode]
650
+ * Node type.
651
+ * @template {Test} [Check=Test]
652
+ * Test type.
653
+ */
654
+ /**
655
+ * Visit nodes.
656
+ *
657
+ * This algorithm performs *depth-first* *tree traversal* in *preorder*
658
+ * (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).
659
+ *
660
+ * You can choose for which nodes `visitor` is called by passing a `test`.
661
+ * For complex tests, you should test yourself in `visitor`, as it will be
662
+ * faster and will have improved type information.
663
+ *
664
+ * Walking the tree is an intensive task.
665
+ * Make use of the return values of the visitor when possible.
666
+ * Instead of walking a tree multiple times, walk it once, use `unist-util-is`
667
+ * to check if a node matches, and then perform different operations.
668
+ *
669
+ * You can change the tree.
670
+ * See `Visitor` for more info.
671
+ *
672
+ * @overload
673
+ * @param {Tree} tree
674
+ * @param {Check} check
675
+ * @param {BuildVisitor<Tree, Check>} visitor
676
+ * @param {boolean | null | undefined} [reverse]
677
+ * @returns {undefined}
678
+ *
679
+ * @overload
680
+ * @param {Tree} tree
681
+ * @param {BuildVisitor<Tree>} visitor
682
+ * @param {boolean | null | undefined} [reverse]
683
+ * @returns {undefined}
684
+ *
685
+ * @param {UnistNode} tree
686
+ * Tree to traverse.
687
+ * @param {Visitor | Test} testOrVisitor
688
+ * `unist-util-is`-compatible test (optional, omit to pass a visitor).
689
+ * @param {Visitor | boolean | null | undefined} [visitorOrReverse]
690
+ * Handle each node (when test is omitted, pass `reverse`).
691
+ * @param {boolean | null | undefined} [maybeReverse=false]
692
+ * Traverse in reverse preorder (NRL) instead of the default preorder (NLR).
693
+ * @returns {undefined}
694
+ * Nothing.
695
+ *
696
+ * @template {UnistNode} Tree
697
+ * Node type.
698
+ * @template {Test} Check
699
+ * `unist-util-is`-compatible test.
700
+ */
701
+ function visit(tree, testOrVisitor, visitorOrReverse, maybeReverse) {
702
+ /** @type {boolean | null | undefined} */
703
+ let reverse;
704
+ /** @type {Test} */
705
+ let test;
706
+ /** @type {Visitor} */
707
+ let visitor;
708
+ if (typeof testOrVisitor === "function" && typeof visitorOrReverse !== "function") {
709
+ test = void 0;
710
+ visitor = testOrVisitor;
711
+ reverse = visitorOrReverse;
712
+ } else {
713
+ test = testOrVisitor;
714
+ visitor = visitorOrReverse;
715
+ reverse = maybeReverse;
716
+ }
717
+ visitParents(tree, test, overload, reverse);
718
+ /**
719
+ * @param {UnistNode} node
720
+ * @param {Array<UnistParent>} parents
721
+ */
722
+ function overload(node, parents) {
723
+ const parent = parents[parents.length - 1];
724
+ const index = parent ? parent.children.indexOf(node) : void 0;
725
+ return visitor(node, index, parent);
726
+ }
727
+ }
728
+ //#endregion
729
+ export { convert as i, SKIP as n, visitParents as r, visit as t };
730
+
731
+ //# sourceMappingURL=lib2.js.map