1/*
2 * Adapted from mdast-util-find-and-replace:
3 * https://github.com/syntax-tree/mdast-util-find-and-replace/blob/main/lib/index.js
4 *
5 * The MIT License
6 *
7 * Copyright (c) Titus Wormer <tituswormer@gmail.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28export type MarkdownNode = {
29 type: string;
30 value?: string;
31 children?: MarkdownNode[];
32 url?: string;
33 data?: unknown;
34};
35
36export type TextMatch = {
37 index: number;
38 input: string;
39};
40
41/** The dependency-free subset of mdast-util-find-and-replace used by PR autolinks. */
42export function findAndReplaceText(
43 tree: MarkdownNode,
44 find: RegExp,
45 replace: (matched: string, match: TextMatch) => MarkdownNode | false,
46 ignoredTypes: ReadonlySet<string>,
47): void {
48 visit(tree);
49
50 function visit(node: MarkdownNode): void {
51 if (node.children === undefined) return;
52 for (let childIndex = 0; childIndex < node.children.length; childIndex += 1) {
53 const child = node.children[childIndex]!;
54 if (ignoredTypes.has(child.type)) continue;
55 if (child.type !== "text" || child.value === undefined) {
56 visit(child);
57 continue;
58 }
59
60 const replacements: MarkdownNode[] = [];
61 let start = 0;
62 let changed = false;
63 find.lastIndex = 0;
64 let match = find.exec(child.value);
65 while (match !== null) {
66 const position = match.index;
67 const replacement = replace(match[0], { index: position, input: match.input });
68 if (replacement === false) {
69 find.lastIndex = position + 1;
70 } else {
71 if (start < position) {
72 replacements.push({ type: "text", value: child.value.slice(start, position) });
73 }
74 replacements.push(replacement);
75 start = position + match[0].length;
76 changed = true;
77 }
78 if (!find.global) break;
79 match = find.exec(child.value);
80 }
81
82 if (!changed) continue;
83 if (start < child.value.length) {
84 replacements.push({ type: "text", value: child.value.slice(start) });
85 }
86 node.children.splice(childIndex, 1, ...replacements);
87 childIndex += replacements.length - 1;
88 }
89 }
90}