MCPcopy Create free account
hub / github.com/block/buzz / splitByPattern

Function splitByPattern

desktop/src/shared/lib/createRemarkPrefixPlugin.ts:68–104  ·  view source on GitHub ↗
(text: string, pattern: RegExp, buildNode: NodeBuilder)

Source from the content-addressed store, hash-verified

66}
67
68function 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
106function normalizeBuildNodeResult(result: NodeBuilderResult): {
107 node: Node;

Callers 1

walkChildrenFunction · 0.70

Calls 3

normalizeBuildNodeResultFunction · 0.85
buildNodeFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected