@scalar/code-highlight
Advanced tools
| import { i as lowlightLanguageMappings, t as rehypeHighlight } from "./rehype-highlight.js"; | ||
| import { n as rehypeStringify, s as rehypeParse, t as unified } from "./lib.js"; | ||
| import { t as visit } from "./lib2.js"; | ||
| //#region src/code/line-numbers.ts | ||
| function isText(element) { | ||
| return element?.type === "text"; | ||
| } | ||
| function isElement(node) { | ||
| return node?.type === "element"; | ||
| } | ||
| function textElement(value) { | ||
| return { | ||
| type: "text", | ||
| value | ||
| }; | ||
| } | ||
| function lineBreak() { | ||
| return { | ||
| type: "text", | ||
| value: "\n" | ||
| }; | ||
| } | ||
| /** | ||
| * Adds lines to code blocks | ||
| */ | ||
| function codeBlockLinesPlugin() { | ||
| return (tree) => { | ||
| visit(tree, "element", (node, _i, parent) => { | ||
| if (parent?.type === "element" && parent.tagName === "pre" && node.tagName === "code") { | ||
| let numLines = 0; | ||
| node.children = addLines(node); | ||
| node.children.forEach((child) => { | ||
| if (child.type === "element" && child.tagName === "span") { | ||
| const lastChild = child.children[child.children.length - 1]; | ||
| if (lastChild && (!isText(lastChild) || isText(lastChild) && !hasLineBreak(lastChild))) { | ||
| child.children.push(lineBreak()); | ||
| numLines++; | ||
| } | ||
| } | ||
| }); | ||
| node.properties.style = [`--line-count: ${numLines};`, `--line-digits: ${numLines.toString().length};`]; | ||
| } | ||
| }); | ||
| }; | ||
| } | ||
| /** | ||
| * Adds lines to a node recursively and returns them | ||
| * | ||
| * @param node - The node to add lines to | ||
| * @param lines - The current lines | ||
| * @param copyParent - Whether to copy the parent node to save the original node styles | ||
| */ | ||
| function addLines(node, lines = [], copyParent) { | ||
| const line = () => lines[lines.length - 1] ?? (lines.push(createLine()) && lines[lines.length - 1] || void 0); | ||
| node.children.forEach((child) => { | ||
| if (isText(child) && hasLineBreak(child)) { | ||
| const split = child.value.split(/\n/); | ||
| split.forEach((content, i) => { | ||
| if (copyParent) line()?.children.push({ | ||
| ...node, | ||
| children: [textElement(content)] | ||
| }); | ||
| else line()?.children.push(textElement(content)); | ||
| i !== split.length - 1 && lines.push(createLine()); | ||
| }); | ||
| } else if (isElement(child) && child.children.some(hasLineBreak)) addLines(child, lines, true); | ||
| else line()?.children.push(child); | ||
| }); | ||
| return lines; | ||
| } | ||
| /** | ||
| * Creates a new line element | ||
| * | ||
| * @param children - The children the line should have initially | ||
| */ | ||
| function createLine(...children) { | ||
| return { | ||
| type: "element", | ||
| tagName: "span", | ||
| properties: { class: ["line"] }, | ||
| children | ||
| }; | ||
| } | ||
| /** | ||
| * Checks if a node has a line break | ||
| * | ||
| * @param node - The node to check | ||
| */ | ||
| function hasLineBreak(node) { | ||
| return isText(node) && /\r?\n/.test(node.value) || isElement(node) && node.children.some(hasLineBreak); | ||
| } | ||
| //#endregion | ||
| //#region src/code/highlight.ts | ||
| /** | ||
| * Syntax highlights a code string using the `rehype-highlight` library. | ||
| */ | ||
| function syntaxHighlight(codeString, options) { | ||
| const credentials = (typeof options?.maskCredentials === "string" ? [options.maskCredentials] : options?.maskCredentials ?? []).filter((c) => { | ||
| if (c.length < 3) return false; | ||
| return true; | ||
| }); | ||
| const className = `language-${lowlightLanguageMappings[options.lang] ?? options.lang}`; | ||
| const nullPlugin = (() => {}); | ||
| const htmlString = unified().use(rehypeParse, { fragment: true }).use(injectRawCodeStringPlugin(codeString)).use(rehypeHighlight, { languages: options.languages }).use(options?.lineNumbers ? codeBlockLinesPlugin : nullPlugin).use(rehypeStringify).processSync(`<pre><code class="${className}"></code></pre>`).toString(); | ||
| return credentials.length ? credentials.reduce((acc, credential) => acc.split(credential).join(`<span class="credential"><span class="credential-value">${credential}</span></span>`), htmlString) : htmlString; | ||
| } | ||
| /** | ||
| * To prevent unified from parsing any content of the code string we inject | ||
| * it as a raw text node into the AST tree as a child of the code element | ||
| */ | ||
| function injectRawCodeStringPlugin(rawCodeString) { | ||
| return () => (tree) => { | ||
| visit(tree, "element", (node) => { | ||
| if (node.tagName === "code") node.children.push({ | ||
| type: "text", | ||
| value: rawCodeString | ||
| }); | ||
| }); | ||
| }; | ||
| } | ||
| //#endregion | ||
| export { syntaxHighlight as t }; | ||
| //# sourceMappingURL=code.js.map |
| {"version":3,"file":"code.js","names":[],"sources":["../../src/code/line-numbers.ts","../../src/code/highlight.ts"],"sourcesContent":["import type { Element, ElementContent, Root, Text } from 'hast'\nimport { visit } from 'unist-util-visit'\n\n// ---------------------------------------------------------------------------\n// Line Numbering plugin\n\nfunction isText(element?: ElementContent): element is Text {\n return element?.type === 'text'\n}\n\nfunction isElement(node?: ElementContent): node is Element {\n return node?.type === 'element'\n}\n\nfunction textElement(value: string): Text {\n return { type: 'text', value }\n}\n\nfunction lineBreak(): Text {\n return { type: 'text', value: '\\n' }\n}\n\n/**\n * Adds lines to code blocks\n */\nexport function codeBlockLinesPlugin() {\n return (tree: Root) => {\n visit(tree, 'element', (node: Element, _i, parent: Root | Element | null) => {\n if (parent?.type === 'element' && parent.tagName === 'pre' && node.tagName === 'code') {\n let numLines = 0\n\n // Wraps each line in a span\n node.children = addLines(node)\n\n // Adds a line break to the end of each line\n node.children.forEach((child: ElementContent) => {\n if (child.type === 'element' && child.tagName === 'span') {\n const lastChild: ElementContent | undefined = child.children[child.children.length - 1]\n\n if (lastChild && (!isText(lastChild) || (isText(lastChild) && !hasLineBreak(lastChild)))) {\n child.children.push(lineBreak())\n numLines++\n }\n }\n })\n\n // We need to maintain a count of the total lines to allow space for the labels\n node.properties.style = [`--line-count: ${numLines};`, `--line-digits: ${numLines.toString().length};`]\n }\n })\n\n // console.log('NUMBER OF LINES IS: ', numLines)\n }\n}\n\n/**\n * Adds lines to a node recursively and returns them\n *\n * @param node - The node to add lines to\n * @param lines - The current lines\n * @param copyParent - Whether to copy the parent node to save the original node styles\n */\nfunction addLines(node: Element, lines: Element[] = [], copyParent?: boolean): Element[] {\n const line = () => lines[lines.length - 1] ?? ((lines.push(createLine()) && lines[lines.length - 1]) || undefined)\n\n node.children.forEach((child: ElementContent) => {\n if (isText(child) && hasLineBreak(child)) {\n const split: string[] = child.value.split(/\\n/)\n\n split.forEach((content: string, i: number) => {\n if (copyParent) {\n line()?.children.push({ ...node, children: [textElement(content)] })\n } else {\n line()?.children.push(textElement(content))\n }\n\n i !== split.length - 1 && lines.push(createLine())\n })\n } else if (isElement(child) && child.children.some(hasLineBreak)) {\n addLines(child, lines, true)\n } else {\n line()?.children.push(child)\n }\n })\n\n return lines\n}\n\n/**\n * Creates a new line element\n *\n * @param children - The children the line should have initially\n */\nfunction createLine(...children: ElementContent[]): Element {\n return {\n type: 'element',\n tagName: 'span',\n properties: { class: ['line'] },\n children,\n }\n}\n\n/**\n * Checks if a node has a line break\n *\n * @param node - The node to check\n */\nfunction hasLineBreak(node: ElementContent): boolean {\n return (isText(node) && /\\r?\\n/.test(node.value)) || (isElement(node) && node.children.some(hasLineBreak))\n}\n","import type { Element, Root } from 'hast'\nimport type { LanguageFn } from 'highlight.js'\nimport rehypeParse from 'rehype-parse'\nimport rehypeStringify from 'rehype-stringify'\nimport { type Plugin, unified } from 'unified'\nimport { visit } from 'unist-util-visit'\n\nimport { lowlightLanguageMappings } from '@/constants'\nimport { rehypeHighlight } from '@/rehype-highlight'\n\nimport { codeBlockLinesPlugin } from './line-numbers'\n\n/**\n * Syntax highlights a code string using the `rehype-highlight` library.\n */\nexport function syntaxHighlight(\n codeString: string,\n options: {\n lang: string\n languages: Record<string, LanguageFn>\n lineNumbers?: boolean\n maskCredentials?: string | string[]\n },\n) {\n // Simple restriction on credentials to prevent unexpected behavior\n const credentials = (\n typeof options?.maskCredentials === 'string' ? [options.maskCredentials] : (options?.maskCredentials ?? [])\n ).filter((c) => {\n // Credentials must be at least 3 characters to mask.\n if (c.length < 3) {\n return false\n }\n\n return true\n })\n\n // Classname is used by lowlight to select the language model\n const className = `language-${lowlightLanguageMappings[options.lang] ?? options.lang}`\n\n // biome-ignore lint/suspicious/noEmptyBlockStatements: empty plugin\n const nullPlugin = (() => {}) satisfies Plugin\n\n const html = unified()\n // Parses markdown\n .use(rehypeParse, { fragment: true })\n // Raw code string must be injected after initial hast parsing\n // so that HTML code is not parsed into the hast tree\n .use(injectRawCodeStringPlugin(codeString))\n // Syntax highlighting\n .use(rehypeHighlight, {\n languages: options.languages,\n })\n .use(options?.lineNumbers ? codeBlockLinesPlugin : nullPlugin)\n // Converts the HTML AST to a string\n .use(rehypeStringify)\n // Run the pipeline\n .processSync(`<pre><code class=\"${className}\"></code></pre>`)\n\n const htmlString = html.toString()\n\n // Replace any credentials with a wrapper element\n return credentials.length\n ? credentials.reduce(\n (acc, credential) =>\n acc\n .split(credential)\n .join(`<span class=\"credential\"><span class=\"credential-value\">${credential}</span></span>`),\n htmlString,\n )\n : htmlString\n}\n\n/**\n * To prevent unified from parsing any content of the code string we inject\n * it as a raw text node into the AST tree as a child of the code element\n */\nfunction injectRawCodeStringPlugin(rawCodeString: string) {\n return () => (tree: Root) => {\n visit(tree, 'element', (node: Element) => {\n if (node.tagName === 'code') {\n node.children.push({\n type: 'text',\n value: rawCodeString,\n })\n }\n })\n }\n}\n"],"mappings":"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"} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
| //#region ../../node_modules/.pnpm/unist-util-is@6.0.0/node_modules/unist-util-is/lib/index.js | ||
| /** | ||
| * Generate an assertion from a test. | ||
| * | ||
| * Useful if you’re going to test many nodes, for example when creating a | ||
| * utility where something else passes a compatible test. | ||
| * | ||
| * The created function is a bit faster because it expects valid input only: | ||
| * a `node`, `index`, and `parent`. | ||
| * | ||
| * @param {Test} test | ||
| * * when nullish, checks if `node` is a `Node`. | ||
| * * when `string`, works like passing `(node) => node.type === test`. | ||
| * * when `function` checks if function passed the node is true. | ||
| * * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values. | ||
| * * when `array`, checks if any one of the subtests pass. | ||
| * @returns {Check} | ||
| * An assertion. | ||
| */ | ||
| var convert = (function(test) { | ||
| if (test === null || test === void 0) return ok; | ||
| if (typeof test === "function") return castFactory(test); | ||
| if (typeof test === "object") return Array.isArray(test) ? anyFactory(test) : propsFactory(test); | ||
| if (typeof test === "string") return typeFactory(test); | ||
| throw new Error("Expected function, string, or object as test"); | ||
| }); | ||
| /** | ||
| * @param {Array<Props | TestFunction | string>} tests | ||
| * @returns {Check} | ||
| */ | ||
| function anyFactory(tests) { | ||
| /** @type {Array<Check>} */ | ||
| const checks = []; | ||
| let index = -1; | ||
| while (++index < tests.length) checks[index] = convert(tests[index]); | ||
| return castFactory(any); | ||
| /** | ||
| * @this {unknown} | ||
| * @type {TestFunction} | ||
| */ | ||
| function any(...parameters) { | ||
| let index = -1; | ||
| while (++index < checks.length) if (checks[index].apply(this, parameters)) return true; | ||
| return false; | ||
| } | ||
| } | ||
| /** | ||
| * Turn an object into a test for a node with a certain fields. | ||
| * | ||
| * @param {Props} check | ||
| * @returns {Check} | ||
| */ | ||
| function propsFactory(check) { | ||
| const checkAsRecord = check; | ||
| return castFactory(all); | ||
| /** | ||
| * @param {Node} node | ||
| * @returns {boolean} | ||
| */ | ||
| function all(node) { | ||
| const nodeAsRecord = node; | ||
| /** @type {string} */ | ||
| let key; | ||
| for (key in check) if (nodeAsRecord[key] !== checkAsRecord[key]) return false; | ||
| return true; | ||
| } | ||
| } | ||
| /** | ||
| * Turn a string into a test for a node with a certain type. | ||
| * | ||
| * @param {string} check | ||
| * @returns {Check} | ||
| */ | ||
| function typeFactory(check) { | ||
| return castFactory(type); | ||
| /** | ||
| * @param {Node} node | ||
| */ | ||
| function type(node) { | ||
| return node && node.type === check; | ||
| } | ||
| } | ||
| /** | ||
| * Turn a custom test into a test for a node that passes that test. | ||
| * | ||
| * @param {TestFunction} testFunction | ||
| * @returns {Check} | ||
| */ | ||
| function castFactory(testFunction) { | ||
| return check; | ||
| /** | ||
| * @this {unknown} | ||
| * @type {Check} | ||
| */ | ||
| function check(value, index, parent) { | ||
| return Boolean(looksLikeANode(value) && testFunction.call(this, value, typeof index === "number" ? index : void 0, parent || void 0)); | ||
| } | ||
| } | ||
| function ok() { | ||
| return true; | ||
| } | ||
| /** | ||
| * @param {unknown} value | ||
| * @returns {value is Node} | ||
| */ | ||
| function looksLikeANode(value) { | ||
| return value !== null && typeof value === "object" && "type" in value; | ||
| } | ||
| //#endregion | ||
| //#region ../../node_modules/.pnpm/unist-util-visit-parents@6.0.2/node_modules/unist-util-visit-parents/lib/color.js | ||
| /** | ||
| * @param {string} d | ||
| * @returns {string} | ||
| */ | ||
| function color(d) { | ||
| return d; | ||
| } | ||
| //#endregion | ||
| //#region ../../node_modules/.pnpm/unist-util-visit-parents@6.0.2/node_modules/unist-util-visit-parents/lib/index.js | ||
| /** | ||
| * @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 doesn’t 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 doesn’t improve performance. | ||
| * > It gets higher with `List > ListItem > Table > TableRow > TableCell`. | ||
| * > Using up to `10` doesn’t 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. | ||
| */ | ||
| /** @type {Readonly<ActionTuple>} */ | ||
| var empty = []; | ||
| /** | ||
| * Do not traverse this node’s children. | ||
| */ | ||
| var 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. | ||
| */ | ||
| function visitParents(tree, test, visitor, reverse) { | ||
| /** @type {Test} */ | ||
| let check; | ||
| if (typeof test === "function" && typeof visitor !== "function") { | ||
| reverse = visitor; | ||
| visitor = test; | ||
| } else check = test; | ||
| const is = convert(check); | ||
| const step = reverse ? -1 : 1; | ||
| factory(tree, void 0, [])(); | ||
| /** | ||
| * @param {UnistNode} node | ||
| * @param {number | undefined} index | ||
| * @param {Array<UnistParent>} parents | ||
| */ | ||
| function factory(node, index, parents) { | ||
| const value = node && typeof node === "object" ? node : {}; | ||
| if (typeof value.type === "string") { | ||
| const name = typeof value.tagName === "string" ? value.tagName : typeof value.name === "string" ? value.name : void 0; | ||
| 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] || void 0)) { | ||
| result = toResult(visitor(node, parents)); | ||
| if (result[0] === false) return result; | ||
| } | ||
| if ("children" in node && node.children) { | ||
| const nodeAsParent = 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] === false) 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 [true, value]; | ||
| return value === null || value === void 0 ? empty : [value]; | ||
| } | ||
| //#endregion | ||
| //#region ../../node_modules/.pnpm/unist-util-visit@5.1.0/node_modules/unist-util-visit/lib/index.js | ||
| /** | ||
| * @import {Node as UnistNode, Parent as UnistParent} from 'unist' | ||
| * @import {VisitorResult} from 'unist-util-visit-parents' | ||
| */ | ||
| /** | ||
| * @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 doesn’t 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<any> | ||
| * ? MatchesOne<Value, Check[number]> | ||
| * : 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 {( | ||
| * 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 doesn’t improve performance. | ||
| * > It gets higher with `List > ListItem > Table > TableRow > TableCell`. | ||
| * > Using up to `10` doesn’t 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. | ||
| */ | ||
| /** | ||
| * @callback Visitor | ||
| * Handle a node (matching `test`, if given). | ||
| * | ||
| * Visitors are free to transform `node`. | ||
| * They can also transform `parent`. | ||
| * | ||
| * 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 `parent` still results in them being | ||
| * traversed. | ||
| * @param {Visited} node | ||
| * Found node. | ||
| * @param {Visited extends UnistNode ? number | undefined : never} index | ||
| * Index of `node` in `parent`. | ||
| * @param {Ancestor extends UnistParent ? Ancestor | undefined : never} parent | ||
| * Parent 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} [Ancestor=UnistParent] | ||
| * Ancestor type. | ||
| */ | ||
| /** | ||
| * @typedef {Visitor<Visited, Parent<Ancestor, Visited>>} BuildVisitorFromMatch | ||
| * Build a typed `Visitor` function from a node and all possible parents. | ||
| * | ||
| * It will infer which values are passed as `node` and which as `parent`. | ||
| * @template {UnistNode} Visited | ||
| * Node type. | ||
| * @template {UnistParent} Ancestor | ||
| * Parent type. | ||
| */ | ||
| /** | ||
| * @typedef {( | ||
| * BuildVisitorFromMatch< | ||
| * Matches<Descendant, Check>, | ||
| * Extract<Descendant, UnistParent> | ||
| * > | ||
| * )} BuildVisitorFromDescendants | ||
| * Build a typed `Visitor` function from a list of descendants and a test. | ||
| * | ||
| * It will infer which values are passed as `node` and which as `parent`. | ||
| * @template {UnistNode} Descendant | ||
| * Node type. | ||
| * @template {Test} Check | ||
| * Test type. | ||
| */ | ||
| /** | ||
| * @typedef {( | ||
| * BuildVisitorFromDescendants< | ||
| * 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 `parent`. | ||
| * @template {UnistNode} [Tree=UnistNode] | ||
| * Node type. | ||
| * @template {Test} [Check=Test] | ||
| * Test type. | ||
| */ | ||
| /** | ||
| * Visit nodes. | ||
| * | ||
| * 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} testOrVisitor | ||
| * `unist-util-is`-compatible test (optional, omit to pass a visitor). | ||
| * @param {Visitor | boolean | null | undefined} [visitorOrReverse] | ||
| * Handle each node (when test is omitted, pass `reverse`). | ||
| * @param {boolean | null | undefined} [maybeReverse=false] | ||
| * 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. | ||
| */ | ||
| function visit(tree, testOrVisitor, visitorOrReverse, maybeReverse) { | ||
| /** @type {boolean | null | undefined} */ | ||
| let reverse; | ||
| /** @type {Test} */ | ||
| let test; | ||
| /** @type {Visitor} */ | ||
| let visitor; | ||
| if (typeof testOrVisitor === "function" && typeof visitorOrReverse !== "function") { | ||
| test = void 0; | ||
| visitor = testOrVisitor; | ||
| reverse = visitorOrReverse; | ||
| } else { | ||
| test = testOrVisitor; | ||
| visitor = visitorOrReverse; | ||
| reverse = maybeReverse; | ||
| } | ||
| visitParents(tree, test, overload, reverse); | ||
| /** | ||
| * @param {UnistNode} node | ||
| * @param {Array<UnistParent>} parents | ||
| */ | ||
| function overload(node, parents) { | ||
| const parent = parents[parents.length - 1]; | ||
| const index = parent ? parent.children.indexOf(node) : void 0; | ||
| return visitor(node, index, parent); | ||
| } | ||
| } | ||
| //#endregion | ||
| export { convert as i, SKIP as n, visitParents as r, visit as t }; | ||
| //# sourceMappingURL=lib2.js.map |
| {"version":3,"file":"lib2.js","names":[],"sources":["../../../../node_modules/.pnpm/unist-util-is@6.0.0/node_modules/unist-util-is/lib/index.js","../../../../node_modules/.pnpm/unist-util-visit-parents@6.0.2/node_modules/unist-util-visit-parents/lib/color.js","../../../../node_modules/.pnpm/unist-util-visit-parents@6.0.2/node_modules/unist-util-visit-parents/lib/index.js","../../../../node_modules/.pnpm/unist-util-visit@5.1.0/node_modules/unist-util-visit/lib/index.js"],"sourcesContent":["/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('unist').Parent} Parent\n */\n\n/**\n * @template Fn\n * @template Fallback\n * @typedef {Fn extends (value: any) => value is infer Thing ? Thing : Fallback} Predicate\n */\n\n/**\n * @callback Check\n * Check that an arbitrary value is a node.\n * @param {unknown} this\n * The given context.\n * @param {unknown} [node]\n * Anything (typically a node).\n * @param {number | null | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | null | undefined} [parent]\n * The node’s parent.\n * @returns {boolean}\n * Whether this is a node and passes a test.\n *\n * @typedef {Record<string, unknown> | Node} Props\n * Object to check for equivalence.\n *\n * Note: `Node` is included as it is common but is not indexable.\n *\n * @typedef {Array<Props | TestFunction | string> | Props | TestFunction | string | null | undefined} Test\n * Check for an arbitrary node.\n *\n * @callback TestFunction\n * Check if a node passes a test.\n * @param {unknown} this\n * The given context.\n * @param {Node} node\n * A node.\n * @param {number | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | undefined} [parent]\n * The node’s parent.\n * @returns {boolean | undefined | void}\n * Whether this node passes the test.\n *\n * Note: `void` is included until TS sees no return as `undefined`.\n */\n\n/**\n * Check if `node` is a `Node` and whether it passes the given test.\n *\n * @param {unknown} node\n * Thing to check, typically `Node`.\n * @param {Test} test\n * A check for a specific node.\n * @param {number | null | undefined} index\n * The node’s position in its parent.\n * @param {Parent | null | undefined} parent\n * The node’s parent.\n * @param {unknown} context\n * Context object (`this`) to pass to `test` functions.\n * @returns {boolean}\n * Whether `node` is a node and passes a test.\n */\nexport const is =\n // Note: overloads in JSDoc can’t yet use different `@template`s.\n /**\n * @type {(\n * (<Condition extends string>(node: unknown, test: Condition, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & {type: Condition}) &\n * (<Condition extends Props>(node: unknown, test: Condition, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & Condition) &\n * (<Condition extends TestFunction>(node: unknown, test: Condition, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & Predicate<Condition, Node>) &\n * ((node?: null | undefined) => false) &\n * ((node: unknown, test?: null | undefined, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node) &\n * ((node: unknown, test?: Test, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => boolean)\n * )}\n */\n (\n /**\n * @param {unknown} [node]\n * @param {Test} [test]\n * @param {number | null | undefined} [index]\n * @param {Parent | null | undefined} [parent]\n * @param {unknown} [context]\n * @returns {boolean}\n */\n // eslint-disable-next-line max-params\n function (node, test, index, parent, context) {\n const check = convert(test)\n\n if (\n index !== undefined &&\n index !== null &&\n (typeof index !== 'number' ||\n index < 0 ||\n index === Number.POSITIVE_INFINITY)\n ) {\n throw new Error('Expected positive finite index')\n }\n\n if (\n parent !== undefined &&\n parent !== null &&\n (!is(parent) || !parent.children)\n ) {\n throw new Error('Expected parent node')\n }\n\n if (\n (parent === undefined || parent === null) !==\n (index === undefined || index === null)\n ) {\n throw new Error('Expected both parent and index')\n }\n\n return looksLikeANode(node)\n ? check.call(context, node, index, parent)\n : false\n }\n )\n\n/**\n * Generate an assertion from a test.\n *\n * Useful if you’re going to test many nodes, for example when creating a\n * utility where something else passes a compatible test.\n *\n * The created function is a bit faster because it expects valid input only:\n * a `node`, `index`, and `parent`.\n *\n * @param {Test} test\n * * when nullish, checks if `node` is a `Node`.\n * * when `string`, works like passing `(node) => node.type === test`.\n * * when `function` checks if function passed the node is true.\n * * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values.\n * * when `array`, checks if any one of the subtests pass.\n * @returns {Check}\n * An assertion.\n */\nexport const convert =\n // Note: overloads in JSDoc can’t yet use different `@template`s.\n /**\n * @type {(\n * (<Condition extends string>(test: Condition) => (node: unknown, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & {type: Condition}) &\n * (<Condition extends Props>(test: Condition) => (node: unknown, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & Condition) &\n * (<Condition extends TestFunction>(test: Condition) => (node: unknown, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node & Predicate<Condition, Node>) &\n * ((test?: null | undefined) => (node?: unknown, index?: number | null | undefined, parent?: Parent | null | undefined, context?: unknown) => node is Node) &\n * ((test?: Test) => Check)\n * )}\n */\n (\n /**\n * @param {Test} [test]\n * @returns {Check}\n */\n function (test) {\n if (test === null || test === undefined) {\n return ok\n }\n\n if (typeof test === 'function') {\n return castFactory(test)\n }\n\n if (typeof test === 'object') {\n return Array.isArray(test) ? anyFactory(test) : propsFactory(test)\n }\n\n if (typeof test === 'string') {\n return typeFactory(test)\n }\n\n throw new Error('Expected function, string, or object as test')\n }\n )\n\n/**\n * @param {Array<Props | TestFunction | string>} tests\n * @returns {Check}\n */\nfunction anyFactory(tests) {\n /** @type {Array<Check>} */\n const checks = []\n let index = -1\n\n while (++index < tests.length) {\n checks[index] = convert(tests[index])\n }\n\n return castFactory(any)\n\n /**\n * @this {unknown}\n * @type {TestFunction}\n */\n function any(...parameters) {\n let index = -1\n\n while (++index < checks.length) {\n if (checks[index].apply(this, parameters)) return true\n }\n\n return false\n }\n}\n\n/**\n * Turn an object into a test for a node with a certain fields.\n *\n * @param {Props} check\n * @returns {Check}\n */\nfunction propsFactory(check) {\n const checkAsRecord = /** @type {Record<string, unknown>} */ (check)\n\n return castFactory(all)\n\n /**\n * @param {Node} node\n * @returns {boolean}\n */\n function all(node) {\n const nodeAsRecord = /** @type {Record<string, unknown>} */ (\n /** @type {unknown} */ (node)\n )\n\n /** @type {string} */\n let key\n\n for (key in check) {\n if (nodeAsRecord[key] !== checkAsRecord[key]) return false\n }\n\n return true\n }\n}\n\n/**\n * Turn a string into a test for a node with a certain type.\n *\n * @param {string} check\n * @returns {Check}\n */\nfunction typeFactory(check) {\n return castFactory(type)\n\n /**\n * @param {Node} node\n */\n function type(node) {\n return node && node.type === check\n }\n}\n\n/**\n * Turn a custom test into a test for a node that passes that test.\n *\n * @param {TestFunction} testFunction\n * @returns {Check}\n */\nfunction castFactory(testFunction) {\n return check\n\n /**\n * @this {unknown}\n * @type {Check}\n */\n function check(value, index, parent) {\n return Boolean(\n looksLikeANode(value) &&\n testFunction.call(\n this,\n value,\n typeof index === 'number' ? index : undefined,\n parent || undefined\n )\n )\n }\n}\n\nfunction ok() {\n return true\n}\n\n/**\n * @param {unknown} value\n * @returns {value is Node}\n */\nfunction looksLikeANode(value) {\n return value !== null && typeof value === 'object' && 'type' in value\n}\n","/**\n * @param {string} d\n * @returns {string}\n */\nexport function color(d) {\n return d\n}\n","/**\n * @import {Node as UnistNode, Parent as UnistParent} from 'unist'\n */\n\n/**\n * @typedef {Exclude<import('unist-util-is').Test, undefined> | undefined} Test\n * Test from `unist-util-is`.\n *\n * Note: we have remove and add `undefined`, because otherwise when generating\n * automatic `.d.ts` files, TS tries to flatten paths from a local perspective,\n * which doesn’t work when publishing on npm.\n */\n\n/**\n * @typedef {(\n * Fn extends (value: any) => value is infer Thing\n * ? Thing\n * : Fallback\n * )} Predicate\n * Get the value of a type guard `Fn`.\n * @template Fn\n * Value; typically function that is a type guard (such as `(x): x is Y`).\n * @template Fallback\n * Value to yield if `Fn` is not a type guard.\n */\n\n/**\n * @typedef {(\n * Check extends null | undefined // No test.\n * ? Value\n * : Value extends {type: Check} // String (type) test.\n * ? Value\n * : Value extends Check // Partial test.\n * ? Value\n * : Check extends Function // Function test.\n * ? Predicate<Check, Value> extends Value\n * ? Predicate<Check, Value>\n * : never\n * : never // Some other test?\n * )} MatchesOne\n * Check whether a node matches a primitive check in the type system.\n * @template Value\n * Value; typically unist `Node`.\n * @template Check\n * Value; typically `unist-util-is`-compatible test, but not arrays.\n */\n\n/**\n * @typedef {(\n * Check extends ReadonlyArray<infer T>\n * ? MatchesOne<Value, T>\n * : Check extends Array<infer T>\n * ? MatchesOne<Value, T>\n * : MatchesOne<Value, Check>\n * )} Matches\n * Check whether a node matches a check in the type system.\n * @template Value\n * Value; typically unist `Node`.\n * @template Check\n * Value; typically `unist-util-is`-compatible test.\n */\n\n/**\n * @typedef {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10} Uint\n * Number; capped reasonably.\n */\n\n/**\n * @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\n * Increment a number in the type system.\n * @template {Uint} [I=0]\n * Index.\n */\n\n/**\n * @typedef {(\n * Node extends UnistParent\n * ? Node extends {children: Array<infer Children>}\n * ? Child extends Children ? Node : never\n * : never\n * : never\n * )} InternalParent\n * Collect nodes that can be parents of `Child`.\n * @template {UnistNode} Node\n * All node types in a tree.\n * @template {UnistNode} Child\n * Node to search for.\n */\n\n/**\n * @typedef {InternalParent<InclusiveDescendant<Tree>, Child>} Parent\n * Collect nodes in `Tree` that can be parents of `Child`.\n * @template {UnistNode} Tree\n * All node types in a tree.\n * @template {UnistNode} Child\n * Node to search for.\n */\n\n/**\n * @typedef {(\n * Depth extends Max\n * ? never\n * :\n * | InternalParent<Node, Child>\n * | InternalAncestor<Node, InternalParent<Node, Child>, Max, Increment<Depth>>\n * )} InternalAncestor\n * Collect nodes in `Tree` that can be ancestors of `Child`.\n * @template {UnistNode} Node\n * All node types in a tree.\n * @template {UnistNode} Child\n * Node to search for.\n * @template {Uint} [Max=10]\n * Max; searches up to this depth.\n * @template {Uint} [Depth=0]\n * Current depth.\n */\n\n/**\n * @typedef {InternalAncestor<InclusiveDescendant<Tree>, Child>} Ancestor\n * Collect nodes in `Tree` that can be ancestors of `Child`.\n * @template {UnistNode} Tree\n * All node types in a tree.\n * @template {UnistNode} Child\n * Node to search for.\n */\n\n/**\n * @typedef {(\n * Tree extends UnistParent\n * ? Depth extends Max\n * ? Tree\n * : Tree | InclusiveDescendant<Tree['children'][number], Max, Increment<Depth>>\n * : Tree\n * )} InclusiveDescendant\n * Collect all (inclusive) descendants of `Tree`.\n *\n * > 👉 **Note**: for performance reasons, this seems to be the fastest way to\n * > recurse without actually running into an infinite loop, which the\n * > previous version did.\n * >\n * > Practically, a max of `2` is typically enough assuming a `Root` is\n * > passed, but it doesn’t improve performance.\n * > It gets higher with `List > ListItem > Table > TableRow > TableCell`.\n * > Using up to `10` doesn’t hurt or help either.\n * @template {UnistNode} Tree\n * Tree type.\n * @template {Uint} [Max=10]\n * Max; searches up to this depth.\n * @template {Uint} [Depth=0]\n * Current depth.\n */\n\n/**\n * @typedef {'skip' | boolean} Action\n * Union of the action types.\n *\n * @typedef {number} Index\n * Move to the sibling at `index` next (after node itself is completely\n * traversed).\n *\n * Useful if mutating the tree, such as removing the node the visitor is\n * currently on, or any of its previous siblings.\n * Results less than 0 or greater than or equal to `children.length` stop\n * traversing the parent.\n *\n * @typedef {[(Action | null | undefined | void)?, (Index | null | undefined)?]} ActionTuple\n * List with one or two values, the first an action, the second an index.\n *\n * @typedef {Action | ActionTuple | Index | null | undefined | void} VisitorResult\n * Any value that can be returned from a visitor.\n */\n\n/**\n * @callback Visitor\n * Handle a node (matching `test`, if given).\n *\n * Visitors are free to transform `node`.\n * They can also transform the parent of node (the last of `ancestors`).\n *\n * Replacing `node` itself, if `SKIP` is not returned, still causes its\n * descendants to be walked (which is a bug).\n *\n * When adding or removing previous siblings of `node` (or next siblings, in\n * case of reverse), the `Visitor` should return a new `Index` to specify the\n * sibling to traverse after `node` is traversed.\n * Adding or removing next siblings of `node` (or previous siblings, in case\n * of reverse) is handled as expected without needing to return a new `Index`.\n *\n * Removing the children property of an ancestor still results in them being\n * traversed.\n * @param {Visited} node\n * Found node.\n * @param {Array<VisitedParents>} ancestors\n * Ancestors of `node`.\n * @returns {VisitorResult}\n * What to do next.\n *\n * An `Index` is treated as a tuple of `[CONTINUE, Index]`.\n * An `Action` is treated as a tuple of `[Action]`.\n *\n * Passing a tuple back only makes sense if the `Action` is `SKIP`.\n * When the `Action` is `EXIT`, that action can be returned.\n * When the `Action` is `CONTINUE`, `Index` can be returned.\n * @template {UnistNode} [Visited=UnistNode]\n * Visited node type.\n * @template {UnistParent} [VisitedParents=UnistParent]\n * Ancestor type.\n */\n\n/**\n * @typedef {Visitor<Matches<InclusiveDescendant<Tree>, Check>, Ancestor<Tree, Matches<InclusiveDescendant<Tree>, Check>>>} BuildVisitor\n * Build a typed `Visitor` function from a tree and a test.\n *\n * It will infer which values are passed as `node` and which as `parents`.\n * @template {UnistNode} [Tree=UnistNode]\n * Tree type.\n * @template {Test} [Check=Test]\n * Test type.\n */\n\nimport {convert} from 'unist-util-is'\nimport {color} from 'unist-util-visit-parents/do-not-use-color'\n\n/** @type {Readonly<ActionTuple>} */\nconst empty = []\n\n/**\n * Continue traversing as normal.\n */\nexport const CONTINUE = true\n\n/**\n * Stop traversing immediately.\n */\nexport const EXIT = false\n\n/**\n * Do not traverse this node’s children.\n */\nexport const SKIP = 'skip'\n\n/**\n * Visit nodes, with ancestral information.\n *\n * This algorithm performs *depth-first* *tree traversal* in *preorder*\n * (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).\n *\n * You can choose for which nodes `visitor` is called by passing a `test`.\n * For complex tests, you should test yourself in `visitor`, as it will be\n * faster and will have improved type information.\n *\n * Walking the tree is an intensive task.\n * Make use of the return values of the visitor when possible.\n * Instead of walking a tree multiple times, walk it once, use `unist-util-is`\n * to check if a node matches, and then perform different operations.\n *\n * You can change the tree.\n * See `Visitor` for more info.\n *\n * @overload\n * @param {Tree} tree\n * @param {Check} check\n * @param {BuildVisitor<Tree, Check>} visitor\n * @param {boolean | null | undefined} [reverse]\n * @returns {undefined}\n *\n * @overload\n * @param {Tree} tree\n * @param {BuildVisitor<Tree>} visitor\n * @param {boolean | null | undefined} [reverse]\n * @returns {undefined}\n *\n * @param {UnistNode} tree\n * Tree to traverse.\n * @param {Visitor | Test} test\n * `unist-util-is`-compatible test\n * @param {Visitor | boolean | null | undefined} [visitor]\n * Handle each node.\n * @param {boolean | null | undefined} [reverse]\n * Traverse in reverse preorder (NRL) instead of the default preorder (NLR).\n * @returns {undefined}\n * Nothing.\n *\n * @template {UnistNode} Tree\n * Node type.\n * @template {Test} Check\n * `unist-util-is`-compatible test.\n */\nexport function visitParents(tree, test, visitor, reverse) {\n /** @type {Test} */\n let check\n\n if (typeof test === 'function' && typeof visitor !== 'function') {\n reverse = visitor\n // @ts-expect-error no visitor given, so `visitor` is test.\n visitor = test\n } else {\n // @ts-expect-error visitor given, so `test` isn’t a visitor.\n check = test\n }\n\n const is = convert(check)\n const step = reverse ? -1 : 1\n\n factory(tree, undefined, [])()\n\n /**\n * @param {UnistNode} node\n * @param {number | undefined} index\n * @param {Array<UnistParent>} parents\n */\n function factory(node, index, parents) {\n const value = /** @type {Record<string, unknown>} */ (\n node && typeof node === 'object' ? node : {}\n )\n\n if (typeof value.type === 'string') {\n const name =\n // `hast`\n typeof value.tagName === 'string'\n ? value.tagName\n : // `xast`\n typeof value.name === 'string'\n ? value.name\n : undefined\n\n Object.defineProperty(visit, 'name', {\n value:\n 'node (' + color(node.type + (name ? '<' + name + '>' : '')) + ')'\n })\n }\n\n return visit\n\n function visit() {\n /** @type {Readonly<ActionTuple>} */\n let result = empty\n /** @type {Readonly<ActionTuple>} */\n let subresult\n /** @type {number} */\n let offset\n /** @type {Array<UnistParent>} */\n let grandparents\n\n if (!test || is(node, index, parents[parents.length - 1] || undefined)) {\n // @ts-expect-error: `visitor` is now a visitor.\n result = toResult(visitor(node, parents))\n\n if (result[0] === EXIT) {\n return result\n }\n }\n\n if ('children' in node && node.children) {\n const nodeAsParent = /** @type {UnistParent} */ (node)\n\n if (nodeAsParent.children && result[0] !== SKIP) {\n offset = (reverse ? nodeAsParent.children.length : -1) + step\n grandparents = parents.concat(nodeAsParent)\n\n while (offset > -1 && offset < nodeAsParent.children.length) {\n const child = nodeAsParent.children[offset]\n\n subresult = factory(child, offset, grandparents)()\n\n if (subresult[0] === EXIT) {\n return subresult\n }\n\n offset =\n typeof subresult[1] === 'number' ? subresult[1] : offset + step\n }\n }\n }\n\n return result\n }\n }\n}\n\n/**\n * Turn a return value into a clean result.\n *\n * @param {VisitorResult} value\n * Valid return values from visitors.\n * @returns {Readonly<ActionTuple>}\n * Clean result.\n */\nfunction toResult(value) {\n if (Array.isArray(value)) {\n return value\n }\n\n if (typeof value === 'number') {\n return [CONTINUE, value]\n }\n\n return value === null || value === undefined ? empty : [value]\n}\n","/**\n * @import {Node as UnistNode, Parent as UnistParent} from 'unist'\n * @import {VisitorResult} from 'unist-util-visit-parents'\n */\n\n/**\n * @typedef {Exclude<import('unist-util-is').Test, undefined> | undefined} Test\n * Test from `unist-util-is`.\n *\n * Note: we have remove and add `undefined`, because otherwise when generating\n * automatic `.d.ts` files, TS tries to flatten paths from a local perspective,\n * which doesn’t work when publishing on npm.\n */\n\n// To do: use types from `unist-util-visit-parents` when it’s released.\n\n/**\n * @typedef {(\n * Fn extends (value: any) => value is infer Thing\n * ? Thing\n * : Fallback\n * )} Predicate\n * Get the value of a type guard `Fn`.\n * @template Fn\n * Value; typically function that is a type guard (such as `(x): x is Y`).\n * @template Fallback\n * Value to yield if `Fn` is not a type guard.\n */\n\n/**\n * @typedef {(\n * Check extends null | undefined // No test.\n * ? Value\n * : Value extends {type: Check} // String (type) test.\n * ? Value\n * : Value extends Check // Partial test.\n * ? Value\n * : Check extends Function // Function test.\n * ? Predicate<Check, Value> extends Value\n * ? Predicate<Check, Value>\n * : never\n * : never // Some other test?\n * )} MatchesOne\n * Check whether a node matches a primitive check in the type system.\n * @template Value\n * Value; typically unist `Node`.\n * @template Check\n * Value; typically `unist-util-is`-compatible test, but not arrays.\n */\n\n/**\n * @typedef {(\n * Check extends ReadonlyArray<any>\n * ? MatchesOne<Value, Check[number]>\n * : MatchesOne<Value, Check>\n * )} Matches\n * Check whether a node matches a check in the type system.\n * @template Value\n * Value; typically unist `Node`.\n * @template Check\n * Value; typically `unist-util-is`-compatible test.\n */\n\n/**\n * @typedef {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10} Uint\n * Number; capped reasonably.\n */\n\n/**\n * @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\n * Increment a number in the type system.\n * @template {Uint} [I=0]\n * Index.\n */\n\n/**\n * @typedef {(\n * Node extends UnistParent\n * ? Node extends {children: Array<infer Children>}\n * ? Child extends Children ? Node : never\n * : never\n * : never\n * )} InternalParent\n * Collect nodes that can be parents of `Child`.\n * @template {UnistNode} Node\n * All node types in a tree.\n * @template {UnistNode} Child\n * Node to search for.\n */\n\n/**\n * @typedef {InternalParent<InclusiveDescendant<Tree>, Child>} Parent\n * Collect nodes in `Tree` that can be parents of `Child`.\n * @template {UnistNode} Tree\n * All node types in a tree.\n * @template {UnistNode} Child\n * Node to search for.\n */\n\n/**\n * @typedef {(\n * Depth extends Max\n * ? never\n * :\n * | InternalParent<Node, Child>\n * | InternalAncestor<Node, InternalParent<Node, Child>, Max, Increment<Depth>>\n * )} InternalAncestor\n * Collect nodes in `Tree` that can be ancestors of `Child`.\n * @template {UnistNode} Node\n * All node types in a tree.\n * @template {UnistNode} Child\n * Node to search for.\n * @template {Uint} [Max=10]\n * Max; searches up to this depth.\n * @template {Uint} [Depth=0]\n * Current depth.\n */\n\n/**\n * @typedef {(\n * Tree extends UnistParent\n * ? Depth extends Max\n * ? Tree\n * : Tree | InclusiveDescendant<Tree['children'][number], Max, Increment<Depth>>\n * : Tree\n * )} InclusiveDescendant\n * Collect all (inclusive) descendants of `Tree`.\n *\n * > 👉 **Note**: for performance reasons, this seems to be the fastest way to\n * > recurse without actually running into an infinite loop, which the\n * > previous version did.\n * >\n * > Practically, a max of `2` is typically enough assuming a `Root` is\n * > passed, but it doesn’t improve performance.\n * > It gets higher with `List > ListItem > Table > TableRow > TableCell`.\n * > Using up to `10` doesn’t hurt or help either.\n * @template {UnistNode} Tree\n * Tree type.\n * @template {Uint} [Max=10]\n * Max; searches up to this depth.\n * @template {Uint} [Depth=0]\n * Current depth.\n */\n\n/**\n * @callback Visitor\n * Handle a node (matching `test`, if given).\n *\n * Visitors are free to transform `node`.\n * They can also transform `parent`.\n *\n * Replacing `node` itself, if `SKIP` is not returned, still causes its\n * descendants to be walked (which is a bug).\n *\n * When adding or removing previous siblings of `node` (or next siblings, in\n * case of reverse), the `Visitor` should return a new `Index` to specify the\n * sibling to traverse after `node` is traversed.\n * Adding or removing next siblings of `node` (or previous siblings, in case\n * of reverse) is handled as expected without needing to return a new `Index`.\n *\n * Removing the children property of `parent` still results in them being\n * traversed.\n * @param {Visited} node\n * Found node.\n * @param {Visited extends UnistNode ? number | undefined : never} index\n * Index of `node` in `parent`.\n * @param {Ancestor extends UnistParent ? Ancestor | undefined : never} parent\n * Parent of `node`.\n * @returns {VisitorResult}\n * What to do next.\n *\n * An `Index` is treated as a tuple of `[CONTINUE, Index]`.\n * An `Action` is treated as a tuple of `[Action]`.\n *\n * Passing a tuple back only makes sense if the `Action` is `SKIP`.\n * When the `Action` is `EXIT`, that action can be returned.\n * When the `Action` is `CONTINUE`, `Index` can be returned.\n * @template {UnistNode} [Visited=UnistNode]\n * Visited node type.\n * @template {UnistParent} [Ancestor=UnistParent]\n * Ancestor type.\n */\n\n/**\n * @typedef {Visitor<Visited, Parent<Ancestor, Visited>>} BuildVisitorFromMatch\n * Build a typed `Visitor` function from a node and all possible parents.\n *\n * It will infer which values are passed as `node` and which as `parent`.\n * @template {UnistNode} Visited\n * Node type.\n * @template {UnistParent} Ancestor\n * Parent type.\n */\n\n/**\n * @typedef {(\n * BuildVisitorFromMatch<\n * Matches<Descendant, Check>,\n * Extract<Descendant, UnistParent>\n * >\n * )} BuildVisitorFromDescendants\n * Build a typed `Visitor` function from a list of descendants and a test.\n *\n * It will infer which values are passed as `node` and which as `parent`.\n * @template {UnistNode} Descendant\n * Node type.\n * @template {Test} Check\n * Test type.\n */\n\n/**\n * @typedef {(\n * BuildVisitorFromDescendants<\n * InclusiveDescendant<Tree>,\n * Check\n * >\n * )} BuildVisitor\n * Build a typed `Visitor` function from a tree and a test.\n *\n * It will infer which values are passed as `node` and which as `parent`.\n * @template {UnistNode} [Tree=UnistNode]\n * Node type.\n * @template {Test} [Check=Test]\n * Test type.\n */\n\nimport {visitParents} from 'unist-util-visit-parents'\n\nexport {CONTINUE, EXIT, SKIP} from 'unist-util-visit-parents'\n\n/**\n * Visit nodes.\n *\n * This algorithm performs *depth-first* *tree traversal* in *preorder*\n * (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).\n *\n * You can choose for which nodes `visitor` is called by passing a `test`.\n * For complex tests, you should test yourself in `visitor`, as it will be\n * faster and will have improved type information.\n *\n * Walking the tree is an intensive task.\n * Make use of the return values of the visitor when possible.\n * Instead of walking a tree multiple times, walk it once, use `unist-util-is`\n * to check if a node matches, and then perform different operations.\n *\n * You can change the tree.\n * See `Visitor` for more info.\n *\n * @overload\n * @param {Tree} tree\n * @param {Check} check\n * @param {BuildVisitor<Tree, Check>} visitor\n * @param {boolean | null | undefined} [reverse]\n * @returns {undefined}\n *\n * @overload\n * @param {Tree} tree\n * @param {BuildVisitor<Tree>} visitor\n * @param {boolean | null | undefined} [reverse]\n * @returns {undefined}\n *\n * @param {UnistNode} tree\n * Tree to traverse.\n * @param {Visitor | Test} testOrVisitor\n * `unist-util-is`-compatible test (optional, omit to pass a visitor).\n * @param {Visitor | boolean | null | undefined} [visitorOrReverse]\n * Handle each node (when test is omitted, pass `reverse`).\n * @param {boolean | null | undefined} [maybeReverse=false]\n * Traverse in reverse preorder (NRL) instead of the default preorder (NLR).\n * @returns {undefined}\n * Nothing.\n *\n * @template {UnistNode} Tree\n * Node type.\n * @template {Test} Check\n * `unist-util-is`-compatible test.\n */\nexport function visit(tree, testOrVisitor, visitorOrReverse, maybeReverse) {\n /** @type {boolean | null | undefined} */\n let reverse\n /** @type {Test} */\n let test\n /** @type {Visitor} */\n let visitor\n\n if (\n typeof testOrVisitor === 'function' &&\n typeof visitorOrReverse !== 'function'\n ) {\n test = undefined\n visitor = testOrVisitor\n reverse = visitorOrReverse\n } else {\n // @ts-expect-error: assume the overload with test was given.\n test = testOrVisitor\n // @ts-expect-error: assume the overload with test was given.\n visitor = visitorOrReverse\n reverse = maybeReverse\n }\n\n visitParents(tree, test, overload, reverse)\n\n /**\n * @param {UnistNode} node\n * @param {Array<UnistParent>} parents\n */\n function overload(node, parents) {\n const parent = parents[parents.length - 1]\n const index = parent ? parent.children.indexOf(node) : undefined\n return visitor(node, index, parent)\n }\n}\n"],"x_google_ignoreList":[0,1,2,3],"mappings":"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"} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
| import { t as visit } from "./lib2.js"; | ||
| //#region src/rehype-alert/rehype-alert.ts | ||
| var ALERT_TYPES = [ | ||
| "note", | ||
| "tip", | ||
| "important", | ||
| "warning", | ||
| "caution", | ||
| "success" | ||
| ]; | ||
| function isWhitespace(node) { | ||
| return node.type === "text" && typeof node.value === "string" && /^\s*$/.test(node.value); | ||
| } | ||
| function rehypeAlert() { | ||
| return (tree) => { | ||
| visit(tree, "element", (node, index, parent) => { | ||
| if (node.tagName !== "blockquote" || typeof index !== "number" || !parent || parent.type !== "root") return; | ||
| const headIndex = node.children.findIndex((child) => !isWhitespace(child)); | ||
| if (headIndex === -1) return; | ||
| const head = node.children[headIndex]; | ||
| if (!head || head.type !== "element" || head.tagName !== "p") return; | ||
| const text = head.children[0]; | ||
| if (!text || text.type !== "text" || !text.value.startsWith("[!")) return; | ||
| const end = text.value.indexOf("]"); | ||
| if (end === -1) return; | ||
| const alertType = text.value.slice(2, end).toLowerCase(); | ||
| if (!ALERT_TYPES.includes(alertType)) return; | ||
| if (end + 1 === text.value.length) { | ||
| const next = head.children[1]; | ||
| if (next) { | ||
| if (next.type !== "element" || next.tagName !== "br") return; | ||
| if (!head.children[2]) return; | ||
| head.children = head.children.slice(2); | ||
| const node = head.children[0]; | ||
| if (node && node.type === "text" && node.value.charAt(0) === "\n") node.value = node.value.slice(1); | ||
| } else { | ||
| const nextIndex = headIndex + 1 < node.children.length && isWhitespace(node.children[headIndex + 1]) ? headIndex + 2 : headIndex + 1; | ||
| if (nextIndex >= node.children.length || node.children[nextIndex]?.type !== "element") return; | ||
| node.children = node.children.slice(nextIndex); | ||
| } | ||
| } else if (text.value.charAt(end + 1) === "\n" && (end + 2 === text.value.length || !/^\s*$/.test(text.value.slice(end + 2)))) text.value = text.value.slice(end + 2); | ||
| else text.value = text.value.replace(/^\s*\[!.*?\]\s*/, ""); | ||
| const contentChildren = []; | ||
| for (let i = headIndex; i < node.children.length; i++) { | ||
| const child = node.children[i]; | ||
| if (child?.type === "element" && child.tagName === "p" && child.children) contentChildren.push(...child.children); | ||
| else contentChildren.push(child); | ||
| } | ||
| parent.children[index] = { | ||
| type: "element", | ||
| tagName: "div", | ||
| properties: { className: ["markdown-alert", `markdown-alert-${alertType}`] }, | ||
| children: [{ | ||
| type: "element", | ||
| tagName: "div", | ||
| properties: { className: ["markdown-alert-icon"] }, | ||
| children: [] | ||
| }, { | ||
| type: "element", | ||
| tagName: "div", | ||
| properties: { className: ["markdown-alert-content"] }, | ||
| children: [{ | ||
| type: "text", | ||
| value: " " | ||
| }, ...contentChildren] | ||
| }] | ||
| }; | ||
| }); | ||
| }; | ||
| } | ||
| //#endregion | ||
| export { rehypeAlert as t }; | ||
| //# sourceMappingURL=rehype-alert.js.map |
| {"version":3,"file":"rehype-alert.js","names":[],"sources":["../../src/rehype-alert/rehype-alert.ts"],"sourcesContent":["import type { Element } from 'hast'\nimport type { Root } from 'mdast'\nimport { visit } from 'unist-util-visit'\n\nconst ALERT_TYPES = ['note', 'tip', 'important', 'warning', 'caution', 'success'] as const\n\n// Simple whitespace check function\nfunction isWhitespace(node: any): boolean {\n return node.type === 'text' && typeof node.value === 'string' && /^\\s*$/.test(node.value)\n}\n\nexport function rehypeAlert() {\n return (tree: Root) => {\n visit(tree, 'element', (node: Element, index, parent: any) => {\n if (node.tagName !== 'blockquote' || typeof index !== 'number' || !parent || parent.type !== 'root') {\n return\n }\n\n // Find the first non-whitespace child\n const headIndex = node.children.findIndex((child) => !isWhitespace(child))\n if (headIndex === -1) {\n return\n }\n\n const head = node.children[headIndex]\n\n if (!head || head.type !== 'element' || head.tagName !== 'p') {\n return\n }\n\n const text = head.children[0]\n if (!text || text.type !== 'text' || !text.value.startsWith('[!')) {\n return\n }\n\n const end = text.value.indexOf(']')\n if (end === -1) {\n return\n }\n\n // Extract the alert type\n const alertType = text.value.slice(2, end).toLowerCase() as (typeof ALERT_TYPES)[number]\n if (!ALERT_TYPES.includes(alertType)) {\n return\n }\n\n // Remove the blockquote if it's empty\n if (end + 1 === text.value.length) {\n const next = head.children[1]\n if (next) {\n if (next.type !== 'element' || next.tagName !== 'br') {\n return\n }\n if (!head.children[2]) {\n return\n }\n head.children = head.children.slice(2)\n const node = head.children[0]\n if (node && node.type === 'text' && node.value.charAt(0) === '\\n') {\n node.value = node.value.slice(1)\n }\n } else {\n const skipped = headIndex + 1 < node.children.length && isWhitespace(node.children[headIndex + 1])\n const nextIndex = skipped ? headIndex + 2 : headIndex + 1\n if (nextIndex >= node.children.length || node.children[nextIndex]?.type !== 'element') {\n return\n }\n\n node.children = node.children.slice(nextIndex)\n }\n } else if (\n text.value.charAt(end + 1) === '\\n' &&\n // Check if the next character is a newline or a non-whitespace character\n (end + 2 === text.value.length || !/^\\s*$/.test(text.value.slice(end + 2)))\n ) {\n text.value = text.value.slice(end + 2)\n } else {\n // Remove the alert marker if it's not followed by a newline or a non-whitespace character\n text.value = text.value.replace(/^\\s*\\[!.*?\\]\\s*/, '')\n }\n\n // Extract content from paragraphs to avoid wrapping in <p> tags\n const contentChildren = []\n for (let i = headIndex; i < node.children.length; i++) {\n const child = node.children[i]\n if (child?.type === 'element' && child.tagName === 'p' && child.children) {\n contentChildren.push(...child.children)\n } else {\n contentChildren.push(child)\n }\n }\n\n // Replace the blockquote with a div containing the alert\n parent.children[index] = {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert', `markdown-alert-${alertType}`] },\n children: [\n {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert-icon'] },\n children: [],\n },\n {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert-content'] },\n children: [{ type: 'text', value: ' ' }, ...contentChildren],\n },\n ],\n }\n })\n }\n}\n"],"mappings":";;AAIA,IAAM,cAAc;CAAC;CAAQ;CAAO;CAAa;CAAW;CAAW;CAAU;AAGjF,SAAS,aAAa,MAAoB;AACxC,QAAO,KAAK,SAAS,UAAU,OAAO,KAAK,UAAU,YAAY,QAAQ,KAAK,KAAK,MAAM;;AAG3F,SAAgB,cAAc;AAC5B,SAAQ,SAAe;AACrB,QAAM,MAAM,YAAY,MAAe,OAAO,WAAgB;AAC5D,OAAI,KAAK,YAAY,gBAAgB,OAAO,UAAU,YAAY,CAAC,UAAU,OAAO,SAAS,OAC3F;GAIF,MAAM,YAAY,KAAK,SAAS,WAAW,UAAU,CAAC,aAAa,MAAM,CAAC;AAC1E,OAAI,cAAc,GAChB;GAGF,MAAM,OAAO,KAAK,SAAS;AAE3B,OAAI,CAAC,QAAQ,KAAK,SAAS,aAAa,KAAK,YAAY,IACvD;GAGF,MAAM,OAAO,KAAK,SAAS;AAC3B,OAAI,CAAC,QAAQ,KAAK,SAAS,UAAU,CAAC,KAAK,MAAM,WAAW,KAAK,CAC/D;GAGF,MAAM,MAAM,KAAK,MAAM,QAAQ,IAAI;AACnC,OAAI,QAAQ,GACV;GAIF,MAAM,YAAY,KAAK,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa;AACxD,OAAI,CAAC,YAAY,SAAS,UAAU,CAClC;AAIF,OAAI,MAAM,MAAM,KAAK,MAAM,QAAQ;IACjC,MAAM,OAAO,KAAK,SAAS;AAC3B,QAAI,MAAM;AACR,SAAI,KAAK,SAAS,aAAa,KAAK,YAAY,KAC9C;AAEF,SAAI,CAAC,KAAK,SAAS,GACjB;AAEF,UAAK,WAAW,KAAK,SAAS,MAAM,EAAE;KACtC,MAAM,OAAO,KAAK,SAAS;AAC3B,SAAI,QAAQ,KAAK,SAAS,UAAU,KAAK,MAAM,OAAO,EAAE,KAAK,KAC3D,MAAK,QAAQ,KAAK,MAAM,MAAM,EAAE;WAE7B;KAEL,MAAM,YADU,YAAY,IAAI,KAAK,SAAS,UAAU,aAAa,KAAK,SAAS,YAAY,GAAG,GACtE,YAAY,IAAI,YAAY;AACxD,SAAI,aAAa,KAAK,SAAS,UAAU,KAAK,SAAS,YAAY,SAAS,UAC1E;AAGF,UAAK,WAAW,KAAK,SAAS,MAAM,UAAU;;cAGhD,KAAK,MAAM,OAAO,MAAM,EAAE,KAAK,SAE9B,MAAM,MAAM,KAAK,MAAM,UAAU,CAAC,QAAQ,KAAK,KAAK,MAAM,MAAM,MAAM,EAAE,CAAC,EAE1E,MAAK,QAAQ,KAAK,MAAM,MAAM,MAAM,EAAE;OAGtC,MAAK,QAAQ,KAAK,MAAM,QAAQ,mBAAmB,GAAG;GAIxD,MAAM,kBAAkB,EAAE;AAC1B,QAAK,IAAI,IAAI,WAAW,IAAI,KAAK,SAAS,QAAQ,KAAK;IACrD,MAAM,QAAQ,KAAK,SAAS;AAC5B,QAAI,OAAO,SAAS,aAAa,MAAM,YAAY,OAAO,MAAM,SAC9D,iBAAgB,KAAK,GAAG,MAAM,SAAS;QAEvC,iBAAgB,KAAK,MAAM;;AAK/B,UAAO,SAAS,SAAS;IACvB,MAAM;IACN,SAAS;IACT,YAAY,EAAE,WAAW,CAAC,kBAAkB,kBAAkB,YAAY,EAAE;IAC5E,UAAU,CACR;KACE,MAAM;KACN,SAAS;KACT,YAAY,EAAE,WAAW,CAAC,sBAAsB,EAAE;KAClD,UAAU,EAAE;KACb,EACD;KACE,MAAM;KACN,SAAS;KACT,YAAY,EAAE,WAAW,CAAC,yBAAyB,EAAE;KACrD,UAAU,CAAC;MAAE,MAAM;MAAQ,OAAO;MAAK,EAAE,GAAG,gBAAgB;KAC7D,CACF;IACF;IACD"} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
+6
-0
| # @scalar/code-highlight | ||
| ## 0.3.5 | ||
| ### Patch Changes | ||
| - [#9451](https://github.com/scalar/scalar/pull/9451): Bundle runtime dependencies into the build so CommonJS-only packages (like `extend` and `debug`) no longer leak to consumers and break Vite dev under pnpm | ||
| ## 0.3.4 | ||
@@ -4,0 +10,0 @@ |
@@ -1,2 +0,2 @@ | ||
| export { syntaxHighlight } from './highlight.js'; | ||
| export { syntaxHighlight } from './highlight'; | ||
| //# sourceMappingURL=index.d.ts.map |
@@ -1,1 +0,3 @@ | ||
| export { syntaxHighlight } from './highlight.js'; | ||
| import "../chunks/lib.js"; | ||
| import { t as syntaxHighlight } from "../chunks/code.js"; | ||
| export { syntaxHighlight }; |
+1
-1
| export { syntaxHighlight } from './code/index.js'; | ||
| export { lowlightLanguageMappings } from './constants.js'; | ||
| export { lowlightLanguageMappings } from './constants'; | ||
| export { basicLanguages, jsonYamlLanguages, standardLanguages } from './languages/index.js'; | ||
@@ -4,0 +4,0 @@ export { type Node, htmlFromMarkdown, isHeading, textFromNode } from './markdown/index.js'; |
+6
-5
@@ -1,5 +0,6 @@ | ||
| export { syntaxHighlight } from './code/index.js'; | ||
| export { lowlightLanguageMappings } from './constants.js'; | ||
| export { basicLanguages, jsonYamlLanguages, standardLanguages } from './languages/index.js'; | ||
| export { htmlFromMarkdown, isHeading, textFromNode } from './markdown/index.js'; | ||
| export { rehypeHighlight } from './rehype-highlight/index.js'; | ||
| import { i as lowlightLanguageMappings, t as rehypeHighlight } from "./chunks/rehype-highlight.js"; | ||
| import "./chunks/lib.js"; | ||
| import { n as jsonYamlLanguages, r as basicLanguages, t as standardLanguages } from "./chunks/languages.js"; | ||
| import { t as syntaxHighlight } from "./chunks/code.js"; | ||
| import { a as textFromNode, n as htmlFromMarkdown, r as isHeading } from "./chunks/markdown.js"; | ||
| export { basicLanguages, htmlFromMarkdown, isHeading, jsonYamlLanguages, lowlightLanguageMappings, rehypeHighlight, standardLanguages, syntaxHighlight, textFromNode }; |
@@ -1,4 +0,4 @@ | ||
| export { basicLanguages } from './basic.js'; | ||
| export { jsonYamlLanguages } from './json-yaml.js'; | ||
| export { standardLanguages } from './standard.js'; | ||
| export { basicLanguages } from './basic'; | ||
| export { jsonYamlLanguages } from './json-yaml'; | ||
| export { standardLanguages } from './standard'; | ||
| //# sourceMappingURL=index.d.ts.map |
@@ -1,3 +0,2 @@ | ||
| export { basicLanguages } from './basic.js'; | ||
| export { jsonYamlLanguages } from './json-yaml.js'; | ||
| export { standardLanguages } from './standard.js'; | ||
| import { n as jsonYamlLanguages, r as basicLanguages, t as standardLanguages } from "../chunks/languages.js"; | ||
| export { basicLanguages, jsonYamlLanguages, standardLanguages }; |
@@ -1,2 +0,2 @@ | ||
| export { getHeadings, htmlFromMarkdown, isHeading, splitContent, textFromNode, type Node } from './markdown.js'; | ||
| export { getHeadings, htmlFromMarkdown, isHeading, splitContent, textFromNode, type Node } from './markdown'; | ||
| //# sourceMappingURL=index.d.ts.map |
@@ -1,1 +0,3 @@ | ||
| export { getHeadings, htmlFromMarkdown, isHeading, splitContent, textFromNode } from './markdown.js'; | ||
| import "../chunks/lib.js"; | ||
| import { a as textFromNode, i as splitContent, n as htmlFromMarkdown, r as isHeading, t as getHeadings } from "../chunks/markdown.js"; | ||
| export { getHeadings, htmlFromMarkdown, isHeading, splitContent, textFromNode }; |
@@ -1,2 +0,2 @@ | ||
| export { rehypeAlert } from './rehype-alert.js'; | ||
| export { rehypeAlert } from './rehype-alert'; | ||
| //# sourceMappingURL=index.d.ts.map |
@@ -1,1 +0,2 @@ | ||
| export { rehypeAlert } from './rehype-alert.js'; | ||
| import { t as rehypeAlert } from "../chunks/rehype-alert.js"; | ||
| export { rehypeAlert }; |
@@ -1,2 +0,2 @@ | ||
| export { rehypeHighlight } from './rehype-highlight.js'; | ||
| export { rehypeHighlight } from './rehype-highlight'; | ||
| //# sourceMappingURL=index.d.ts.map |
@@ -1,1 +0,2 @@ | ||
| export { rehypeHighlight } from './rehype-highlight.js'; | ||
| import { t as rehypeHighlight } from "../chunks/rehype-highlight.js"; | ||
| export { rehypeHighlight }; |
+4
-4
@@ -19,3 +19,3 @@ { | ||
| ], | ||
| "version": "0.3.4", | ||
| "version": "0.3.5", | ||
| "engines": { | ||
@@ -96,10 +96,10 @@ "node": ">=22" | ||
| "vite": "8.0.0", | ||
| "@scalar/themes": "0.15.3" | ||
| "@scalar/themes": "0.16.0" | ||
| }, | ||
| "scripts": { | ||
| "build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json && shx cp -r src/css dist/css", | ||
| "build": "vite build && tsc -p tsconfig.build.json --emitDeclarationOnly && tsc-alias -p tsconfig.build.json && shx cp -r src/css dist/css", | ||
| "dev": "vite", | ||
| "test": "vitest --run", | ||
| "types:check": "tsc --noEmit" | ||
| "types:check": "tsgo --noEmit" | ||
| } | ||
| } |
| import rehypeParse from 'rehype-parse'; | ||
| import rehypeStringify from 'rehype-stringify'; | ||
| import { unified } from 'unified'; | ||
| import { visit } from 'unist-util-visit'; | ||
| import { lowlightLanguageMappings } from '../constants.js'; | ||
| import { rehypeHighlight } from '../rehype-highlight/index.js'; | ||
| import { codeBlockLinesPlugin } from './line-numbers.js'; | ||
| /** | ||
| * Syntax highlights a code string using the `rehype-highlight` library. | ||
| */ | ||
| export function syntaxHighlight(codeString, options) { | ||
| // Simple restriction on credentials to prevent unexpected behavior | ||
| const credentials = (typeof options?.maskCredentials === 'string' ? [options.maskCredentials] : (options?.maskCredentials ?? [])).filter((c) => { | ||
| // Credentials must be at least 3 characters to mask. | ||
| if (c.length < 3) { | ||
| return false; | ||
| } | ||
| return true; | ||
| }); | ||
| // Classname is used by lowlight to select the language model | ||
| const className = `language-${lowlightLanguageMappings[options.lang] ?? options.lang}`; | ||
| // biome-ignore lint/suspicious/noEmptyBlockStatements: empty plugin | ||
| const nullPlugin = (() => { }); | ||
| const html = unified() | ||
| // Parses markdown | ||
| .use(rehypeParse, { fragment: true }) | ||
| // Raw code string must be injected after initial hast parsing | ||
| // so that HTML code is not parsed into the hast tree | ||
| .use(injectRawCodeStringPlugin(codeString)) | ||
| // Syntax highlighting | ||
| .use(rehypeHighlight, { | ||
| languages: options.languages, | ||
| }) | ||
| .use(options?.lineNumbers ? codeBlockLinesPlugin : nullPlugin) | ||
| // Converts the HTML AST to a string | ||
| .use(rehypeStringify) | ||
| // Run the pipeline | ||
| .processSync(`<pre><code class="${className}"></code></pre>`); | ||
| const htmlString = html.toString(); | ||
| // Replace any credentials with a wrapper element | ||
| return credentials.length | ||
| ? credentials.reduce((acc, credential) => acc | ||
| .split(credential) | ||
| .join(`<span class="credential"><span class="credential-value">${credential}</span></span>`), htmlString) | ||
| : htmlString; | ||
| } | ||
| /** | ||
| * To prevent unified from parsing any content of the code string we inject | ||
| * it as a raw text node into the AST tree as a child of the code element | ||
| */ | ||
| function injectRawCodeStringPlugin(rawCodeString) { | ||
| return () => (tree) => { | ||
| visit(tree, 'element', (node) => { | ||
| if (node.tagName === 'code') { | ||
| node.children.push({ | ||
| type: 'text', | ||
| value: rawCodeString, | ||
| }); | ||
| } | ||
| }); | ||
| }; | ||
| } |
| import { visit } from 'unist-util-visit'; | ||
| // --------------------------------------------------------------------------- | ||
| // Line Numbering plugin | ||
| function isText(element) { | ||
| return element?.type === 'text'; | ||
| } | ||
| function isElement(node) { | ||
| return node?.type === 'element'; | ||
| } | ||
| function textElement(value) { | ||
| return { type: 'text', value }; | ||
| } | ||
| function lineBreak() { | ||
| return { type: 'text', value: '\n' }; | ||
| } | ||
| /** | ||
| * Adds lines to code blocks | ||
| */ | ||
| export function codeBlockLinesPlugin() { | ||
| return (tree) => { | ||
| visit(tree, 'element', (node, _i, parent) => { | ||
| if (parent?.type === 'element' && parent.tagName === 'pre' && node.tagName === 'code') { | ||
| let numLines = 0; | ||
| // Wraps each line in a span | ||
| node.children = addLines(node); | ||
| // Adds a line break to the end of each line | ||
| node.children.forEach((child) => { | ||
| if (child.type === 'element' && child.tagName === 'span') { | ||
| const lastChild = child.children[child.children.length - 1]; | ||
| if (lastChild && (!isText(lastChild) || (isText(lastChild) && !hasLineBreak(lastChild)))) { | ||
| child.children.push(lineBreak()); | ||
| numLines++; | ||
| } | ||
| } | ||
| }); | ||
| // We need to maintain a count of the total lines to allow space for the labels | ||
| node.properties.style = [`--line-count: ${numLines};`, `--line-digits: ${numLines.toString().length};`]; | ||
| } | ||
| }); | ||
| // console.log('NUMBER OF LINES IS: ', numLines) | ||
| }; | ||
| } | ||
| /** | ||
| * Adds lines to a node recursively and returns them | ||
| * | ||
| * @param node - The node to add lines to | ||
| * @param lines - The current lines | ||
| * @param copyParent - Whether to copy the parent node to save the original node styles | ||
| */ | ||
| function addLines(node, lines = [], copyParent) { | ||
| const line = () => lines[lines.length - 1] ?? ((lines.push(createLine()) && lines[lines.length - 1]) || undefined); | ||
| node.children.forEach((child) => { | ||
| if (isText(child) && hasLineBreak(child)) { | ||
| const split = child.value.split(/\n/); | ||
| split.forEach((content, i) => { | ||
| if (copyParent) { | ||
| line()?.children.push({ ...node, children: [textElement(content)] }); | ||
| } | ||
| else { | ||
| line()?.children.push(textElement(content)); | ||
| } | ||
| i !== split.length - 1 && lines.push(createLine()); | ||
| }); | ||
| } | ||
| else if (isElement(child) && child.children.some(hasLineBreak)) { | ||
| addLines(child, lines, true); | ||
| } | ||
| else { | ||
| line()?.children.push(child); | ||
| } | ||
| }); | ||
| return lines; | ||
| } | ||
| /** | ||
| * Creates a new line element | ||
| * | ||
| * @param children - The children the line should have initially | ||
| */ | ||
| function createLine(...children) { | ||
| return { | ||
| type: 'element', | ||
| tagName: 'span', | ||
| properties: { class: ['line'] }, | ||
| children, | ||
| }; | ||
| } | ||
| /** | ||
| * Checks if a node has a line break | ||
| * | ||
| * @param node - The node to check | ||
| */ | ||
| function hasLineBreak(node) { | ||
| return (isText(node) && /\r?\n/.test(node.value)) || (isElement(node) && node.children.some(hasLineBreak)); | ||
| } |
| /** Map common markdown language shortcuts to lowlight languages */ | ||
| export const lowlightLanguageMappings = { | ||
| 'ts': 'typescript', | ||
| 'js': 'javascript', | ||
| 'py': 'python', | ||
| 'py3': 'python', | ||
| 'c#': 'csharp', | ||
| 'c++': 'cpp', | ||
| 'node': 'javascript', | ||
| }; | ||
| // |
| import bash from 'highlight.js/lib/languages/bash'; | ||
| import css from 'highlight.js/lib/languages/css'; | ||
| import javascript from 'highlight.js/lib/languages/javascript'; | ||
| import json from 'highlight.js/lib/languages/json'; | ||
| import less from 'highlight.js/lib/languages/less'; | ||
| import markdown from 'highlight.js/lib/languages/markdown'; | ||
| import plaintext from 'highlight.js/lib/languages/plaintext'; | ||
| import python from 'highlight.js/lib/languages/python'; | ||
| import scss from 'highlight.js/lib/languages/scss'; | ||
| import shell from 'highlight.js/lib/languages/shell'; | ||
| import typescript from 'highlight.js/lib/languages/typescript'; | ||
| import xml from 'highlight.js/lib/languages/xml'; | ||
| import yaml from 'highlight.js/lib/languages/yaml'; | ||
| const basicLanguages = { | ||
| bash, | ||
| css, | ||
| html: xml, | ||
| javascript, | ||
| json, | ||
| less, | ||
| markdown, | ||
| plaintext, | ||
| python, | ||
| scss, | ||
| shell, | ||
| typescript, | ||
| xml, | ||
| yaml, | ||
| }; | ||
| export { basicLanguages }; |
| /** | ||
| * cURL grammar adapted from highlightjs-curl to avoid a CJS dependency. | ||
| */ | ||
| const curlLanguage = (hljs) => { | ||
| const quoteString = { | ||
| className: 'string', | ||
| begin: /"/, | ||
| end: /"/, | ||
| contains: [ | ||
| hljs.BACKSLASH_ESCAPE, | ||
| { | ||
| className: 'variable', | ||
| begin: /\$\(/, | ||
| end: /\)/, | ||
| contains: [hljs.BACKSLASH_ESCAPE], | ||
| }, | ||
| ], | ||
| relevance: 0, | ||
| }; | ||
| const optionRequest = { | ||
| className: 'literal', | ||
| begin: /(--request|-X)\s/, | ||
| contains: [ | ||
| { | ||
| className: 'symbol', | ||
| begin: /(get|post|delete|options|head|put|patch|trace|connect)/, | ||
| end: /\s/, | ||
| returnEnd: true, | ||
| }, | ||
| ], | ||
| returnEnd: true, | ||
| relevance: 10, | ||
| }; | ||
| const option = { | ||
| className: 'literal', | ||
| begin: /--/, | ||
| end: /[\s"]/, | ||
| returnEnd: true, | ||
| relevance: 0, | ||
| }; | ||
| const optionSingle = { | ||
| className: 'literal', | ||
| begin: /-\w/, | ||
| end: /[\s"]/, | ||
| returnEnd: true, | ||
| relevance: 0, | ||
| }; | ||
| const escapedQuote = { | ||
| className: 'string', | ||
| begin: /\\"/, | ||
| relevance: 0, | ||
| }; | ||
| const aposString = { | ||
| className: 'string', | ||
| begin: /'/, | ||
| end: /'/, | ||
| relevance: 0, | ||
| }; | ||
| const number = { | ||
| className: 'number', | ||
| variants: [{ begin: hljs.C_NUMBER_RE }], | ||
| relevance: 0, | ||
| }; | ||
| const pathMode = { | ||
| match: /(\/[a-z._-]+)+/, | ||
| }; | ||
| return { | ||
| name: 'curl', | ||
| aliases: ['curl'], | ||
| keywords: 'curl', | ||
| case_insensitive: true, | ||
| contains: [ | ||
| optionRequest, | ||
| option, | ||
| optionSingle, | ||
| quoteString, | ||
| escapedQuote, | ||
| aposString, | ||
| hljs.APOS_STRING_MODE, | ||
| hljs.QUOTE_STRING_MODE, | ||
| number, | ||
| pathMode, | ||
| ], | ||
| }; | ||
| }; | ||
| export default curlLanguage; |
| import json from 'highlight.js/lib/languages/json'; | ||
| import plaintext from 'highlight.js/lib/languages/plaintext'; | ||
| import yaml from 'highlight.js/lib/languages/yaml'; | ||
| const jsonYamlLanguages = { | ||
| json, | ||
| plaintext, | ||
| yaml, | ||
| }; | ||
| export { jsonYamlLanguages }; |
| import bash from 'highlight.js/lib/languages/bash'; | ||
| import c from 'highlight.js/lib/languages/c'; | ||
| import clojure from 'highlight.js/lib/languages/clojure'; | ||
| import cpp from 'highlight.js/lib/languages/cpp'; | ||
| import csharp from 'highlight.js/lib/languages/csharp'; | ||
| import css from 'highlight.js/lib/languages/css'; | ||
| import dart from 'highlight.js/lib/languages/dart'; | ||
| import diff from 'highlight.js/lib/languages/diff'; | ||
| import dockerfile from 'highlight.js/lib/languages/dockerfile'; | ||
| import elixir from 'highlight.js/lib/languages/elixir'; | ||
| import fsharp from 'highlight.js/lib/languages/fsharp'; | ||
| import go from 'highlight.js/lib/languages/go'; | ||
| import graphql from 'highlight.js/lib/languages/graphql'; | ||
| import haskell from 'highlight.js/lib/languages/haskell'; | ||
| import http from 'highlight.js/lib/languages/http'; | ||
| import ini from 'highlight.js/lib/languages/ini'; | ||
| import java from 'highlight.js/lib/languages/java'; | ||
| import javascript from 'highlight.js/lib/languages/javascript'; | ||
| import json from 'highlight.js/lib/languages/json'; | ||
| import kotlin from 'highlight.js/lib/languages/kotlin'; | ||
| import less from 'highlight.js/lib/languages/less'; | ||
| import lua from 'highlight.js/lib/languages/lua'; | ||
| import makefile from 'highlight.js/lib/languages/makefile'; | ||
| import markdown from 'highlight.js/lib/languages/markdown'; | ||
| import matlab from 'highlight.js/lib/languages/matlab'; | ||
| import nginx from 'highlight.js/lib/languages/nginx'; | ||
| import objectivec from 'highlight.js/lib/languages/objectivec'; | ||
| import ocaml from 'highlight.js/lib/languages/ocaml'; | ||
| import perl from 'highlight.js/lib/languages/perl'; | ||
| import php from 'highlight.js/lib/languages/php'; | ||
| import plaintext from 'highlight.js/lib/languages/plaintext'; | ||
| import powershell from 'highlight.js/lib/languages/powershell'; | ||
| import properties from 'highlight.js/lib/languages/properties'; | ||
| import python from 'highlight.js/lib/languages/python'; | ||
| import r from 'highlight.js/lib/languages/r'; | ||
| import ruby from 'highlight.js/lib/languages/ruby'; | ||
| import rust from 'highlight.js/lib/languages/rust'; | ||
| import scala from 'highlight.js/lib/languages/scala'; | ||
| import scss from 'highlight.js/lib/languages/scss'; | ||
| import shell from 'highlight.js/lib/languages/shell'; | ||
| import sql from 'highlight.js/lib/languages/sql'; | ||
| import swift from 'highlight.js/lib/languages/swift'; | ||
| import typescript from 'highlight.js/lib/languages/typescript'; | ||
| import xml from 'highlight.js/lib/languages/xml'; | ||
| import yaml from 'highlight.js/lib/languages/yaml'; | ||
| import curl from './curl.js'; | ||
| /** | ||
| * We group languages into three categories based on their popularity and usage. | ||
| * This helps in optimizing the bundle size by allowing users to include only | ||
| * the languages they need. | ||
| * | ||
| * 1. Standard Languages: These are the most popular languages that cover the | ||
| * majority of use cases. | ||
| * 2. Medium Languages: These languages are not as popular as the ones in | ||
| * `standardLanguages`, but still have a decent amount of users. | ||
| * 3. Specialized Languages: These languages are more specialized and have a | ||
| * smaller user base. | ||
| * | ||
| * Each category is represented as a separate object, making it easy to import | ||
| * only the languages you need. | ||
| */ | ||
| /** | ||
| * These are the most popular languages that cover the majority of use cases. | ||
| */ | ||
| export const standardLanguages = { | ||
| bash, | ||
| c, | ||
| clojure, | ||
| cpp, | ||
| csharp, | ||
| css, | ||
| curl, | ||
| dart, | ||
| diff, | ||
| docker: dockerfile, | ||
| dockerfile, | ||
| elixir, | ||
| fsharp, | ||
| go, | ||
| graphql, | ||
| haskell, | ||
| html: xml, | ||
| http, | ||
| ini, | ||
| java, | ||
| javascript, | ||
| json, | ||
| kotlin, | ||
| less, | ||
| lua, | ||
| makefile, | ||
| markdown, | ||
| matlab, | ||
| nginx, | ||
| objectivec, | ||
| ocaml, | ||
| perl, | ||
| php, | ||
| plaintext, | ||
| powershell, | ||
| properties, | ||
| python, | ||
| r, | ||
| ruby, | ||
| rust, | ||
| scala, | ||
| scss, | ||
| shell, | ||
| sql, | ||
| swift, | ||
| toml: ini, | ||
| typescript, | ||
| xml, | ||
| yaml, | ||
| }; |
| import rehypeExternalLinks from 'rehype-external-links'; | ||
| import rehypeFormat from 'rehype-format'; | ||
| import rehypeParse from 'rehype-parse'; | ||
| import rehypeRaw from 'rehype-raw'; | ||
| import rehypeSanitize, { defaultSchema } from 'rehype-sanitize'; | ||
| import rehypeStringify from 'rehype-stringify'; | ||
| import remarkGfm from 'remark-gfm'; | ||
| import remarkParse from 'remark-parse'; | ||
| import remarkRehype from 'remark-rehype'; | ||
| import remarkStringify from 'remark-stringify'; | ||
| import { unified } from 'unified'; | ||
| import { SKIP, visit } from 'unist-util-visit'; | ||
| import { standardLanguages } from '../languages/index.js'; | ||
| import { rehypeAlert } from '../rehype-alert/index.js'; | ||
| import { rehypeHighlight } from '../rehype-highlight/index.js'; | ||
| /** | ||
| * Type-guard to check if a node is a heading. | ||
| */ | ||
| export const isHeading = (node) => { | ||
| return node.type === 'heading' && 'depth' in node && 'children' in node; | ||
| }; | ||
| /** | ||
| * Plugin to transform nodes in a Markdown AST | ||
| */ | ||
| const transformNodes = (options, ..._ignored) => (tree) => { | ||
| if (!options?.transform || !options?.type) { | ||
| return; | ||
| } | ||
| visit(tree, options?.type, (node) => { | ||
| options?.transform ? options?.transform(node) : node; | ||
| return SKIP; | ||
| }); | ||
| return; | ||
| }; | ||
| const TAGS_WITH_INLINE_MARKDOWN = new Set(['dd', 'dt', 'li', 'p', 'summary', 'td', 'th']); | ||
| const MAY_CONTAIN_INLINE_MARKDOWN = /[`*_\[~]/; | ||
| /** | ||
| * Preserve HTML-like text in inline markdown by turning mdast `html` nodes into text nodes. | ||
| */ | ||
| const preserveHtmlLikeText = () => (tree) => { | ||
| visit(tree, 'html', (node, index, parent) => { | ||
| if (typeof index !== 'number' || !parent || !('children' in parent) || !Array.isArray(parent.children)) { | ||
| return; | ||
| } | ||
| parent.children[index] = { | ||
| type: 'text', | ||
| value: node.value ?? '', | ||
| }; | ||
| }); | ||
| }; | ||
| const inlineMarkdownProcessor = unified().use(remarkParse).use(remarkGfm).use(preserveHtmlLikeText).use(remarkRehype); | ||
| const htmlFragmentParser = unified().use(rehypeParse, { fragment: true }); | ||
| const htmlFragmentStringifier = unified().use(rehypeStringify); | ||
| /** | ||
| * Parse inline markdown and return children from the generated paragraph. | ||
| */ | ||
| const extractInlineChildrenFromMarkdown = (value) => { | ||
| const tree = inlineMarkdownProcessor.runSync(inlineMarkdownProcessor.parse(value)); | ||
| if (tree.children.length !== 1) { | ||
| return []; | ||
| } | ||
| const paragraph = tree.children.at(0); | ||
| if (!paragraph || paragraph.type !== 'element' || paragraph.tagName !== 'p') { | ||
| return []; | ||
| } | ||
| return paragraph.children; | ||
| }; | ||
| /** | ||
| * Re-parses text nodes in selected HTML tags so inline markdown works in tags like <p>. | ||
| */ | ||
| const transformInlineMarkdownInHtml = () => (tree) => { | ||
| visit(tree, 'element', (node) => { | ||
| if (!TAGS_WITH_INLINE_MARKDOWN.has(node.tagName)) { | ||
| return; | ||
| } | ||
| node.children = node.children.flatMap((child) => { | ||
| if (child.type !== 'text' || !MAY_CONTAIN_INLINE_MARKDOWN.test(child.value)) { | ||
| return [child]; | ||
| } | ||
| const markdownChildren = extractInlineChildrenFromMarkdown(child.value); | ||
| return markdownChildren.length ? markdownChildren : [child]; | ||
| }); | ||
| }); | ||
| }; | ||
| /** | ||
| * Rewrites raw HTML strings so inline markdown parsing is only applied to raw HTML input. | ||
| */ | ||
| const transformInlineMarkdownInRawHtml = () => (tree) => { | ||
| visit(tree, 'raw', (node) => { | ||
| if (typeof node.value !== 'string' || !MAY_CONTAIN_INLINE_MARKDOWN.test(node.value)) { | ||
| return; | ||
| } | ||
| const htmlFragmentTree = htmlFragmentParser.parse(node.value); | ||
| transformInlineMarkdownInHtml()(htmlFragmentTree); | ||
| node.value = htmlFragmentStringifier.stringify(htmlFragmentTree); | ||
| }); | ||
| }; | ||
| /** | ||
| * Take a Markdown string and generate HTML from it | ||
| */ | ||
| export function htmlFromMarkdown(markdown, options) { | ||
| // Add permitted tags and remove stripped ones | ||
| const removeTags = options?.removeTags ?? []; | ||
| const tagNames = [...(defaultSchema.tagNames ?? []), ...(options?.allowTags ?? [])].filter((t) => !removeTags.includes(t)); | ||
| const html = unified() | ||
| // Parses markdown | ||
| .use(remarkParse) | ||
| // Support autolink literals, footnotes, strikethrough, tables and tasklists | ||
| .use(remarkGfm) | ||
| .use(transformNodes, { | ||
| transform: options?.transform, | ||
| type: options?.transformType, | ||
| }) | ||
| // Allows any HTML tags | ||
| .use(remarkRehype, { allowDangerousHtml: true }) | ||
| // Adds GitHub alerts | ||
| .use(rehypeAlert) | ||
| // Parse inline markdown only inside raw HTML fragments, not normal markdown output | ||
| .use(transformInlineMarkdownInRawHtml) | ||
| // Creates an HTML AST | ||
| .use(rehypeRaw) | ||
| // Removes disallowed tags | ||
| .use(rehypeSanitize, { | ||
| ...defaultSchema, | ||
| // Don't prefix the heading ids | ||
| clobberPrefix: '', | ||
| // Makes it even more strict | ||
| tagNames, | ||
| attributes: { | ||
| ...defaultSchema.attributes, | ||
| abbr: ['title'], | ||
| // Allow all class names while preserving the existing default attributes | ||
| '*': [...(defaultSchema.attributes?.['*'] ?? []), 'className'], | ||
| }, | ||
| // Strip content of dangerous elements, not just the tags | ||
| strip: ['script', 'style', 'object', 'embed', 'form'], | ||
| }) | ||
| // Syntax highlighting | ||
| .use(rehypeHighlight, { | ||
| languages: standardLanguages, | ||
| // Enable auto detection | ||
| detect: true, | ||
| }) | ||
| // Adds target="_blank" to external links | ||
| .use(rehypeExternalLinks, { target: '_blank' }) | ||
| // Formats the HTML | ||
| .use(rehypeFormat) | ||
| // Converts the HTML AST to a string | ||
| .use(rehypeStringify) | ||
| // Run the pipeline | ||
| .processSync(markdown); | ||
| return html.toString(); | ||
| } | ||
| /** | ||
| * Create a Markdown AST from a string. | ||
| */ | ||
| function getMarkdownAst(markdown) { | ||
| return unified().use(remarkParse).use(remarkGfm).parse(markdown); | ||
| } | ||
| /** | ||
| * Find all headings of a specific type in a Markdown AST. | ||
| */ | ||
| export function getHeadings(markdown, depth = 1) { | ||
| const tree = getMarkdownAst(markdown); | ||
| const nodes = []; | ||
| visit(tree, 'heading', (node) => { | ||
| const text = textFromNode(node); | ||
| if (text) { | ||
| nodes.push({ depth: node.depth ?? depth, value: text }); | ||
| } | ||
| }); | ||
| return nodes; | ||
| } | ||
| /** | ||
| * Extract plain text from a Markdown AST node (recursively). | ||
| * | ||
| * Handles headings with nested phrasing content such as links. | ||
| */ | ||
| export function textFromNode(node) { | ||
| if (node.type === 'text') { | ||
| return node.value ?? ''; | ||
| } | ||
| if ('children' in node && Array.isArray(node.children)) { | ||
| return node.children.map((child) => textFromNode(child)).join(''); | ||
| } | ||
| return ''; | ||
| } | ||
| /** | ||
| * Return multiple Markdown documents. Every heading should be its own document. | ||
| */ | ||
| export function splitContent(markdown) { | ||
| const tree = getMarkdownAst(markdown); | ||
| /** Sections */ | ||
| const sections = []; | ||
| /** Nodes inside a section */ | ||
| let nodes = []; | ||
| tree.children?.forEach((node) => { | ||
| // If the node is a heading, start a new section | ||
| if (node.type === 'heading') { | ||
| if (nodes.length) { | ||
| sections.push(nodes); | ||
| } | ||
| sections.push([node]); | ||
| nodes = []; | ||
| } | ||
| // Otherwise, add the node to the current section | ||
| else { | ||
| nodes.push(node); | ||
| } | ||
| }); | ||
| // Add any remaining nodes | ||
| if (nodes.length) { | ||
| sections.push(nodes); | ||
| } | ||
| return sections.map((section) => createDocument(section)); | ||
| } | ||
| /** | ||
| * Use remark to create a Markdown document from a list of nodes. | ||
| */ | ||
| function createDocument(nodes) { | ||
| // Create the Markdown string | ||
| const markdown = unified().use(remarkStringify).use(remarkGfm).stringify({ | ||
| type: 'root', | ||
| children: nodes, | ||
| }); | ||
| // Remove the whitespace | ||
| return markdown.trim(); | ||
| } |
| import { visit } from 'unist-util-visit'; | ||
| const ALERT_TYPES = ['note', 'tip', 'important', 'warning', 'caution', 'success']; | ||
| // Simple whitespace check function | ||
| function isWhitespace(node) { | ||
| return node.type === 'text' && typeof node.value === 'string' && /^\s*$/.test(node.value); | ||
| } | ||
| export function rehypeAlert() { | ||
| return (tree) => { | ||
| visit(tree, 'element', (node, index, parent) => { | ||
| if (node.tagName !== 'blockquote' || typeof index !== 'number' || !parent || parent.type !== 'root') { | ||
| return; | ||
| } | ||
| // Find the first non-whitespace child | ||
| const headIndex = node.children.findIndex((child) => !isWhitespace(child)); | ||
| if (headIndex === -1) { | ||
| return; | ||
| } | ||
| const head = node.children[headIndex]; | ||
| if (!head || head.type !== 'element' || head.tagName !== 'p') { | ||
| return; | ||
| } | ||
| const text = head.children[0]; | ||
| if (!text || text.type !== 'text' || !text.value.startsWith('[!')) { | ||
| return; | ||
| } | ||
| const end = text.value.indexOf(']'); | ||
| if (end === -1) { | ||
| return; | ||
| } | ||
| // Extract the alert type | ||
| const alertType = text.value.slice(2, end).toLowerCase(); | ||
| if (!ALERT_TYPES.includes(alertType)) { | ||
| return; | ||
| } | ||
| // Remove the blockquote if it's empty | ||
| if (end + 1 === text.value.length) { | ||
| const next = head.children[1]; | ||
| if (next) { | ||
| if (next.type !== 'element' || next.tagName !== 'br') { | ||
| return; | ||
| } | ||
| if (!head.children[2]) { | ||
| return; | ||
| } | ||
| head.children = head.children.slice(2); | ||
| const node = head.children[0]; | ||
| if (node && node.type === 'text' && node.value.charAt(0) === '\n') { | ||
| node.value = node.value.slice(1); | ||
| } | ||
| } | ||
| else { | ||
| const skipped = headIndex + 1 < node.children.length && isWhitespace(node.children[headIndex + 1]); | ||
| const nextIndex = skipped ? headIndex + 2 : headIndex + 1; | ||
| if (nextIndex >= node.children.length || node.children[nextIndex]?.type !== 'element') { | ||
| return; | ||
| } | ||
| node.children = node.children.slice(nextIndex); | ||
| } | ||
| } | ||
| else if (text.value.charAt(end + 1) === '\n' && | ||
| // Check if the next character is a newline or a non-whitespace character | ||
| (end + 2 === text.value.length || !/^\s*$/.test(text.value.slice(end + 2)))) { | ||
| text.value = text.value.slice(end + 2); | ||
| } | ||
| else { | ||
| // Remove the alert marker if it's not followed by a newline or a non-whitespace character | ||
| text.value = text.value.replace(/^\s*\[!.*?\]\s*/, ''); | ||
| } | ||
| // Extract content from paragraphs to avoid wrapping in <p> tags | ||
| const contentChildren = []; | ||
| for (let i = headIndex; i < node.children.length; i++) { | ||
| const child = node.children[i]; | ||
| if (child?.type === 'element' && child.tagName === 'p' && child.children) { | ||
| contentChildren.push(...child.children); | ||
| } | ||
| else { | ||
| contentChildren.push(child); | ||
| } | ||
| } | ||
| // Replace the blockquote with a div containing the alert | ||
| parent.children[index] = { | ||
| type: 'element', | ||
| tagName: 'div', | ||
| properties: { className: ['markdown-alert', `markdown-alert-${alertType}`] }, | ||
| children: [ | ||
| { | ||
| type: 'element', | ||
| tagName: 'div', | ||
| properties: { className: ['markdown-alert-icon'] }, | ||
| children: [], | ||
| }, | ||
| { | ||
| type: 'element', | ||
| tagName: 'div', | ||
| properties: { className: ['markdown-alert-content'] }, | ||
| children: [{ type: 'text', value: ' ' }, ...contentChildren], | ||
| }, | ||
| ], | ||
| }; | ||
| }); | ||
| }; | ||
| } |
| import { toText } from 'hast-util-to-text'; | ||
| import { createLowlight } from 'lowlight'; | ||
| import { visit } from 'unist-util-visit'; | ||
| import { lowlightLanguageMappings } from '../constants.js'; | ||
| const emptyOptions = {}; | ||
| /** | ||
| * Lowlight syntax highlighting plugin for rehype pipelines | ||
| * | ||
| * Derived from: @url https://github.com/rehypejs/rehype-highlight/blob/main/lib/index.js | ||
| */ | ||
| export function rehypeHighlight(options) { | ||
| const settings = options || emptyOptions; | ||
| const aliases = settings.aliases; | ||
| const detect = options?.detect ?? false; | ||
| const languages = settings.languages; | ||
| const plainText = settings.plainText; | ||
| const prefix = settings.prefix; | ||
| const subset = settings.subset; | ||
| let name = 'hljs'; | ||
| // Create a lowlight instance if not provided | ||
| const lowlight = options?.lowlight ?? createLowlight(languages); | ||
| if (aliases) { | ||
| lowlight.registerAlias(aliases); | ||
| } | ||
| if (prefix) { | ||
| const pos = prefix.indexOf('-'); | ||
| name = pos > -1 ? prefix.slice(0, pos) : prefix; | ||
| } | ||
| /** Transform.*/ | ||
| return (tree, file) => { | ||
| visit(tree, 'element', (node, _, parent) => { | ||
| if (node.tagName !== 'code' || !parent || parent.type !== 'element' || parent.tagName !== 'pre') { | ||
| return; | ||
| } | ||
| const lang = language(node); | ||
| if (lang === 'no-highlight' || (!lang && !detect) || (lang && plainText?.includes(lang))) { | ||
| return; | ||
| } | ||
| if (!Array.isArray(node.properties.className)) { | ||
| node.properties.className = []; | ||
| } | ||
| if (!node.properties.className.includes(name)) { | ||
| node.properties.className.unshift(name); | ||
| } | ||
| let result; | ||
| try { | ||
| result = lang | ||
| ? lowlight.highlight(lang, toText(parent), { prefix }) | ||
| : lowlight.highlightAuto(toText(parent), { prefix, subset }); | ||
| } | ||
| catch (error) { | ||
| const cause = error; | ||
| if (lang && /Unknown language/.test(cause.message)) { | ||
| file.message(`Cannot highlight as \`${lang}\`, it's not registered`, { | ||
| ancestors: [parent, node], | ||
| cause, | ||
| place: node.position, | ||
| ruleId: 'missing-language', | ||
| source: 'rehype-highlight', | ||
| }); | ||
| /* c8 ignore next 5 -- throw arbitrary hljs errors */ | ||
| return; | ||
| } | ||
| throw cause; | ||
| } | ||
| if (!lang && result.data?.language) { | ||
| node.properties.className.push('language-' + result.data.language); | ||
| } | ||
| if (result.children.length > 0) { | ||
| node.children = result.children; | ||
| } | ||
| }); | ||
| }; | ||
| } | ||
| /** Get the programming language of `node` or an empty string */ | ||
| function language(node) { | ||
| const list = node.properties.className; | ||
| if (!Array.isArray(list)) { | ||
| return ''; | ||
| } | ||
| const name = list.reduce((result, _item) => { | ||
| if (result) { | ||
| return result; | ||
| } | ||
| const item = String(_item); | ||
| if (item === 'no-highlight' || item === 'nohighlight') { | ||
| return 'no-highlight'; | ||
| } | ||
| if (item.slice(0, 5) === 'lang-') { | ||
| return item.slice(5); | ||
| } | ||
| if (item.slice(0, 9) === 'language-') { | ||
| return item.slice(9); | ||
| } | ||
| return result; | ||
| }, ''); | ||
| return lowlightLanguageMappings[name || ''] || name; | ||
| } |
Obfuscated code
Supply chain riskObfuscated files are intentionally packed to hide their behavior. This could be a sign of malware.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
High entropy strings
Supply chain riskContains high entropy strings. This could be a sign of encrypted data, leaked secrets or obfuscated code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
3760833
6589.97%56
7.69%51726
4279.85%1
Infinity%8
Infinity%1
Infinity%