| 137 | // TODO: make this customizable |
| 138 | const tokenRegex = /(?<=\s|^)(https?:\/\/)?(www\.)?([^/\s]+\.[a-z]{2,}(\/\S+)?)(?=\s)/g; |
| 139 | function 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 | |
| 164 | interface UrlTokenValue { |
| 165 | type: 'url'; |