(text: string, pattern: RegExp, buildNode: NodeBuilder)
| 66 | } |
| 67 | |
| 68 | function splitByPattern(text: string, pattern: RegExp, buildNode: NodeBuilder) { |
| 69 | // Reset lastIndex — the pattern is reused across text nodes with the `g` flag |
| 70 | pattern.lastIndex = 0; |
| 71 | // biome-ignore lint/suspicious/noExplicitAny: building mdast-compatible nodes |
| 72 | const parts: any[] = []; |
| 73 | let lastIndex = 0; |
| 74 | let match: RegExpExecArray | null = null; |
| 75 | |
| 76 | while (true) { |
| 77 | match = pattern.exec(text); |
| 78 | if (!match) { |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | if (match.index > lastIndex) { |
| 83 | parts.push({ type: "text", value: text.slice(lastIndex, match.index) }); |
| 84 | } |
| 85 | |
| 86 | const result = normalizeBuildNodeResult(buildNode(match[0])); |
| 87 | parts.push(result.node); |
| 88 | if (result.trailing) { |
| 89 | parts.push({ type: "text", value: result.trailing }); |
| 90 | } |
| 91 | |
| 92 | lastIndex = match.index + match[0].length; |
| 93 | } |
| 94 | |
| 95 | if (parts.length === 0) { |
| 96 | return [{ type: "text", value: text }]; |
| 97 | } |
| 98 | |
| 99 | if (lastIndex < text.length) { |
| 100 | parts.push({ type: "text", value: text.slice(lastIndex) }); |
| 101 | } |
| 102 | |
| 103 | return parts; |
| 104 | } |
| 105 | |
| 106 | function normalizeBuildNodeResult(result: NodeBuilderResult): { |
| 107 | node: Node; |
no test coverage detected