(url: string, includeDataPrefix: boolean = true)
| 280 | * For non-data URLs, returns the original URL unchanged. |
| 281 | */ |
| 282 | export function stripDataUrlContent(url: string, includeDataPrefix: boolean = true): string { |
| 283 | if (url.startsWith('data:')) { |
| 284 | // Match the MIME type (everything after 'data:' until the first ';' or ',') |
| 285 | const match = url.match(/^data:([^;,]+)/); |
| 286 | const mimeType = match ? match[1] : 'text/plain'; |
| 287 | const isBase64 = url.includes(';base64,'); |
| 288 | |
| 289 | // Find where the actual data starts (after the comma) |
| 290 | const dataStart = url.indexOf(','); |
| 291 | let dataPrefix = ''; |
| 292 | if (includeDataPrefix && dataStart !== -1) { |
| 293 | const data = url.slice(dataStart + 1); |
| 294 | // Include first 10 chars of data to help identify content (e.g., magic bytes) |
| 295 | dataPrefix = data.length > 10 ? `${data.slice(0, 10)}... [truncated]` : data; |
| 296 | } |
| 297 | |
| 298 | return `data:${mimeType}${isBase64 ? ',base64' : ''}${dataPrefix ? `,${dataPrefix}` : ''}`; |
| 299 | } |
| 300 | return url; |
| 301 | } |
no test coverage detected