added gitignore & removed node_modules

This commit is contained in:
2026-01-12 10:57:35 -05:00
parent 3669dbfd51
commit ff88990fc7
4007 changed files with 1447 additions and 790893 deletions

View File

@@ -1,6 +0,0 @@
/**
* @param {string} d
* @returns {string}
*/
export function color(d: string): string;
//# sourceMappingURL=color.d.ts.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"color.d.ts","sourceRoot":"","sources":["color.js"],"names":[],"mappings":"AAAA;;;GAGG;AACH,yBAHW,MAAM,GACJ,MAAM,CAIlB"}

View File

@@ -1,7 +0,0 @@
/**
* @param {string} d
* @returns {string}
*/
export function color(d) {
return d
}

View File

@@ -1,6 +0,0 @@
/**
* @param {string} d
* @returns {string}
*/
export function color(d: string): string;
//# sourceMappingURL=color.node.d.ts.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"color.node.d.ts","sourceRoot":"","sources":["color.node.js"],"names":[],"mappings":"AAAA;;;GAGG;AACH,yBAHW,MAAM,GACJ,MAAM,CAIlB"}

View File

@@ -1,7 +0,0 @@
/**
* @param {string} d
* @returns {string}
*/
export function color(d) {
return '\u001B[33m' + d + '\u001B[39m'
}

View File

@@ -1,219 +0,0 @@
/**
* Visit nodes, with ancestral information.
*
* This algorithm performs *depth-first* *tree traversal* in *preorder*
* (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).
*
* You can choose for which nodes `visitor` is called by passing a `test`.
* For complex tests, you should test yourself in `visitor`, as it will be
* faster and will have improved type information.
*
* Walking the tree is an intensive task.
* Make use of the return values of the visitor when possible.
* Instead of walking a tree multiple times, walk it once, use `unist-util-is`
* to check if a node matches, and then perform different operations.
*
* You can change the tree.
* See `Visitor` for more info.
*
* @overload
* @param {Tree} tree
* @param {Check} check
* @param {BuildVisitor<Tree, Check>} visitor
* @param {boolean | null | undefined} [reverse]
* @returns {undefined}
*
* @overload
* @param {Tree} tree
* @param {BuildVisitor<Tree>} visitor
* @param {boolean | null | undefined} [reverse]
* @returns {undefined}
*
* @param {UnistNode} tree
* Tree to traverse.
* @param {Visitor | Test} test
* `unist-util-is`-compatible test
* @param {Visitor | boolean | null | undefined} [visitor]
* Handle each node.
* @param {boolean | null | undefined} [reverse]
* Traverse in reverse preorder (NRL) instead of the default preorder (NLR).
* @returns {undefined}
* Nothing.
*
* @template {UnistNode} Tree
* Node type.
* @template {Test} Check
* `unist-util-is`-compatible test.
*/
export function visitParents<Tree extends UnistNode, Check extends Test>(tree: Tree, check: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean | null | undefined): undefined;
/**
* Visit nodes, with ancestral information.
*
* This algorithm performs *depth-first* *tree traversal* in *preorder*
* (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).
*
* You can choose for which nodes `visitor` is called by passing a `test`.
* For complex tests, you should test yourself in `visitor`, as it will be
* faster and will have improved type information.
*
* Walking the tree is an intensive task.
* Make use of the return values of the visitor when possible.
* Instead of walking a tree multiple times, walk it once, use `unist-util-is`
* to check if a node matches, and then perform different operations.
*
* You can change the tree.
* See `Visitor` for more info.
*
* @overload
* @param {Tree} tree
* @param {Check} check
* @param {BuildVisitor<Tree, Check>} visitor
* @param {boolean | null | undefined} [reverse]
* @returns {undefined}
*
* @overload
* @param {Tree} tree
* @param {BuildVisitor<Tree>} visitor
* @param {boolean | null | undefined} [reverse]
* @returns {undefined}
*
* @param {UnistNode} tree
* Tree to traverse.
* @param {Visitor | Test} test
* `unist-util-is`-compatible test
* @param {Visitor | boolean | null | undefined} [visitor]
* Handle each node.
* @param {boolean | null | undefined} [reverse]
* Traverse in reverse preorder (NRL) instead of the default preorder (NLR).
* @returns {undefined}
* Nothing.
*
* @template {UnistNode} Tree
* Node type.
* @template {Test} Check
* `unist-util-is`-compatible test.
*/
export function visitParents<Tree extends UnistNode, Check extends Test>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean | null | undefined): undefined;
/**
* Continue traversing as normal.
*/
export const CONTINUE: true;
/**
* Stop traversing immediately.
*/
export const EXIT: false;
/**
* Do not traverse this nodes children.
*/
export const SKIP: "skip";
/**
* Test from `unist-util-is`.
*
* Note: we have remove and add `undefined`, because otherwise when generating
* automatic `.d.ts` files, TS tries to flatten paths from a local perspective,
* which doesnt work when publishing on npm.
*/
export type Test = Exclude<import("unist-util-is").Test, undefined> | undefined;
/**
* Get the value of a type guard `Fn`.
*/
export type Predicate<Fn, Fallback> = (Fn extends (value: any) => value is infer Thing ? Thing : Fallback);
/**
* Check whether a node matches a primitive check in the type system.
*/
export type MatchesOne<Value, Check> = (Check extends null | undefined ? Value : Value extends {
type: Check;
} ? Value : Value extends Check ? Value : Check extends Function ? Predicate<Check, Value> extends Value ? Predicate<Check, Value> : never : never);
/**
* Check whether a node matches a check in the type system.
*/
export type Matches<Value, Check> = (Check extends ReadonlyArray<infer T> ? MatchesOne<Value, T> : Check extends Array<infer T> ? MatchesOne<Value, T> : MatchesOne<Value, Check>);
/**
* Number; capped reasonably.
*/
export type Uint = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
/**
* Increment a number in the type system.
*/
export type Increment<I extends Uint = 0> = 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;
/**
* Collect nodes that can be parents of `Child`.
*/
export type InternalParent<Node extends UnistNode, Child extends UnistNode> = (Node extends UnistParent ? Node extends {
children: Array<infer Children>;
} ? Child extends Children ? Node : never : never : never);
/**
* Collect nodes in `Tree` that can be parents of `Child`.
*/
export type Parent<Tree extends UnistNode, Child extends UnistNode> = InternalParent<InclusiveDescendant<Tree>, Child>;
/**
* Collect nodes in `Tree` that can be ancestors of `Child`.
*/
export type InternalAncestor<Node extends UnistNode, Child extends UnistNode, Max extends Uint = 10, Depth extends Uint = 0> = (Depth extends Max ? never : InternalParent<Node, Child> | InternalAncestor<Node, InternalParent<Node, Child>, Max, Increment<Depth>>);
/**
* Collect nodes in `Tree` that can be ancestors of `Child`.
*/
export type Ancestor<Tree extends UnistNode, Child extends UnistNode> = InternalAncestor<InclusiveDescendant<Tree>, Child>;
/**
* Collect all (inclusive) descendants of `Tree`.
*
* > 👉 **Note**: for performance reasons, this seems to be the fastest way to
* > recurse without actually running into an infinite loop, which the
* > previous version did.
* >
* > Practically, a max of `2` is typically enough assuming a `Root` is
* > passed, but it doesnt improve performance.
* > It gets higher with `List > ListItem > Table > TableRow > TableCell`.
* > Using up to `10` doesnt hurt or help either.
*/
export type InclusiveDescendant<Tree extends UnistNode, Max extends Uint = 10, Depth extends Uint = 0> = (Tree extends UnistParent ? Depth extends Max ? Tree : Tree | InclusiveDescendant<Tree["children"][number], Max, Increment<Depth>> : Tree);
/**
* Union of the action types.
*/
export type Action = "skip" | boolean;
/**
* Move to the sibling at `index` next (after node itself is completely
* traversed).
*
* Useful if mutating the tree, such as removing the node the visitor is
* currently on, or any of its previous siblings.
* Results less than 0 or greater than or equal to `children.length` stop
* traversing the parent.
*/
export type Index = number;
/**
* List with one or two values, the first an action, the second an index.
*/
export type ActionTuple = [(Action | null | undefined | void)?, (Index | null | undefined)?];
/**
* Any value that can be returned from a visitor.
*/
export type VisitorResult = Action | ActionTuple | Index | null | undefined | void;
/**
* Handle a node (matching `test`, if given).
*
* Visitors are free to transform `node`.
* They can also transform the parent of node (the last of `ancestors`).
*
* Replacing `node` itself, if `SKIP` is not returned, still causes its
* descendants to be walked (which is a bug).
*
* When adding or removing previous siblings of `node` (or next siblings, in
* case of reverse), the `Visitor` should return a new `Index` to specify the
* sibling to traverse after `node` is traversed.
* Adding or removing next siblings of `node` (or previous siblings, in case
* of reverse) is handled as expected without needing to return a new `Index`.
*
* Removing the children property of an ancestor still results in them being
* traversed.
*/
export type Visitor<Visited extends UnistNode = UnistNode, VisitedParents extends UnistParent = UnistParent> = (node: Visited, ancestors: Array<VisitedParents>) => VisitorResult;
/**
* Build a typed `Visitor` function from a tree and a test.
*
* It will infer which values are passed as `node` and which as `parents`.
*/
export type BuildVisitor<Tree extends UnistNode = UnistNode, Check extends Test = Test> = Visitor<Matches<InclusiveDescendant<Tree>, Check>, Ancestor<Tree, Matches<InclusiveDescendant<Tree>, Check>>>;
import type { Node as UnistNode } from 'unist';
import type { Parent as UnistParent } from 'unist';
//# sourceMappingURL=index.d.ts.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmQG,6BAwBsB,IAAI,SAAhB,SAAW,EAEJ,KAAK,SAAX,IAAK,QAzBR,IAAI,SACJ,KAAK,WACL,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,YACzB,OAAO,GAAG,IAAI,GAAG,SAAS,GACxB,SAAS,CAEnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6BAiBsB,IAAI,SAAhB,SAAW,EAEJ,KAAK,SAAX,IAAK,QAlBR,IAAI,WACJ,YAAY,CAAC,IAAI,CAAC,YAClB,OAAO,GAAG,IAAI,GAAG,SAAS,GACxB,SAAS,CAEnB;AA9CH;;GAEG;AACH,uBAAwB,IAAI,CAAA;AAE5B;;GAEG;AACH,mBAAoB,KAAK,CAAA;AAEzB;;GAEG;AACH,mBAAoB,MAAM,CAAA;;;;;;;;mBA1Ob,OAAO,CAAC,OAAO,eAAe,EAAE,IAAI,EAAE,SAAS,CAAC,GAAG,SAAS;;;;sBAe5D,EAAE,EAEF,QAAQ,IARR,CACR,EAAE,SAAS,CAAC,KAAK,EAAE,GAAG,KAAK,KAAK,IAAI,MAAM,KAAK,GAC7C,KAAK,GACL,QAAQ,CACX;;;;uBAuBS,KAAK,EAEL,KAAK,IAhBL,CACR,KAAK,SAAS,IAAI,GAAG,SAAS,GAC5B,KAAK,GACL,KAAK,SAAS;IAAC,IAAI,EAAE,KAAK,CAAA;CAAC,GAC3B,KAAK,GACL,KAAK,SAAS,KAAK,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IAAK,WANN,OAAO,CAAC,OAAO,CAAC,mBAAmB,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,EAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,mBAAmB,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;uCAjN/D,OAAO;2CAAP,OAAO"}

View File

@@ -1,399 +0,0 @@
/**
* @import {Node as UnistNode, Parent as UnistParent} from 'unist'
*/
/**
* @typedef {Exclude<import('unist-util-is').Test, undefined> | undefined} Test
* Test from `unist-util-is`.
*
* Note: we have remove and add `undefined`, because otherwise when generating
* automatic `.d.ts` files, TS tries to flatten paths from a local perspective,
* which doesnt work when publishing on npm.
*/
/**
* @typedef {(
* Fn extends (value: any) => value is infer Thing
* ? Thing
* : Fallback
* )} Predicate
* Get the value of a type guard `Fn`.
* @template Fn
* Value; typically function that is a type guard (such as `(x): x is Y`).
* @template Fallback
* Value to yield if `Fn` is not a type guard.
*/
/**
* @typedef {(
* Check extends null | undefined // No test.
* ? Value
* : Value extends {type: Check} // String (type) test.
* ? Value
* : Value extends Check // Partial test.
* ? Value
* : Check extends Function // Function test.
* ? Predicate<Check, Value> extends Value
* ? Predicate<Check, Value>
* : never
* : never // Some other test?
* )} MatchesOne
* Check whether a node matches a primitive check in the type system.
* @template Value
* Value; typically unist `Node`.
* @template Check
* Value; typically `unist-util-is`-compatible test, but not arrays.
*/
/**
* @typedef {(
* Check extends ReadonlyArray<infer T>
* ? MatchesOne<Value, T>
* : Check extends Array<infer T>
* ? MatchesOne<Value, T>
* : MatchesOne<Value, Check>
* )} Matches
* Check whether a node matches a check in the type system.
* @template Value
* Value; typically unist `Node`.
* @template Check
* Value; typically `unist-util-is`-compatible test.
*/
/**
* @typedef {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10} Uint
* Number; capped reasonably.
*/
/**
* @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
* Increment a number in the type system.
* @template {Uint} [I=0]
* Index.
*/
/**
* @typedef {(
* Node extends UnistParent
* ? Node extends {children: Array<infer Children>}
* ? Child extends Children ? Node : never
* : never
* : never
* )} InternalParent
* Collect nodes that can be parents of `Child`.
* @template {UnistNode} Node
* All node types in a tree.
* @template {UnistNode} Child
* Node to search for.
*/
/**
* @typedef {InternalParent<InclusiveDescendant<Tree>, Child>} Parent
* Collect nodes in `Tree` that can be parents of `Child`.
* @template {UnistNode} Tree
* All node types in a tree.
* @template {UnistNode} Child
* Node to search for.
*/
/**
* @typedef {(
* Depth extends Max
* ? never
* :
* | InternalParent<Node, Child>
* | InternalAncestor<Node, InternalParent<Node, Child>, Max, Increment<Depth>>
* )} InternalAncestor
* Collect nodes in `Tree` that can be ancestors of `Child`.
* @template {UnistNode} Node
* All node types in a tree.
* @template {UnistNode} Child
* Node to search for.
* @template {Uint} [Max=10]
* Max; searches up to this depth.
* @template {Uint} [Depth=0]
* Current depth.
*/
/**
* @typedef {InternalAncestor<InclusiveDescendant<Tree>, Child>} Ancestor
* Collect nodes in `Tree` that can be ancestors of `Child`.
* @template {UnistNode} Tree
* All node types in a tree.
* @template {UnistNode} Child
* Node to search for.
*/
/**
* @typedef {(
* Tree extends UnistParent
* ? Depth extends Max
* ? Tree
* : Tree | InclusiveDescendant<Tree['children'][number], Max, Increment<Depth>>
* : Tree
* )} InclusiveDescendant
* Collect all (inclusive) descendants of `Tree`.
*
* > 👉 **Note**: for performance reasons, this seems to be the fastest way to
* > recurse without actually running into an infinite loop, which the
* > previous version did.
* >
* > Practically, a max of `2` is typically enough assuming a `Root` is
* > passed, but it doesnt improve performance.
* > It gets higher with `List > ListItem > Table > TableRow > TableCell`.
* > Using up to `10` doesnt hurt or help either.
* @template {UnistNode} Tree
* Tree type.
* @template {Uint} [Max=10]
* Max; searches up to this depth.
* @template {Uint} [Depth=0]
* Current depth.
*/
/**
* @typedef {'skip' | boolean} Action
* Union of the action types.
*
* @typedef {number} Index
* Move to the sibling at `index` next (after node itself is completely
* traversed).
*
* Useful if mutating the tree, such as removing the node the visitor is
* currently on, or any of its previous siblings.
* Results less than 0 or greater than or equal to `children.length` stop
* traversing the parent.
*
* @typedef {[(Action | null | undefined | void)?, (Index | null | undefined)?]} ActionTuple
* List with one or two values, the first an action, the second an index.
*
* @typedef {Action | ActionTuple | Index | null | undefined | void} VisitorResult
* Any value that can be returned from a visitor.
*/
/**
* @callback Visitor
* Handle a node (matching `test`, if given).
*
* Visitors are free to transform `node`.
* They can also transform the parent of node (the last of `ancestors`).
*
* Replacing `node` itself, if `SKIP` is not returned, still causes its
* descendants to be walked (which is a bug).
*
* When adding or removing previous siblings of `node` (or next siblings, in
* case of reverse), the `Visitor` should return a new `Index` to specify the
* sibling to traverse after `node` is traversed.
* Adding or removing next siblings of `node` (or previous siblings, in case
* of reverse) is handled as expected without needing to return a new `Index`.
*
* Removing the children property of an ancestor still results in them being
* traversed.
* @param {Visited} node
* Found node.
* @param {Array<VisitedParents>} ancestors
* Ancestors of `node`.
* @returns {VisitorResult}
* What to do next.
*
* An `Index` is treated as a tuple of `[CONTINUE, Index]`.
* An `Action` is treated as a tuple of `[Action]`.
*
* Passing a tuple back only makes sense if the `Action` is `SKIP`.
* When the `Action` is `EXIT`, that action can be returned.
* When the `Action` is `CONTINUE`, `Index` can be returned.
* @template {UnistNode} [Visited=UnistNode]
* Visited node type.
* @template {UnistParent} [VisitedParents=UnistParent]
* Ancestor type.
*/
/**
* @typedef {Visitor<Matches<InclusiveDescendant<Tree>, Check>, Ancestor<Tree, Matches<InclusiveDescendant<Tree>, Check>>>} BuildVisitor
* Build a typed `Visitor` function from a tree and a test.
*
* It will infer which values are passed as `node` and which as `parents`.
* @template {UnistNode} [Tree=UnistNode]
* Tree type.
* @template {Test} [Check=Test]
* Test type.
*/
import {convert} from 'unist-util-is'
import {color} from 'unist-util-visit-parents/do-not-use-color'
/** @type {Readonly<ActionTuple>} */
const empty = []
/**
* Continue traversing as normal.
*/
export const CONTINUE = true
/**
* Stop traversing immediately.
*/
export const EXIT = false
/**
* Do not traverse this nodes children.
*/
export const SKIP = 'skip'
/**
* Visit nodes, with ancestral information.
*
* This algorithm performs *depth-first* *tree traversal* in *preorder*
* (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).
*
* You can choose for which nodes `visitor` is called by passing a `test`.
* For complex tests, you should test yourself in `visitor`, as it will be
* faster and will have improved type information.
*
* Walking the tree is an intensive task.
* Make use of the return values of the visitor when possible.
* Instead of walking a tree multiple times, walk it once, use `unist-util-is`
* to check if a node matches, and then perform different operations.
*
* You can change the tree.
* See `Visitor` for more info.
*
* @overload
* @param {Tree} tree
* @param {Check} check
* @param {BuildVisitor<Tree, Check>} visitor
* @param {boolean | null | undefined} [reverse]
* @returns {undefined}
*
* @overload
* @param {Tree} tree
* @param {BuildVisitor<Tree>} visitor
* @param {boolean | null | undefined} [reverse]
* @returns {undefined}
*
* @param {UnistNode} tree
* Tree to traverse.
* @param {Visitor | Test} test
* `unist-util-is`-compatible test
* @param {Visitor | boolean | null | undefined} [visitor]
* Handle each node.
* @param {boolean | null | undefined} [reverse]
* Traverse in reverse preorder (NRL) instead of the default preorder (NLR).
* @returns {undefined}
* Nothing.
*
* @template {UnistNode} Tree
* Node type.
* @template {Test} Check
* `unist-util-is`-compatible test.
*/
export function visitParents(tree, test, visitor, reverse) {
/** @type {Test} */
let check
if (typeof test === 'function' && typeof visitor !== 'function') {
reverse = visitor
// @ts-expect-error no visitor given, so `visitor` is test.
visitor = test
} else {
// @ts-expect-error visitor given, so `test` isnt a visitor.
check = test
}
const is = convert(check)
const step = reverse ? -1 : 1
factory(tree, undefined, [])()
/**
* @param {UnistNode} node
* @param {number | undefined} index
* @param {Array<UnistParent>} parents
*/
function factory(node, index, parents) {
const value = /** @type {Record<string, unknown>} */ (
node && typeof node === 'object' ? node : {}
)
if (typeof value.type === 'string') {
const name =
// `hast`
typeof value.tagName === 'string'
? value.tagName
: // `xast`
typeof value.name === 'string'
? value.name
: undefined
Object.defineProperty(visit, 'name', {
value:
'node (' + color(node.type + (name ? '<' + name + '>' : '')) + ')'
})
}
return visit
function visit() {
/** @type {Readonly<ActionTuple>} */
let result = empty
/** @type {Readonly<ActionTuple>} */
let subresult
/** @type {number} */
let offset
/** @type {Array<UnistParent>} */
let grandparents
if (!test || is(node, index, parents[parents.length - 1] || undefined)) {
// @ts-expect-error: `visitor` is now a visitor.
result = toResult(visitor(node, parents))
if (result[0] === EXIT) {
return result
}
}
if ('children' in node && node.children) {
const nodeAsParent = /** @type {UnistParent} */ (node)
if (nodeAsParent.children && result[0] !== SKIP) {
offset = (reverse ? nodeAsParent.children.length : -1) + step
grandparents = parents.concat(nodeAsParent)
while (offset > -1 && offset < nodeAsParent.children.length) {
const child = nodeAsParent.children[offset]
subresult = factory(child, offset, grandparents)()
if (subresult[0] === EXIT) {
return subresult
}
offset =
typeof subresult[1] === 'number' ? subresult[1] : offset + step
}
}
}
return result
}
}
}
/**
* Turn a return value into a clean result.
*
* @param {VisitorResult} value
* Valid return values from visitors.
* @returns {Readonly<ActionTuple>}
* Clean result.
*/
function toResult(value) {
if (Array.isArray(value)) {
return value
}
if (typeof value === 'number') {
return [CONTINUE, value]
}
return value === null || value === undefined ? empty : [value]
}