| 45 | } |
| 46 | |
| 47 | function splitTextWithHighlight( |
| 48 | value: string, |
| 49 | pattern: RegExp, |
| 50 | bgColor: string |
| 51 | ): rendererNode[] { |
| 52 | const out: rendererNode[] = []; |
| 53 | let lastIndex = 0; |
| 54 | for (const m of value.matchAll(pattern)) { |
| 55 | const start = m.index ?? 0; |
| 56 | if (start > lastIndex) { |
| 57 | out.push({ type: "text", value: value.slice(lastIndex, start) }); |
| 58 | } |
| 59 | out.push({ |
| 60 | type: "element", |
| 61 | tagName: "span", |
| 62 | properties: { |
| 63 | style: { |
| 64 | backgroundColor: bgColor, |
| 65 | borderRadius: 2, |
| 66 | paddingInline: 4 |
| 67 | }, |
| 68 | className: ["inline-highlight"] |
| 69 | }, |
| 70 | children: [{ type: "text", value: m[0] }] |
| 71 | }); |
| 72 | lastIndex = start + m[0].length; |
| 73 | } |
| 74 | if (lastIndex < value.length) { |
| 75 | out.push({ type: "text", value: value.slice(lastIndex) }); |
| 76 | } |
| 77 | return out; |
| 78 | } |
| 79 | |
| 80 | function rewriteNode( |
| 81 | node: rendererNode, |