@scalar/code-highlight 0.4.15 → 0.4.16
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.
- package/CHANGELOG.md +6 -0
- package/dist/chunks/code.js +3 -5
- package/dist/chunks/code.js.map +1 -1
- package/dist/chunks/languages.js +52 -104
- package/dist/chunks/languages.js.map +1 -1
- package/dist/chunks/lib.js +342 -208
- package/dist/chunks/lib.js.map +1 -1
- package/dist/chunks/markdown.js +975 -359
- package/dist/chunks/markdown.js.map +1 -1
- package/dist/chunks/rehype-alert.js +1 -2
- package/dist/chunks/rehype-highlight.js +206 -366
- package/dist/chunks/rehype-highlight.js.map +1 -1
- package/dist/chunks/shared-lowlight.js +84 -167
- package/dist/chunks/shared-lowlight.js.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +5 -5
package/dist/chunks/lib.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// ../../node_modules/.pnpm/unist-util-is@6.0.0/node_modules/unist-util-is/lib/index.js
|
|
2
2
|
/**
|
|
3
3
|
* Generate an assertion from a test.
|
|
4
4
|
*
|
|
@@ -111,17 +111,19 @@ function ok() {
|
|
|
111
111
|
function looksLikeANode(value) {
|
|
112
112
|
return value !== null && typeof value === "object" && "type" in value;
|
|
113
113
|
}
|
|
114
|
-
|
|
115
|
-
//#region ../../node_modules/.pnpm/unist-util-visit-parents@6.0.2/node_modules/unist-util-visit-parents/lib/color.js
|
|
114
|
+
// ../../node_modules/.pnpm/unist-util-visit-parents@6.1.0/node_modules/unist-util-visit-parents/lib/color.js
|
|
116
115
|
/**
|
|
116
|
+
* Color a string.
|
|
117
|
+
*
|
|
117
118
|
* @param {string} d
|
|
119
|
+
* Value.
|
|
118
120
|
* @returns {string}
|
|
121
|
+
* Colored value.
|
|
119
122
|
*/
|
|
120
123
|
function color(d) {
|
|
121
124
|
return d;
|
|
122
125
|
}
|
|
123
|
-
|
|
124
|
-
//#region ../../node_modules/.pnpm/unist-util-visit-parents@6.0.2/node_modules/unist-util-visit-parents/lib/index.js
|
|
126
|
+
// ../../node_modules/.pnpm/unist-util-visit-parents@6.1.0/node_modules/unist-util-visit-parents/lib/index.js
|
|
125
127
|
/**
|
|
126
128
|
* @import {Node as UnistNode, Parent as UnistParent} from 'unist'
|
|
127
129
|
*/
|
|
@@ -279,45 +281,95 @@ function color(d) {
|
|
|
279
281
|
* List with one or two values, the first an action, the second an index.
|
|
280
282
|
*
|
|
281
283
|
* @typedef {Action | ActionTuple | Index | null | undefined | void} VisitorResult
|
|
282
|
-
* Any value that can be returned from
|
|
284
|
+
* Any value that can be returned from an enter visitor.
|
|
285
|
+
*
|
|
286
|
+
* Like `VisitorResultExit`,
|
|
287
|
+
* but `SKIP` can also be returned,
|
|
288
|
+
* to not walk the children of the node,
|
|
289
|
+
* and `ActionTuple` can be used to combine an action with an index.
|
|
290
|
+
*
|
|
291
|
+
* @typedef {Index | boolean | null | undefined | void} VisitorResultExit
|
|
292
|
+
* Any value that can be returned from an exit visitor.
|
|
283
293
|
*/
|
|
284
294
|
/**
|
|
285
295
|
* @callback Visitor
|
|
286
|
-
* Handle a node (matching `test`, if given).
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
* sibling to traverse after `node` is traversed.
|
|
297
|
-
* Adding or removing next siblings of `node` (or previous siblings, in case
|
|
298
|
-
* of reverse) is handled as expected without needing to return a new `Index`.
|
|
299
|
-
*
|
|
300
|
-
* Removing the children property of an ancestor still results in them being
|
|
301
|
-
* traversed.
|
|
296
|
+
* Handle a node (matching `test`, if given) before its children.
|
|
297
|
+
*
|
|
298
|
+
* Changes inside `node` are walked next.
|
|
299
|
+
* Replacing or removing it is easier in `exit`.
|
|
300
|
+
* When replacing or removing it here,
|
|
301
|
+
* return `SKIP` (as otherwise its old children are walked),
|
|
302
|
+
* or `[SKIP, index]` to continue with the node now at `index`.
|
|
303
|
+
* Changing next siblings (in walk order) is fine;
|
|
304
|
+
* changing previous siblings needs a new `Index`.
|
|
305
|
+
* Removing an ancestor or changing its previous siblings is not supported.
|
|
302
306
|
* @param {Visited} node
|
|
303
307
|
* Found node.
|
|
304
308
|
* @param {Array<VisitedParents>} ancestors
|
|
305
309
|
* Ancestors of `node`.
|
|
310
|
+
* @param {number | undefined} index
|
|
311
|
+
* Index of `node` in its parent (the last of `ancestors`),
|
|
312
|
+
* or `undefined` for the tree itself.
|
|
306
313
|
* @returns {VisitorResult}
|
|
307
314
|
* What to do next.
|
|
308
315
|
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
316
|
+
* Like `VisitorExit`,
|
|
317
|
+
* but `SKIP` can also be returned,
|
|
318
|
+
* to not walk the children of `node`.
|
|
319
|
+
* Passing a tuple back (`ActionTuple`) only makes sense as `[SKIP, Index]`.
|
|
320
|
+
* @template {UnistNode} [Visited=UnistNode]
|
|
321
|
+
* Visited node type.
|
|
322
|
+
* @template {UnistParent} [VisitedParents=UnistParent]
|
|
323
|
+
* Ancestor type.
|
|
324
|
+
*/
|
|
325
|
+
/**
|
|
326
|
+
* @callback VisitorExit
|
|
327
|
+
* Handle a node (matching `test`, if given) after its children.
|
|
328
|
+
*
|
|
329
|
+
* Changes inside `node` are not walked,
|
|
330
|
+
* which makes this a good place to change `node` based on its children,
|
|
331
|
+
* or to replace or remove it.
|
|
332
|
+
* When removing `node` or changing previous siblings (in walk order),
|
|
333
|
+
* return a new `Index`.
|
|
334
|
+
* Removing an ancestor or changing its previous siblings is not supported.
|
|
335
|
+
* @param {Visited} node
|
|
336
|
+
* Found node.
|
|
337
|
+
* @param {Array<VisitedParents>} ancestors
|
|
338
|
+
* Ancestors of `node`.
|
|
339
|
+
* @param {number | undefined} index
|
|
340
|
+
* Index of `node` in its parent (the last of `ancestors`),
|
|
341
|
+
* or `undefined` for the tree itself.
|
|
342
|
+
* @returns {VisitorResultExit}
|
|
343
|
+
* What to do next.
|
|
311
344
|
*
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
*
|
|
345
|
+
* `CONTINUE` (`true`, nothing) continues as normal,
|
|
346
|
+
* `EXIT` (`false`) stops traversing,
|
|
347
|
+
* and `Index` continues at that sibling.
|
|
315
348
|
* @template {UnistNode} [Visited=UnistNode]
|
|
316
349
|
* Visited node type.
|
|
317
350
|
* @template {UnistParent} [VisitedParents=UnistParent]
|
|
318
351
|
* Ancestor type.
|
|
319
352
|
*/
|
|
320
353
|
/**
|
|
354
|
+
* @template {UnistNode} [Visited=UnistNode]
|
|
355
|
+
* Visited node type.
|
|
356
|
+
* @template {UnistParent} [VisitedParents=UnistParent]
|
|
357
|
+
* Ancestor type.
|
|
358
|
+
* @typedef Visitors
|
|
359
|
+
* Handle nodes when entering and exiting them.
|
|
360
|
+
* The `enter` visitor is optional while `exit` is required.
|
|
361
|
+
*
|
|
362
|
+
* Pass an `enter` visitor directly to only handle nodes when entering them.
|
|
363
|
+
*
|
|
364
|
+
* `enter` is called before children (if any) are walked and `exit` after that.
|
|
365
|
+
* `exit` is still called when `enter` returns `SKIP`.
|
|
366
|
+
* Both `enter` and `exit` are only called for nodes that match an optional `test`.
|
|
367
|
+
* @property {Visitor<Visited, VisitedParents> | null | undefined} [enter]
|
|
368
|
+
* Handle a node before its children (optional).
|
|
369
|
+
* @property {VisitorExit<Visited, VisitedParents>} exit
|
|
370
|
+
* Handle a node after its children.
|
|
371
|
+
*/
|
|
372
|
+
/**
|
|
321
373
|
* @typedef {Visitor<Matches<InclusiveDescendant<Tree>, Check>, Ancestor<Tree, Matches<InclusiveDescendant<Tree>, Check>>>} BuildVisitor
|
|
322
374
|
* Build a typed `Visitor` function from a tree and a test.
|
|
323
375
|
*
|
|
@@ -327,20 +379,34 @@ function color(d) {
|
|
|
327
379
|
* @template {Test} [Check=Test]
|
|
328
380
|
* Test type.
|
|
329
381
|
*/
|
|
382
|
+
/**
|
|
383
|
+
* @typedef {Visitors<Matches<InclusiveDescendant<Tree>, Check>, Ancestor<Tree, Matches<InclusiveDescendant<Tree>, Check>>>} BuildVisitors
|
|
384
|
+
* Build typed `Visitors` from a tree and a test.
|
|
385
|
+
*
|
|
386
|
+
* It will infer which values are passed as `node` and which as `parents`.
|
|
387
|
+
* @template {UnistNode} [Tree=UnistNode]
|
|
388
|
+
* Tree type.
|
|
389
|
+
* @template {Test} [Check=Test]
|
|
390
|
+
* Test type.
|
|
391
|
+
*/
|
|
330
392
|
/** @type {Readonly<ActionTuple>} */
|
|
331
393
|
var empty = [];
|
|
332
394
|
/**
|
|
333
395
|
* Do not traverse this node’s children.
|
|
396
|
+
* Only works when entering a node.
|
|
334
397
|
*/
|
|
335
398
|
var SKIP = "skip";
|
|
336
399
|
/**
|
|
337
400
|
* Visit nodes, with ancestral information.
|
|
338
401
|
*
|
|
339
402
|
* This algorithm performs *depth-first* *tree traversal* in *preorder*
|
|
340
|
-
* (**NLR**) or if `reverse` is given,
|
|
403
|
+
* (**NLR**) or if `reverse` is given,
|
|
404
|
+
* in *reverse preorder* (**NRL**).
|
|
405
|
+
* An `exit` visitor is called in *postorder* (**LRN**)
|
|
406
|
+
* or if `reverse` is given, in *reverse postorder* (**RLN**).
|
|
341
407
|
*
|
|
342
|
-
* You can choose
|
|
343
|
-
* For complex tests, you should test yourself in
|
|
408
|
+
* You can choose which nodes are visited by passing a `test`.
|
|
409
|
+
* For complex tests, you should test yourself in a visitor, as it will be
|
|
344
410
|
* faster and will have improved type information.
|
|
345
411
|
*
|
|
346
412
|
* Walking the tree is an intensive task.
|
|
@@ -349,29 +415,72 @@ var SKIP = "skip";
|
|
|
349
415
|
* to check if a node matches, and then perform different operations.
|
|
350
416
|
*
|
|
351
417
|
* You can change the tree.
|
|
352
|
-
*
|
|
418
|
+
* Changes inside a node made when entering it are walked,
|
|
419
|
+
* changes made when exiting it are not.
|
|
420
|
+
* See `Visitor` and `VisitorExit` for more info.
|
|
421
|
+
*
|
|
422
|
+
* @overload
|
|
423
|
+
* @param {Tree} tree
|
|
424
|
+
* Tree to traverse.
|
|
425
|
+
* @param {Check} check
|
|
426
|
+
* `unist-util-is`-compatible test.
|
|
427
|
+
* @param {BuildVisitor<Tree, Check>} enter
|
|
428
|
+
* Handle each node when entering it.
|
|
429
|
+
* @param {boolean | null | undefined} [reverse]
|
|
430
|
+
* Walk children from last to first;
|
|
431
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
432
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
433
|
+
* @returns {undefined}
|
|
434
|
+
* Nothing.
|
|
353
435
|
*
|
|
354
436
|
* @overload
|
|
355
437
|
* @param {Tree} tree
|
|
438
|
+
* Tree to traverse.
|
|
356
439
|
* @param {Check} check
|
|
357
|
-
*
|
|
440
|
+
* `unist-util-is`-compatible test.
|
|
441
|
+
* @param {BuildVisitors<Tree, Check>} visitors
|
|
442
|
+
* Handle each node when exiting it, and optionally when entering it.
|
|
358
443
|
* @param {boolean | null | undefined} [reverse]
|
|
444
|
+
* Walk children from last to first;
|
|
445
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
446
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
359
447
|
* @returns {undefined}
|
|
448
|
+
* Nothing.
|
|
360
449
|
*
|
|
361
450
|
* @overload
|
|
362
451
|
* @param {Tree} tree
|
|
363
|
-
*
|
|
452
|
+
* Tree to traverse.
|
|
453
|
+
* @param {BuildVisitor<Tree>} enter
|
|
454
|
+
* Handle each node when entering it.
|
|
364
455
|
* @param {boolean | null | undefined} [reverse]
|
|
456
|
+
* Walk children from last to first;
|
|
457
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
458
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
365
459
|
* @returns {undefined}
|
|
460
|
+
* Nothing.
|
|
461
|
+
*
|
|
462
|
+
* @overload
|
|
463
|
+
* @param {Tree} tree
|
|
464
|
+
* Tree to traverse.
|
|
465
|
+
* @param {BuildVisitors<Tree>} visitors
|
|
466
|
+
* Handle each node when exiting it, and optionally when entering it.
|
|
467
|
+
* @param {boolean | null | undefined} [reverse]
|
|
468
|
+
* Walk children from last to first;
|
|
469
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
470
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
471
|
+
* @returns {undefined}
|
|
472
|
+
* Nothing.
|
|
366
473
|
*
|
|
367
474
|
* @param {UnistNode} tree
|
|
368
475
|
* Tree to traverse.
|
|
369
|
-
* @param {Visitor | Test}
|
|
370
|
-
* `unist-util-is`-compatible test
|
|
371
|
-
* @param {Visitor | boolean | null | undefined} [
|
|
372
|
-
*
|
|
476
|
+
* @param {Visitor | Visitors | Test} testOrEnterOrVisitors
|
|
477
|
+
* `unist-util-is`-compatible test, `enter`, or `visitors`.
|
|
478
|
+
* @param {Visitor | Visitors | boolean | null | undefined} [enterOrVisitorsOrReverse]
|
|
479
|
+
* `enter`, `visitors`, or `reverse` after a test.
|
|
373
480
|
* @param {boolean | null | undefined} [reverse]
|
|
374
|
-
*
|
|
481
|
+
* Walk children from last to first;
|
|
482
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
483
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
375
484
|
* @returns {undefined}
|
|
376
485
|
* Nothing.
|
|
377
486
|
*
|
|
@@ -380,20 +489,46 @@ var SKIP = "skip";
|
|
|
380
489
|
* @template {Test} Check
|
|
381
490
|
* `unist-util-is`-compatible test.
|
|
382
491
|
*/
|
|
383
|
-
function visitParents(tree,
|
|
492
|
+
function visitParents(tree, testOrEnterOrVisitors, enterOrVisitorsOrReverse, reverse) {
|
|
493
|
+
if (!tree || typeof tree !== "object") throw new Error("Unexpected value `" + tree + "` for `tree`, expected node");
|
|
494
|
+
let preorder = true;
|
|
384
495
|
/** @type {Test} */
|
|
385
496
|
let check;
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
497
|
+
/** @type {Visitors | Visitor} */
|
|
498
|
+
let visitors;
|
|
499
|
+
/** @type {Visitor | null | undefined} */
|
|
500
|
+
let enter;
|
|
501
|
+
/** @type {VisitorExit | null | undefined} */
|
|
502
|
+
let exit;
|
|
503
|
+
if (typeof enterOrVisitorsOrReverse === "function" || enterOrVisitorsOrReverse && typeof enterOrVisitorsOrReverse === "object") {
|
|
504
|
+
check = testOrEnterOrVisitors;
|
|
505
|
+
visitors = enterOrVisitorsOrReverse;
|
|
506
|
+
if (reverse) preorder = false;
|
|
507
|
+
} else {
|
|
508
|
+
visitors = testOrEnterOrVisitors;
|
|
509
|
+
if (enterOrVisitorsOrReverse) preorder = false;
|
|
510
|
+
}
|
|
511
|
+
if (typeof visitors === "function") enter = visitors;
|
|
512
|
+
else if (visitors && typeof visitors === "object") {
|
|
513
|
+
if (visitors.enter === null || visitors.enter === void 0) {} else if (typeof visitors.enter === "function") enter = visitors.enter;
|
|
514
|
+
else throw new Error("Unexpected value `" + visitors.enter + "` for `visitors.enter`, expected optional visitor");
|
|
515
|
+
if (typeof visitors.exit === "function") exit = visitors.exit;
|
|
516
|
+
else throw new Error("Unexpected value `" + visitors.exit + "` for `visitors.exit`, expected visitor (pass a visitor directly if you only want an enter visitor)");
|
|
517
|
+
} else throw new Error("Unexpected value `" + visitors + "` for `enter` or `visitors`, expected visitor or object with visitors");
|
|
390
518
|
const is = convert(check);
|
|
391
|
-
const step =
|
|
519
|
+
const step = preorder ? 1 : -1;
|
|
392
520
|
factory(tree, void 0, [])();
|
|
393
521
|
/**
|
|
522
|
+
* Create a function to visit a node.
|
|
523
|
+
*
|
|
394
524
|
* @param {UnistNode} node
|
|
525
|
+
* Node to visit.
|
|
395
526
|
* @param {number | undefined} index
|
|
527
|
+
* Index of `node` in its parent.
|
|
396
528
|
* @param {Array<UnistParent>} parents
|
|
529
|
+
* Ancestors of `node`.
|
|
530
|
+
* @returns {() => Readonly<ActionTuple>}
|
|
531
|
+
* Visit `node`.
|
|
397
532
|
*/
|
|
398
533
|
function factory(node, index, parents) {
|
|
399
534
|
const value = node && typeof node === "object" ? node : {};
|
|
@@ -403,31 +538,31 @@ function visitParents(tree, test, visitor, reverse) {
|
|
|
403
538
|
}
|
|
404
539
|
return visit;
|
|
405
540
|
function visit() {
|
|
541
|
+
const matches = !check || is(node, index, parents[parents.length - 1] || void 0);
|
|
406
542
|
/** @type {Readonly<ActionTuple>} */
|
|
407
543
|
let result = empty;
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
/** @type {number} */
|
|
411
|
-
let offset;
|
|
412
|
-
/** @type {Array<UnistParent>} */
|
|
413
|
-
let grandparents;
|
|
414
|
-
if (!test || is(node, index, parents[parents.length - 1] || void 0)) {
|
|
415
|
-
result = toResult(visitor(node, parents));
|
|
544
|
+
if (enter && matches) {
|
|
545
|
+
result = toResult(enter(node, parents, index));
|
|
416
546
|
if (result[0] === false) return result;
|
|
417
547
|
}
|
|
418
548
|
if ("children" in node && node.children) {
|
|
419
549
|
const nodeAsParent = node;
|
|
420
550
|
if (nodeAsParent.children && result[0] !== "skip") {
|
|
421
|
-
offset = (
|
|
422
|
-
grandparents = parents
|
|
551
|
+
let offset = (preorder ? -1 : nodeAsParent.children.length) + step;
|
|
552
|
+
const grandparents = [...parents, nodeAsParent];
|
|
423
553
|
while (offset > -1 && offset < nodeAsParent.children.length) {
|
|
424
554
|
const child = nodeAsParent.children[offset];
|
|
425
|
-
subresult = factory(child, offset, grandparents)();
|
|
555
|
+
const subresult = factory(child, offset, grandparents)();
|
|
426
556
|
if (subresult[0] === false) return subresult;
|
|
427
557
|
offset = typeof subresult[1] === "number" ? subresult[1] : offset + step;
|
|
428
558
|
}
|
|
429
559
|
}
|
|
430
560
|
}
|
|
561
|
+
if (exit && matches) {
|
|
562
|
+
const exitResult = toResult(exit(node, parents, index));
|
|
563
|
+
if (exitResult[0] === "skip") throw new Error("Unexpected value `" + exitResult[0] + "` from `exit`, expected `CONTINUE`, `EXIT`, index, or nothing");
|
|
564
|
+
if (exitResult[0] === false || typeof exitResult[1] === "number") return exitResult;
|
|
565
|
+
}
|
|
431
566
|
return result;
|
|
432
567
|
}
|
|
433
568
|
}
|
|
@@ -445,11 +580,16 @@ function toResult(value) {
|
|
|
445
580
|
if (typeof value === "number") return [true, value];
|
|
446
581
|
return value === null || value === void 0 ? empty : [value];
|
|
447
582
|
}
|
|
448
|
-
|
|
449
|
-
//#region ../../node_modules/.pnpm/unist-util-visit@5.1.0/node_modules/unist-util-visit/lib/index.js
|
|
583
|
+
// ../../node_modules/.pnpm/unist-util-visit@5.2.0/node_modules/unist-util-visit/lib/index.js
|
|
450
584
|
/**
|
|
451
585
|
* @import {Node as UnistNode, Parent as UnistParent} from 'unist'
|
|
452
|
-
* @import {
|
|
586
|
+
* @import {
|
|
587
|
+
* InclusiveDescendant,
|
|
588
|
+
* Matches,
|
|
589
|
+
* Visitors as ParentsVisitors,
|
|
590
|
+
* VisitorResult,
|
|
591
|
+
* VisitorResultExit
|
|
592
|
+
* } from 'unist-util-visit-parents'
|
|
453
593
|
*/
|
|
454
594
|
/**
|
|
455
595
|
* @typedef {Exclude<import('unist-util-is').Test, undefined> | undefined} Test
|
|
@@ -461,60 +601,6 @@ function toResult(value) {
|
|
|
461
601
|
*/
|
|
462
602
|
/**
|
|
463
603
|
* @typedef {(
|
|
464
|
-
* Fn extends (value: any) => value is infer Thing
|
|
465
|
-
* ? Thing
|
|
466
|
-
* : Fallback
|
|
467
|
-
* )} Predicate
|
|
468
|
-
* Get the value of a type guard `Fn`.
|
|
469
|
-
* @template Fn
|
|
470
|
-
* Value; typically function that is a type guard (such as `(x): x is Y`).
|
|
471
|
-
* @template Fallback
|
|
472
|
-
* Value to yield if `Fn` is not a type guard.
|
|
473
|
-
*/
|
|
474
|
-
/**
|
|
475
|
-
* @typedef {(
|
|
476
|
-
* Check extends null | undefined // No test.
|
|
477
|
-
* ? Value
|
|
478
|
-
* : Value extends {type: Check} // String (type) test.
|
|
479
|
-
* ? Value
|
|
480
|
-
* : Value extends Check // Partial test.
|
|
481
|
-
* ? Value
|
|
482
|
-
* : Check extends Function // Function test.
|
|
483
|
-
* ? Predicate<Check, Value> extends Value
|
|
484
|
-
* ? Predicate<Check, Value>
|
|
485
|
-
* : never
|
|
486
|
-
* : never // Some other test?
|
|
487
|
-
* )} MatchesOne
|
|
488
|
-
* Check whether a node matches a primitive check in the type system.
|
|
489
|
-
* @template Value
|
|
490
|
-
* Value; typically unist `Node`.
|
|
491
|
-
* @template Check
|
|
492
|
-
* Value; typically `unist-util-is`-compatible test, but not arrays.
|
|
493
|
-
*/
|
|
494
|
-
/**
|
|
495
|
-
* @typedef {(
|
|
496
|
-
* Check extends ReadonlyArray<any>
|
|
497
|
-
* ? MatchesOne<Value, Check[number]>
|
|
498
|
-
* : MatchesOne<Value, Check>
|
|
499
|
-
* )} Matches
|
|
500
|
-
* Check whether a node matches a check in the type system.
|
|
501
|
-
* @template Value
|
|
502
|
-
* Value; typically unist `Node`.
|
|
503
|
-
* @template Check
|
|
504
|
-
* Value; typically `unist-util-is`-compatible test.
|
|
505
|
-
*/
|
|
506
|
-
/**
|
|
507
|
-
* @typedef {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10} Uint
|
|
508
|
-
* Number; capped reasonably.
|
|
509
|
-
*/
|
|
510
|
-
/**
|
|
511
|
-
* @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
|
|
512
|
-
* Increment a number in the type system.
|
|
513
|
-
* @template {Uint} [I=0]
|
|
514
|
-
* Index.
|
|
515
|
-
*/
|
|
516
|
-
/**
|
|
517
|
-
* @typedef {(
|
|
518
604
|
* Node extends UnistParent
|
|
519
605
|
* ? Node extends {children: Array<infer Children>}
|
|
520
606
|
* ? Child extends Children ? Node : never
|
|
@@ -536,87 +622,62 @@ function toResult(value) {
|
|
|
536
622
|
* Node to search for.
|
|
537
623
|
*/
|
|
538
624
|
/**
|
|
539
|
-
* @typedef {(
|
|
540
|
-
* Depth extends Max
|
|
541
|
-
* ? never
|
|
542
|
-
* :
|
|
543
|
-
* | InternalParent<Node, Child>
|
|
544
|
-
* | InternalAncestor<Node, InternalParent<Node, Child>, Max, Increment<Depth>>
|
|
545
|
-
* )} InternalAncestor
|
|
546
|
-
* Collect nodes in `Tree` that can be ancestors of `Child`.
|
|
547
|
-
* @template {UnistNode} Node
|
|
548
|
-
* All node types in a tree.
|
|
549
|
-
* @template {UnistNode} Child
|
|
550
|
-
* Node to search for.
|
|
551
|
-
* @template {Uint} [Max=10]
|
|
552
|
-
* Max; searches up to this depth.
|
|
553
|
-
* @template {Uint} [Depth=0]
|
|
554
|
-
* Current depth.
|
|
555
|
-
*/
|
|
556
|
-
/**
|
|
557
|
-
* @typedef {(
|
|
558
|
-
* Tree extends UnistParent
|
|
559
|
-
* ? Depth extends Max
|
|
560
|
-
* ? Tree
|
|
561
|
-
* : Tree | InclusiveDescendant<Tree['children'][number], Max, Increment<Depth>>
|
|
562
|
-
* : Tree
|
|
563
|
-
* )} InclusiveDescendant
|
|
564
|
-
* Collect all (inclusive) descendants of `Tree`.
|
|
565
|
-
*
|
|
566
|
-
* > 👉 **Note**: for performance reasons, this seems to be the fastest way to
|
|
567
|
-
* > recurse without actually running into an infinite loop, which the
|
|
568
|
-
* > previous version did.
|
|
569
|
-
* >
|
|
570
|
-
* > Practically, a max of `2` is typically enough assuming a `Root` is
|
|
571
|
-
* > passed, but it doesn’t improve performance.
|
|
572
|
-
* > It gets higher with `List > ListItem > Table > TableRow > TableCell`.
|
|
573
|
-
* > Using up to `10` doesn’t hurt or help either.
|
|
574
|
-
* @template {UnistNode} Tree
|
|
575
|
-
* Tree type.
|
|
576
|
-
* @template {Uint} [Max=10]
|
|
577
|
-
* Max; searches up to this depth.
|
|
578
|
-
* @template {Uint} [Depth=0]
|
|
579
|
-
* Current depth.
|
|
580
|
-
*/
|
|
581
|
-
/**
|
|
582
625
|
* @callback Visitor
|
|
583
|
-
* Handle a node (matching `test`, if given).
|
|
626
|
+
* Handle a node (matching `test`, if given) before its children.
|
|
584
627
|
*
|
|
585
|
-
*
|
|
586
|
-
*
|
|
587
|
-
*
|
|
588
|
-
* Replacing `node` itself, if `SKIP` is not returned, still causes its
|
|
589
|
-
* descendants to be walked (which is a bug).
|
|
590
|
-
*
|
|
591
|
-
* When adding or removing previous siblings of `node` (or next siblings, in
|
|
592
|
-
* case of reverse), the `Visitor` should return a new `Index` to specify the
|
|
593
|
-
* sibling to traverse after `node` is traversed.
|
|
594
|
-
* Adding or removing next siblings of `node` (or previous siblings, in case
|
|
595
|
-
* of reverse) is handled as expected without needing to return a new `Index`.
|
|
596
|
-
*
|
|
597
|
-
* Removing the children property of `parent` still results in them being
|
|
598
|
-
* traversed.
|
|
628
|
+
* Like `Visitor` in `unist-util-visit-parents`,
|
|
629
|
+
* but with `index` and `parent` instead of `parents`.
|
|
599
630
|
* @param {Visited} node
|
|
600
631
|
* Found node.
|
|
601
632
|
* @param {Visited extends UnistNode ? number | undefined : never} index
|
|
602
|
-
* Index of `node` in `parent
|
|
633
|
+
* Index of `node` in `parent`,
|
|
634
|
+
* or `undefined` for the tree itself.
|
|
603
635
|
* @param {Ancestor extends UnistParent ? Ancestor | undefined : never} parent
|
|
604
|
-
* Parent of `node
|
|
636
|
+
* Parent of `node`,
|
|
637
|
+
* or `undefined` for the tree itself.
|
|
605
638
|
* @returns {VisitorResult}
|
|
606
639
|
* What to do next.
|
|
640
|
+
* @template {UnistNode} [Visited=UnistNode]
|
|
641
|
+
* Visited node type.
|
|
642
|
+
* @template {UnistParent} [Ancestor=UnistParent]
|
|
643
|
+
* Ancestor type.
|
|
644
|
+
*/
|
|
645
|
+
/**
|
|
646
|
+
* @callback VisitorExit
|
|
647
|
+
* Handle a node (matching `test`, if given) after its children.
|
|
607
648
|
*
|
|
608
|
-
*
|
|
609
|
-
*
|
|
610
|
-
*
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
*
|
|
649
|
+
* Like `VisitorExit` in `unist-util-visit-parents`,
|
|
650
|
+
* but with `index` and `parent` instead of `parents`.
|
|
651
|
+
* @param {Visited} node
|
|
652
|
+
* Found node.
|
|
653
|
+
* @param {Visited extends UnistNode ? number | undefined : never} index
|
|
654
|
+
* Index of `node` in `parent`,
|
|
655
|
+
* or `undefined` for the tree itself.
|
|
656
|
+
* @param {Ancestor extends UnistParent ? Ancestor | undefined : never} parent
|
|
657
|
+
* Parent of `node`,
|
|
658
|
+
* or `undefined` for the tree itself.
|
|
659
|
+
* @returns {VisitorResultExit}
|
|
660
|
+
* What to do next.
|
|
614
661
|
* @template {UnistNode} [Visited=UnistNode]
|
|
615
662
|
* Visited node type.
|
|
616
663
|
* @template {UnistParent} [Ancestor=UnistParent]
|
|
617
664
|
* Ancestor type.
|
|
618
665
|
*/
|
|
619
666
|
/**
|
|
667
|
+
* @template {UnistNode} [Visited=UnistNode]
|
|
668
|
+
* Visited node type.
|
|
669
|
+
* @template {UnistParent} [Ancestor=UnistParent]
|
|
670
|
+
* Ancestor type.
|
|
671
|
+
* @typedef Visitors
|
|
672
|
+
* Handle nodes when entering and exiting them.
|
|
673
|
+
*
|
|
674
|
+
* Like `Visitors` in `unist-util-visit-parents`.
|
|
675
|
+
* @property {Visitor<Visited, Ancestor> | null | undefined} [enter]
|
|
676
|
+
* Handle a node before its children (optional).
|
|
677
|
+
* @property {VisitorExit<Visited, Ancestor>} exit
|
|
678
|
+
* Handle a node after its children.
|
|
679
|
+
*/
|
|
680
|
+
/**
|
|
620
681
|
* @typedef {Visitor<Visited, Parent<Ancestor, Visited>>} BuildVisitorFromMatch
|
|
621
682
|
* Build a typed `Visitor` function from a node and all possible parents.
|
|
622
683
|
*
|
|
@@ -657,13 +718,34 @@ function toResult(value) {
|
|
|
657
718
|
* Test type.
|
|
658
719
|
*/
|
|
659
720
|
/**
|
|
721
|
+
* @typedef {(
|
|
722
|
+
* Visitors<
|
|
723
|
+
* Matches<InclusiveDescendant<Tree>, Check>,
|
|
724
|
+
* Parent<
|
|
725
|
+
* Extract<InclusiveDescendant<Tree>, UnistParent>,
|
|
726
|
+
* Matches<InclusiveDescendant<Tree>, Check>
|
|
727
|
+
* >
|
|
728
|
+
* >
|
|
729
|
+
* )} BuildVisitors
|
|
730
|
+
* Build typed `Visitors` from a tree and a test.
|
|
731
|
+
*
|
|
732
|
+
* It will infer which values are passed as `node` and which as `parent`.
|
|
733
|
+
* @template {UnistNode} [Tree=UnistNode]
|
|
734
|
+
* Node type.
|
|
735
|
+
* @template {Test} [Check=Test]
|
|
736
|
+
* Test type.
|
|
737
|
+
*/
|
|
738
|
+
/**
|
|
660
739
|
* Visit nodes.
|
|
661
740
|
*
|
|
662
741
|
* This algorithm performs *depth-first* *tree traversal* in *preorder*
|
|
663
|
-
* (**NLR**) or if `reverse` is given,
|
|
742
|
+
* (**NLR**) or if `reverse` is given,
|
|
743
|
+
* in *reverse preorder* (**NRL**).
|
|
744
|
+
* An `exit` visitor is called in *postorder* (**LRN**)
|
|
745
|
+
* or if `reverse` is given, in *reverse postorder* (**RLN**).
|
|
664
746
|
*
|
|
665
|
-
* You can choose
|
|
666
|
-
* For complex tests, you should test yourself in
|
|
747
|
+
* You can choose which nodes are visited by passing a `test`.
|
|
748
|
+
* For complex tests, you should test yourself in a visitor, as it will be
|
|
667
749
|
* faster and will have improved type information.
|
|
668
750
|
*
|
|
669
751
|
* Walking the tree is an intensive task.
|
|
@@ -672,29 +754,48 @@ function toResult(value) {
|
|
|
672
754
|
* to check if a node matches, and then perform different operations.
|
|
673
755
|
*
|
|
674
756
|
* You can change the tree.
|
|
675
|
-
*
|
|
757
|
+
* Changes inside a node made when entering it are walked,
|
|
758
|
+
* changes made when exiting it are not.
|
|
759
|
+
* See `Visitor` and `VisitorExit` for more info.
|
|
676
760
|
*
|
|
677
761
|
* @overload
|
|
678
762
|
* @param {Tree} tree
|
|
763
|
+
* Tree to traverse.
|
|
679
764
|
* @param {Check} check
|
|
680
|
-
*
|
|
765
|
+
* `unist-util-is`-compatible test.
|
|
766
|
+
* @param {BuildVisitor<Tree, Check> | BuildVisitors<Tree, Check>} visitors
|
|
767
|
+
* Handle each node when entering it,
|
|
768
|
+
* or `{enter, exit}` to also handle it when exiting it.
|
|
681
769
|
* @param {boolean | null | undefined} [reverse]
|
|
770
|
+
* Walk children from last to first;
|
|
771
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
772
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
682
773
|
* @returns {undefined}
|
|
774
|
+
* Nothing.
|
|
683
775
|
*
|
|
684
776
|
* @overload
|
|
685
777
|
* @param {Tree} tree
|
|
686
|
-
*
|
|
778
|
+
* Tree to traverse.
|
|
779
|
+
* @param {BuildVisitor<Tree> | BuildVisitors<Tree>} visitors
|
|
780
|
+
* Handle each node when entering it,
|
|
781
|
+
* or `{enter, exit}` to also handle it when exiting it.
|
|
687
782
|
* @param {boolean | null | undefined} [reverse]
|
|
783
|
+
* Walk children from last to first;
|
|
784
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
785
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
688
786
|
* @returns {undefined}
|
|
787
|
+
* Nothing.
|
|
689
788
|
*
|
|
690
789
|
* @param {UnistNode} tree
|
|
691
790
|
* Tree to traverse.
|
|
692
|
-
* @param {Visitor | Test}
|
|
693
|
-
* `unist-util-is`-compatible test
|
|
694
|
-
* @param {Visitor | boolean | null | undefined} [
|
|
695
|
-
*
|
|
696
|
-
* @param {boolean | null | undefined} [maybeReverse
|
|
697
|
-
*
|
|
791
|
+
* @param {Visitor | Visitors | Test} testOrEnterOrVisitors
|
|
792
|
+
* `unist-util-is`-compatible test, `enter`, or `visitors`.
|
|
793
|
+
* @param {Visitor | Visitors | boolean | null | undefined} [enterOrVisitorsOrReverse]
|
|
794
|
+
* `enter`, `visitors`, or `reverse` after a test.
|
|
795
|
+
* @param {boolean | null | undefined} [maybeReverse]
|
|
796
|
+
* Walk children from last to first;
|
|
797
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
798
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
698
799
|
* @returns {undefined}
|
|
699
800
|
* Nothing.
|
|
700
801
|
*
|
|
@@ -703,34 +804,67 @@ function toResult(value) {
|
|
|
703
804
|
* @template {Test} Check
|
|
704
805
|
* `unist-util-is`-compatible test.
|
|
705
806
|
*/
|
|
706
|
-
function visit(tree,
|
|
807
|
+
function visit(tree, testOrEnterOrVisitors, enterOrVisitorsOrReverse, maybeReverse) {
|
|
808
|
+
/** @type {Test} */
|
|
809
|
+
let check;
|
|
810
|
+
/** @type {Visitor | Visitors} */
|
|
811
|
+
let visitors;
|
|
707
812
|
/** @type {boolean | null | undefined} */
|
|
708
813
|
let reverse;
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
let visitor;
|
|
713
|
-
if (typeof testOrVisitor === "function" && typeof visitorOrReverse !== "function") {
|
|
714
|
-
test = void 0;
|
|
715
|
-
visitor = testOrVisitor;
|
|
716
|
-
reverse = visitorOrReverse;
|
|
717
|
-
} else {
|
|
718
|
-
test = testOrVisitor;
|
|
719
|
-
visitor = visitorOrReverse;
|
|
814
|
+
if (typeof enterOrVisitorsOrReverse === "function" || enterOrVisitorsOrReverse && typeof enterOrVisitorsOrReverse === "object") {
|
|
815
|
+
check = testOrEnterOrVisitors;
|
|
816
|
+
visitors = enterOrVisitorsOrReverse;
|
|
720
817
|
reverse = maybeReverse;
|
|
818
|
+
} else {
|
|
819
|
+
visitors = testOrEnterOrVisitors;
|
|
820
|
+
reverse = enterOrVisitorsOrReverse;
|
|
721
821
|
}
|
|
722
|
-
visitParents(tree,
|
|
822
|
+
if (typeof visitors === "function") visitParents(tree, check, wrap(visitors), reverse);
|
|
823
|
+
else visitParents(tree, check, wrapVisitors(visitors), reverse);
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Turn `visit` visitors into `visitParents` visitors.
|
|
827
|
+
*
|
|
828
|
+
* @param {Visitors} visitors
|
|
829
|
+
* Visitors for `visit`.
|
|
830
|
+
* @returns {ParentsVisitors}
|
|
831
|
+
* Visitors for `visitParents`.
|
|
832
|
+
*/
|
|
833
|
+
function wrapVisitors(visitors) {
|
|
834
|
+
if (!visitors || typeof visitors !== "object") return visitors;
|
|
835
|
+
return {
|
|
836
|
+
enter: typeof visitors.enter === "function" ? wrap(visitors.enter) : visitors.enter,
|
|
837
|
+
exit: typeof visitors.exit === "function" ? wrap(visitors.exit) : visitors.exit
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
/**
|
|
841
|
+
* Turn a `visit` visitor into a `visitParents` one.
|
|
842
|
+
*
|
|
843
|
+
* @template {VisitorResult} Result
|
|
844
|
+
* Result type.
|
|
845
|
+
* @param {(node: UnistNode, index: number | undefined, parent: UnistParent | undefined) => Result} visitor
|
|
846
|
+
* Visitor for `visit`.
|
|
847
|
+
* @returns {(node: UnistNode, parents: Array<UnistParent>, index: number | undefined) => Result}
|
|
848
|
+
* Visitor for `visitParents`.
|
|
849
|
+
*/
|
|
850
|
+
function wrap(visitor) {
|
|
851
|
+
return overload;
|
|
723
852
|
/**
|
|
853
|
+
* Call `visitor` with `index` and `parent` instead of `parents`.
|
|
854
|
+
*
|
|
724
855
|
* @param {UnistNode} node
|
|
856
|
+
* Visited node.
|
|
725
857
|
* @param {Array<UnistParent>} parents
|
|
858
|
+
* Ancestors of `node`.
|
|
859
|
+
* @param {number | undefined} index
|
|
860
|
+
* Index of `node` in its parent.
|
|
861
|
+
* @returns {Result}
|
|
862
|
+
* What to do next.
|
|
726
863
|
*/
|
|
727
|
-
function overload(node, parents) {
|
|
728
|
-
|
|
729
|
-
const index = parent ? parent.children.indexOf(node) : void 0;
|
|
730
|
-
return visitor(node, index, parent);
|
|
864
|
+
function overload(node, parents, index) {
|
|
865
|
+
return visitor(node, index, parents[parents.length - 1]);
|
|
731
866
|
}
|
|
732
867
|
}
|
|
733
|
-
//#endregion
|
|
734
868
|
export { convert as i, SKIP as n, visitParents as r, visit as t };
|
|
735
869
|
|
|
736
870
|
//# sourceMappingURL=lib.js.map
|