@scalar/code-highlight 0.4.15 → 0.4.17
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 +15 -0
- package/dist/chunks/code.js +13613 -6
- 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 -213
- package/dist/chunks/lib.js.map +1 -1
- package/dist/chunks/markdown.js +8 -20971
- package/dist/chunks/markdown.js.map +1 -1
- package/dist/chunks/rehype-highlight.js +216 -415
- package/dist/chunks/rehype-highlight.js.map +1 -1
- package/dist/chunks/shared-lowlight.js +3 -13695
- package/dist/chunks/shared-lowlight.js.map +1 -1
- package/dist/index.js +2 -2
- package/dist/markdown/index.d.ts +2 -1
- package/dist/markdown/index.d.ts.map +1 -1
- package/dist/markdown/index.js +1 -1
- package/dist/markdown/markdown.d.ts +4 -30
- package/dist/markdown/markdown.d.ts.map +1 -1
- package/dist/rehype-alert/index.js +73 -1
- package/dist/rehype-alert/index.js.map +1 -0
- package/package.json +8 -13
- package/dist/chunks/rehype-alert.js +0 -74
- package/dist/chunks/rehype-alert.js.map +0 -1
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,29 @@ function color(d) {
|
|
|
327
379
|
* @template {Test} [Check=Test]
|
|
328
380
|
* Test type.
|
|
329
381
|
*/
|
|
330
|
-
/** @type {Readonly<ActionTuple>} */
|
|
331
|
-
var empty = [];
|
|
332
382
|
/**
|
|
333
|
-
*
|
|
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.
|
|
334
391
|
*/
|
|
335
|
-
|
|
392
|
+
/** @type {Readonly<ActionTuple>} */
|
|
393
|
+
var empty = [];
|
|
336
394
|
/**
|
|
337
395
|
* Visit nodes, with ancestral information.
|
|
338
396
|
*
|
|
339
397
|
* This algorithm performs *depth-first* *tree traversal* in *preorder*
|
|
340
|
-
* (**NLR**) or if `reverse` is given,
|
|
398
|
+
* (**NLR**) or if `reverse` is given,
|
|
399
|
+
* in *reverse preorder* (**NRL**).
|
|
400
|
+
* An `exit` visitor is called in *postorder* (**LRN**)
|
|
401
|
+
* or if `reverse` is given, in *reverse postorder* (**RLN**).
|
|
341
402
|
*
|
|
342
|
-
* You can choose
|
|
343
|
-
* For complex tests, you should test yourself in
|
|
403
|
+
* You can choose which nodes are visited by passing a `test`.
|
|
404
|
+
* For complex tests, you should test yourself in a visitor, as it will be
|
|
344
405
|
* faster and will have improved type information.
|
|
345
406
|
*
|
|
346
407
|
* Walking the tree is an intensive task.
|
|
@@ -349,29 +410,72 @@ var SKIP = "skip";
|
|
|
349
410
|
* to check if a node matches, and then perform different operations.
|
|
350
411
|
*
|
|
351
412
|
* You can change the tree.
|
|
352
|
-
*
|
|
413
|
+
* Changes inside a node made when entering it are walked,
|
|
414
|
+
* changes made when exiting it are not.
|
|
415
|
+
* See `Visitor` and `VisitorExit` for more info.
|
|
416
|
+
*
|
|
417
|
+
* @overload
|
|
418
|
+
* @param {Tree} tree
|
|
419
|
+
* Tree to traverse.
|
|
420
|
+
* @param {Check} check
|
|
421
|
+
* `unist-util-is`-compatible test.
|
|
422
|
+
* @param {BuildVisitor<Tree, Check>} enter
|
|
423
|
+
* Handle each node when entering it.
|
|
424
|
+
* @param {boolean | null | undefined} [reverse]
|
|
425
|
+
* Walk children from last to first;
|
|
426
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
427
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
428
|
+
* @returns {undefined}
|
|
429
|
+
* Nothing.
|
|
353
430
|
*
|
|
354
431
|
* @overload
|
|
355
432
|
* @param {Tree} tree
|
|
433
|
+
* Tree to traverse.
|
|
356
434
|
* @param {Check} check
|
|
357
|
-
*
|
|
435
|
+
* `unist-util-is`-compatible test.
|
|
436
|
+
* @param {BuildVisitors<Tree, Check>} visitors
|
|
437
|
+
* Handle each node when exiting it, and optionally when entering it.
|
|
438
|
+
* @param {boolean | null | undefined} [reverse]
|
|
439
|
+
* Walk children from last to first;
|
|
440
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
441
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
442
|
+
* @returns {undefined}
|
|
443
|
+
* Nothing.
|
|
444
|
+
*
|
|
445
|
+
* @overload
|
|
446
|
+
* @param {Tree} tree
|
|
447
|
+
* Tree to traverse.
|
|
448
|
+
* @param {BuildVisitor<Tree>} enter
|
|
449
|
+
* Handle each node when entering it.
|
|
358
450
|
* @param {boolean | null | undefined} [reverse]
|
|
451
|
+
* Walk children from last to first;
|
|
452
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
453
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
359
454
|
* @returns {undefined}
|
|
455
|
+
* Nothing.
|
|
360
456
|
*
|
|
361
457
|
* @overload
|
|
362
458
|
* @param {Tree} tree
|
|
363
|
-
*
|
|
459
|
+
* Tree to traverse.
|
|
460
|
+
* @param {BuildVisitors<Tree>} visitors
|
|
461
|
+
* Handle each node when exiting it, and optionally when entering it.
|
|
364
462
|
* @param {boolean | null | undefined} [reverse]
|
|
463
|
+
* Walk children from last to first;
|
|
464
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
465
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
365
466
|
* @returns {undefined}
|
|
467
|
+
* Nothing.
|
|
366
468
|
*
|
|
367
469
|
* @param {UnistNode} tree
|
|
368
470
|
* Tree to traverse.
|
|
369
|
-
* @param {Visitor | Test}
|
|
370
|
-
* `unist-util-is`-compatible test
|
|
371
|
-
* @param {Visitor | boolean | null | undefined} [
|
|
372
|
-
*
|
|
471
|
+
* @param {Visitor | Visitors | Test} testOrEnterOrVisitors
|
|
472
|
+
* `unist-util-is`-compatible test, `enter`, or `visitors`.
|
|
473
|
+
* @param {Visitor | Visitors | boolean | null | undefined} [enterOrVisitorsOrReverse]
|
|
474
|
+
* `enter`, `visitors`, or `reverse` after a test.
|
|
373
475
|
* @param {boolean | null | undefined} [reverse]
|
|
374
|
-
*
|
|
476
|
+
* Walk children from last to first;
|
|
477
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
478
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
375
479
|
* @returns {undefined}
|
|
376
480
|
* Nothing.
|
|
377
481
|
*
|
|
@@ -380,20 +484,46 @@ var SKIP = "skip";
|
|
|
380
484
|
* @template {Test} Check
|
|
381
485
|
* `unist-util-is`-compatible test.
|
|
382
486
|
*/
|
|
383
|
-
function visitParents(tree,
|
|
487
|
+
function visitParents(tree, testOrEnterOrVisitors, enterOrVisitorsOrReverse, reverse) {
|
|
488
|
+
if (!tree || typeof tree !== "object") throw new Error("Unexpected value `" + tree + "` for `tree`, expected node");
|
|
489
|
+
let preorder = true;
|
|
384
490
|
/** @type {Test} */
|
|
385
491
|
let check;
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
492
|
+
/** @type {Visitors | Visitor} */
|
|
493
|
+
let visitors;
|
|
494
|
+
/** @type {Visitor | null | undefined} */
|
|
495
|
+
let enter;
|
|
496
|
+
/** @type {VisitorExit | null | undefined} */
|
|
497
|
+
let exit;
|
|
498
|
+
if (typeof enterOrVisitorsOrReverse === "function" || enterOrVisitorsOrReverse && typeof enterOrVisitorsOrReverse === "object") {
|
|
499
|
+
check = testOrEnterOrVisitors;
|
|
500
|
+
visitors = enterOrVisitorsOrReverse;
|
|
501
|
+
if (reverse) preorder = false;
|
|
502
|
+
} else {
|
|
503
|
+
visitors = testOrEnterOrVisitors;
|
|
504
|
+
if (enterOrVisitorsOrReverse) preorder = false;
|
|
505
|
+
}
|
|
506
|
+
if (typeof visitors === "function") enter = visitors;
|
|
507
|
+
else if (visitors && typeof visitors === "object") {
|
|
508
|
+
if (visitors.enter === null || visitors.enter === void 0) {} else if (typeof visitors.enter === "function") enter = visitors.enter;
|
|
509
|
+
else throw new Error("Unexpected value `" + visitors.enter + "` for `visitors.enter`, expected optional visitor");
|
|
510
|
+
if (typeof visitors.exit === "function") exit = visitors.exit;
|
|
511
|
+
else throw new Error("Unexpected value `" + visitors.exit + "` for `visitors.exit`, expected visitor (pass a visitor directly if you only want an enter visitor)");
|
|
512
|
+
} else throw new Error("Unexpected value `" + visitors + "` for `enter` or `visitors`, expected visitor or object with visitors");
|
|
390
513
|
const is = convert(check);
|
|
391
|
-
const step =
|
|
514
|
+
const step = preorder ? 1 : -1;
|
|
392
515
|
factory(tree, void 0, [])();
|
|
393
516
|
/**
|
|
517
|
+
* Create a function to visit a node.
|
|
518
|
+
*
|
|
394
519
|
* @param {UnistNode} node
|
|
520
|
+
* Node to visit.
|
|
395
521
|
* @param {number | undefined} index
|
|
522
|
+
* Index of `node` in its parent.
|
|
396
523
|
* @param {Array<UnistParent>} parents
|
|
524
|
+
* Ancestors of `node`.
|
|
525
|
+
* @returns {() => Readonly<ActionTuple>}
|
|
526
|
+
* Visit `node`.
|
|
397
527
|
*/
|
|
398
528
|
function factory(node, index, parents) {
|
|
399
529
|
const value = node && typeof node === "object" ? node : {};
|
|
@@ -403,31 +533,31 @@ function visitParents(tree, test, visitor, reverse) {
|
|
|
403
533
|
}
|
|
404
534
|
return visit;
|
|
405
535
|
function visit() {
|
|
536
|
+
const matches = !check || is(node, index, parents[parents.length - 1] || void 0);
|
|
406
537
|
/** @type {Readonly<ActionTuple>} */
|
|
407
538
|
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));
|
|
539
|
+
if (enter && matches) {
|
|
540
|
+
result = toResult(enter(node, parents, index));
|
|
416
541
|
if (result[0] === false) return result;
|
|
417
542
|
}
|
|
418
543
|
if ("children" in node && node.children) {
|
|
419
544
|
const nodeAsParent = node;
|
|
420
545
|
if (nodeAsParent.children && result[0] !== "skip") {
|
|
421
|
-
offset = (
|
|
422
|
-
grandparents = parents
|
|
546
|
+
let offset = (preorder ? -1 : nodeAsParent.children.length) + step;
|
|
547
|
+
const grandparents = [...parents, nodeAsParent];
|
|
423
548
|
while (offset > -1 && offset < nodeAsParent.children.length) {
|
|
424
549
|
const child = nodeAsParent.children[offset];
|
|
425
|
-
subresult = factory(child, offset, grandparents)();
|
|
550
|
+
const subresult = factory(child, offset, grandparents)();
|
|
426
551
|
if (subresult[0] === false) return subresult;
|
|
427
552
|
offset = typeof subresult[1] === "number" ? subresult[1] : offset + step;
|
|
428
553
|
}
|
|
429
554
|
}
|
|
430
555
|
}
|
|
556
|
+
if (exit && matches) {
|
|
557
|
+
const exitResult = toResult(exit(node, parents, index));
|
|
558
|
+
if (exitResult[0] === "skip") throw new Error("Unexpected value `" + exitResult[0] + "` from `exit`, expected `CONTINUE`, `EXIT`, index, or nothing");
|
|
559
|
+
if (exitResult[0] === false || typeof exitResult[1] === "number") return exitResult;
|
|
560
|
+
}
|
|
431
561
|
return result;
|
|
432
562
|
}
|
|
433
563
|
}
|
|
@@ -445,11 +575,16 @@ function toResult(value) {
|
|
|
445
575
|
if (typeof value === "number") return [true, value];
|
|
446
576
|
return value === null || value === void 0 ? empty : [value];
|
|
447
577
|
}
|
|
448
|
-
|
|
449
|
-
//#region ../../node_modules/.pnpm/unist-util-visit@5.1.0/node_modules/unist-util-visit/lib/index.js
|
|
578
|
+
// ../../node_modules/.pnpm/unist-util-visit@5.2.0/node_modules/unist-util-visit/lib/index.js
|
|
450
579
|
/**
|
|
451
580
|
* @import {Node as UnistNode, Parent as UnistParent} from 'unist'
|
|
452
|
-
* @import {
|
|
581
|
+
* @import {
|
|
582
|
+
* InclusiveDescendant,
|
|
583
|
+
* Matches,
|
|
584
|
+
* Visitors as ParentsVisitors,
|
|
585
|
+
* VisitorResult,
|
|
586
|
+
* VisitorResultExit
|
|
587
|
+
* } from 'unist-util-visit-parents'
|
|
453
588
|
*/
|
|
454
589
|
/**
|
|
455
590
|
* @typedef {Exclude<import('unist-util-is').Test, undefined> | undefined} Test
|
|
@@ -461,60 +596,6 @@ function toResult(value) {
|
|
|
461
596
|
*/
|
|
462
597
|
/**
|
|
463
598
|
* @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
599
|
* Node extends UnistParent
|
|
519
600
|
* ? Node extends {children: Array<infer Children>}
|
|
520
601
|
* ? Child extends Children ? Node : never
|
|
@@ -536,87 +617,62 @@ function toResult(value) {
|
|
|
536
617
|
* Node to search for.
|
|
537
618
|
*/
|
|
538
619
|
/**
|
|
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
620
|
* @callback Visitor
|
|
583
|
-
* Handle a node (matching `test`, if given).
|
|
584
|
-
*
|
|
585
|
-
* Visitors are free to transform `node`.
|
|
586
|
-
* They can also transform `parent`.
|
|
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`.
|
|
621
|
+
* Handle a node (matching `test`, if given) before its children.
|
|
596
622
|
*
|
|
597
|
-
*
|
|
598
|
-
*
|
|
623
|
+
* Like `Visitor` in `unist-util-visit-parents`,
|
|
624
|
+
* but with `index` and `parent` instead of `parents`.
|
|
599
625
|
* @param {Visited} node
|
|
600
626
|
* Found node.
|
|
601
627
|
* @param {Visited extends UnistNode ? number | undefined : never} index
|
|
602
|
-
* Index of `node` in `parent
|
|
628
|
+
* Index of `node` in `parent`,
|
|
629
|
+
* or `undefined` for the tree itself.
|
|
603
630
|
* @param {Ancestor extends UnistParent ? Ancestor | undefined : never} parent
|
|
604
|
-
* Parent of `node
|
|
631
|
+
* Parent of `node`,
|
|
632
|
+
* or `undefined` for the tree itself.
|
|
605
633
|
* @returns {VisitorResult}
|
|
606
634
|
* What to do next.
|
|
635
|
+
* @template {UnistNode} [Visited=UnistNode]
|
|
636
|
+
* Visited node type.
|
|
637
|
+
* @template {UnistParent} [Ancestor=UnistParent]
|
|
638
|
+
* Ancestor type.
|
|
639
|
+
*/
|
|
640
|
+
/**
|
|
641
|
+
* @callback VisitorExit
|
|
642
|
+
* Handle a node (matching `test`, if given) after its children.
|
|
607
643
|
*
|
|
608
|
-
*
|
|
609
|
-
*
|
|
610
|
-
*
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
*
|
|
644
|
+
* Like `VisitorExit` in `unist-util-visit-parents`,
|
|
645
|
+
* but with `index` and `parent` instead of `parents`.
|
|
646
|
+
* @param {Visited} node
|
|
647
|
+
* Found node.
|
|
648
|
+
* @param {Visited extends UnistNode ? number | undefined : never} index
|
|
649
|
+
* Index of `node` in `parent`,
|
|
650
|
+
* or `undefined` for the tree itself.
|
|
651
|
+
* @param {Ancestor extends UnistParent ? Ancestor | undefined : never} parent
|
|
652
|
+
* Parent of `node`,
|
|
653
|
+
* or `undefined` for the tree itself.
|
|
654
|
+
* @returns {VisitorResultExit}
|
|
655
|
+
* What to do next.
|
|
614
656
|
* @template {UnistNode} [Visited=UnistNode]
|
|
615
657
|
* Visited node type.
|
|
616
658
|
* @template {UnistParent} [Ancestor=UnistParent]
|
|
617
659
|
* Ancestor type.
|
|
618
660
|
*/
|
|
619
661
|
/**
|
|
662
|
+
* @template {UnistNode} [Visited=UnistNode]
|
|
663
|
+
* Visited node type.
|
|
664
|
+
* @template {UnistParent} [Ancestor=UnistParent]
|
|
665
|
+
* Ancestor type.
|
|
666
|
+
* @typedef Visitors
|
|
667
|
+
* Handle nodes when entering and exiting them.
|
|
668
|
+
*
|
|
669
|
+
* Like `Visitors` in `unist-util-visit-parents`.
|
|
670
|
+
* @property {Visitor<Visited, Ancestor> | null | undefined} [enter]
|
|
671
|
+
* Handle a node before its children (optional).
|
|
672
|
+
* @property {VisitorExit<Visited, Ancestor>} exit
|
|
673
|
+
* Handle a node after its children.
|
|
674
|
+
*/
|
|
675
|
+
/**
|
|
620
676
|
* @typedef {Visitor<Visited, Parent<Ancestor, Visited>>} BuildVisitorFromMatch
|
|
621
677
|
* Build a typed `Visitor` function from a node and all possible parents.
|
|
622
678
|
*
|
|
@@ -657,13 +713,34 @@ function toResult(value) {
|
|
|
657
713
|
* Test type.
|
|
658
714
|
*/
|
|
659
715
|
/**
|
|
716
|
+
* @typedef {(
|
|
717
|
+
* Visitors<
|
|
718
|
+
* Matches<InclusiveDescendant<Tree>, Check>,
|
|
719
|
+
* Parent<
|
|
720
|
+
* Extract<InclusiveDescendant<Tree>, UnistParent>,
|
|
721
|
+
* Matches<InclusiveDescendant<Tree>, Check>
|
|
722
|
+
* >
|
|
723
|
+
* >
|
|
724
|
+
* )} BuildVisitors
|
|
725
|
+
* Build typed `Visitors` from a tree and a test.
|
|
726
|
+
*
|
|
727
|
+
* It will infer which values are passed as `node` and which as `parent`.
|
|
728
|
+
* @template {UnistNode} [Tree=UnistNode]
|
|
729
|
+
* Node type.
|
|
730
|
+
* @template {Test} [Check=Test]
|
|
731
|
+
* Test type.
|
|
732
|
+
*/
|
|
733
|
+
/**
|
|
660
734
|
* Visit nodes.
|
|
661
735
|
*
|
|
662
736
|
* This algorithm performs *depth-first* *tree traversal* in *preorder*
|
|
663
|
-
* (**NLR**) or if `reverse` is given,
|
|
737
|
+
* (**NLR**) or if `reverse` is given,
|
|
738
|
+
* in *reverse preorder* (**NRL**).
|
|
739
|
+
* An `exit` visitor is called in *postorder* (**LRN**)
|
|
740
|
+
* or if `reverse` is given, in *reverse postorder* (**RLN**).
|
|
664
741
|
*
|
|
665
|
-
* You can choose
|
|
666
|
-
* For complex tests, you should test yourself in
|
|
742
|
+
* You can choose which nodes are visited by passing a `test`.
|
|
743
|
+
* For complex tests, you should test yourself in a visitor, as it will be
|
|
667
744
|
* faster and will have improved type information.
|
|
668
745
|
*
|
|
669
746
|
* Walking the tree is an intensive task.
|
|
@@ -672,29 +749,48 @@ function toResult(value) {
|
|
|
672
749
|
* to check if a node matches, and then perform different operations.
|
|
673
750
|
*
|
|
674
751
|
* You can change the tree.
|
|
675
|
-
*
|
|
752
|
+
* Changes inside a node made when entering it are walked,
|
|
753
|
+
* changes made when exiting it are not.
|
|
754
|
+
* See `Visitor` and `VisitorExit` for more info.
|
|
676
755
|
*
|
|
677
756
|
* @overload
|
|
678
757
|
* @param {Tree} tree
|
|
758
|
+
* Tree to traverse.
|
|
679
759
|
* @param {Check} check
|
|
680
|
-
*
|
|
760
|
+
* `unist-util-is`-compatible test.
|
|
761
|
+
* @param {BuildVisitor<Tree, Check> | BuildVisitors<Tree, Check>} visitors
|
|
762
|
+
* Handle each node when entering it,
|
|
763
|
+
* or `{enter, exit}` to also handle it when exiting it.
|
|
681
764
|
* @param {boolean | null | undefined} [reverse]
|
|
765
|
+
* Walk children from last to first;
|
|
766
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
767
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
682
768
|
* @returns {undefined}
|
|
769
|
+
* Nothing.
|
|
683
770
|
*
|
|
684
771
|
* @overload
|
|
685
772
|
* @param {Tree} tree
|
|
686
|
-
*
|
|
773
|
+
* Tree to traverse.
|
|
774
|
+
* @param {BuildVisitor<Tree> | BuildVisitors<Tree>} visitors
|
|
775
|
+
* Handle each node when entering it,
|
|
776
|
+
* or `{enter, exit}` to also handle it when exiting it.
|
|
687
777
|
* @param {boolean | null | undefined} [reverse]
|
|
778
|
+
* Walk children from last to first;
|
|
779
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
780
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
688
781
|
* @returns {undefined}
|
|
782
|
+
* Nothing.
|
|
689
783
|
*
|
|
690
784
|
* @param {UnistNode} tree
|
|
691
785
|
* 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
|
-
*
|
|
786
|
+
* @param {Visitor | Visitors | Test} testOrEnterOrVisitors
|
|
787
|
+
* `unist-util-is`-compatible test, `enter`, or `visitors`.
|
|
788
|
+
* @param {Visitor | Visitors | boolean | null | undefined} [enterOrVisitorsOrReverse]
|
|
789
|
+
* `enter`, `visitors`, or `reverse` after a test.
|
|
790
|
+
* @param {boolean | null | undefined} [maybeReverse]
|
|
791
|
+
* Walk children from last to first;
|
|
792
|
+
* for `enter`, reverse preorder (NRL) instead of preorder (NLR);
|
|
793
|
+
* for `exit`, reverse postorder (RLN) instead of postorder (LRN).
|
|
698
794
|
* @returns {undefined}
|
|
699
795
|
* Nothing.
|
|
700
796
|
*
|
|
@@ -703,34 +799,67 @@ function toResult(value) {
|
|
|
703
799
|
* @template {Test} Check
|
|
704
800
|
* `unist-util-is`-compatible test.
|
|
705
801
|
*/
|
|
706
|
-
function visit(tree,
|
|
802
|
+
function visit(tree, testOrEnterOrVisitors, enterOrVisitorsOrReverse, maybeReverse) {
|
|
803
|
+
/** @type {Test} */
|
|
804
|
+
let check;
|
|
805
|
+
/** @type {Visitor | Visitors} */
|
|
806
|
+
let visitors;
|
|
707
807
|
/** @type {boolean | null | undefined} */
|
|
708
808
|
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;
|
|
809
|
+
if (typeof enterOrVisitorsOrReverse === "function" || enterOrVisitorsOrReverse && typeof enterOrVisitorsOrReverse === "object") {
|
|
810
|
+
check = testOrEnterOrVisitors;
|
|
811
|
+
visitors = enterOrVisitorsOrReverse;
|
|
720
812
|
reverse = maybeReverse;
|
|
813
|
+
} else {
|
|
814
|
+
visitors = testOrEnterOrVisitors;
|
|
815
|
+
reverse = enterOrVisitorsOrReverse;
|
|
721
816
|
}
|
|
722
|
-
visitParents(tree,
|
|
817
|
+
if (typeof visitors === "function") visitParents(tree, check, wrap(visitors), reverse);
|
|
818
|
+
else visitParents(tree, check, wrapVisitors(visitors), reverse);
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* Turn `visit` visitors into `visitParents` visitors.
|
|
822
|
+
*
|
|
823
|
+
* @param {Visitors} visitors
|
|
824
|
+
* Visitors for `visit`.
|
|
825
|
+
* @returns {ParentsVisitors}
|
|
826
|
+
* Visitors for `visitParents`.
|
|
827
|
+
*/
|
|
828
|
+
function wrapVisitors(visitors) {
|
|
829
|
+
if (!visitors || typeof visitors !== "object") return visitors;
|
|
830
|
+
return {
|
|
831
|
+
enter: typeof visitors.enter === "function" ? wrap(visitors.enter) : visitors.enter,
|
|
832
|
+
exit: typeof visitors.exit === "function" ? wrap(visitors.exit) : visitors.exit
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* Turn a `visit` visitor into a `visitParents` one.
|
|
837
|
+
*
|
|
838
|
+
* @template {VisitorResult} Result
|
|
839
|
+
* Result type.
|
|
840
|
+
* @param {(node: UnistNode, index: number | undefined, parent: UnistParent | undefined) => Result} visitor
|
|
841
|
+
* Visitor for `visit`.
|
|
842
|
+
* @returns {(node: UnistNode, parents: Array<UnistParent>, index: number | undefined) => Result}
|
|
843
|
+
* Visitor for `visitParents`.
|
|
844
|
+
*/
|
|
845
|
+
function wrap(visitor) {
|
|
846
|
+
return overload;
|
|
723
847
|
/**
|
|
848
|
+
* Call `visitor` with `index` and `parent` instead of `parents`.
|
|
849
|
+
*
|
|
724
850
|
* @param {UnistNode} node
|
|
851
|
+
* Visited node.
|
|
725
852
|
* @param {Array<UnistParent>} parents
|
|
853
|
+
* Ancestors of `node`.
|
|
854
|
+
* @param {number | undefined} index
|
|
855
|
+
* Index of `node` in its parent.
|
|
856
|
+
* @returns {Result}
|
|
857
|
+
* What to do next.
|
|
726
858
|
*/
|
|
727
|
-
function overload(node, parents) {
|
|
728
|
-
|
|
729
|
-
const index = parent ? parent.children.indexOf(node) : void 0;
|
|
730
|
-
return visitor(node, index, parent);
|
|
859
|
+
function overload(node, parents, index) {
|
|
860
|
+
return visitor(node, index, parents[parents.length - 1]);
|
|
731
861
|
}
|
|
732
862
|
}
|
|
733
|
-
|
|
734
|
-
export { convert as i, SKIP as n, visitParents as r, visit as t };
|
|
863
|
+
export { visit as t };
|
|
735
864
|
|
|
736
865
|
//# sourceMappingURL=lib.js.map
|