Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

mdast-find-and-replace.ts

89 lines3.0 KB

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​
41export function findAndReplaceText(
42 tree: MarkdownNode,
43 find: RegExp,
44 replace: (matched: string, match: TextMatch) => MarkdownNode | false,
45 ignoredTypes: ReadonlySet<string>,
46): void {
47 visit(tree);
48​
49 function visit(node: MarkdownNode): void {
50 if (node.children === undefined) return;
51 for (let childIndex = 0; childIndex < node.children.length; childIndex += 1) {
52 const child = node.children[childIndex]!;
53 if (ignoredTypes.has(child.type)) continue;
54 if (child.type !== "text" || child.value === undefined) {
55 visit(child);
56 continue;
57 }
58​
59 const replacements: MarkdownNode[] = [];
60 let start = 0;
61 let changed = false;
62 find.lastIndex = 0;
63 let match = find.exec(child.value);
64 while (match !== null) {
65 const position = match.index;
66 const replacement = replace(match[0], { index: position, input: match.input });
67 if (replacement === false) {
68 find.lastIndex = position + 1;
69 } else {
70 if (start < position) {
71 replacements.push({ type: "text", value: child.value.slice(start, position) });
72 }
73 replacements.push(replacement);
74 start = position + match[0].length;
75 changed = true;
76 }
77 if (!find.global) break;
78 match = find.exec(child.value);
79 }
80​
81 if (!changed) continue;
82 if (start < child.value.length) {
83 replacements.push({ type: "text", value: child.value.slice(start) });
84 }
85 node.children.splice(childIndex, 1, ...replacements);
86 childIndex += replacements.length - 1;
87 }
88 }