MCPcopy Create free account
hub / github.com/adobe/react-spectrum / tokenizeURLs

Function tokenizeURLs

packages/@react-spectrum/ai/src/PromptField.tsx:139–162  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

137// TODO: make this customizable
138const tokenRegex = /(?<=\s|^)(https?:\/\/)?(www\.)?([^/\s]+\.[a-z]{2,}(\/\S+)?)(?=\s)/g;
139function tokenizeURLs(text: string): TokenFieldSegment[] {
140 if (text.length === 0) {
141 return [{type: 'text', text}];
142 }
143
144 tokenRegex.lastIndex = 0;
145
146 let match: RegExpExecArray | null = null;
147 let start = 0;
148 let segments: TokenFieldSegment[] = [];
149 while ((match = tokenRegex.exec(text))) {
150 if (match.index > start) {
151 segments.push({type: 'text', text: text.slice(start, match.index)});
152 }
153 segments.push({type: 'token', text: match[3], value: {type: 'url', url: match[0]}});
154 start = match.index + match[0].length;
155 }
156
157 if (start < text.length) {
158 segments.push({type: 'text', text: text.slice(start)});
159 }
160
161 return segments;
162}
163
164interface UrlTokenValue {
165 type: 'url';

Callers 1

tokenizeMethod · 0.85

Calls 2

pushMethod · 0.80
sliceMethod · 0.80

Tested by

no test coverage detected