(output: string)
| 1282 | // Uses indexOf instead of a global regex to avoid O(n²) backtracking when the |
| 1283 | // input contains many '![' sequences (polynomial ReDoS with the g flag). |
| 1284 | export const removeInvalidImageMarkdown = (output: string): string => { |
| 1285 | if (typeof output !== 'string') return output |
| 1286 | let result = '' |
| 1287 | let pos = 0 |
| 1288 | while (pos < output.length) { |
| 1289 | const start = output.indexOf('![', pos) |
| 1290 | if (start === -1) { |
| 1291 | result += output.slice(pos) |
| 1292 | break |
| 1293 | } |
| 1294 | result += output.slice(pos, start) |
| 1295 | const closeBracket = output.indexOf(']', start + 2) |
| 1296 | if (closeBracket === -1 || output[closeBracket + 1] !== '(') { |
| 1297 | result += '![' |
| 1298 | pos = start + 2 |
| 1299 | continue |
| 1300 | } |
| 1301 | const closeParen = output.indexOf(')', closeBracket + 2) |
| 1302 | if (closeParen === -1) { |
| 1303 | result += output.slice(start) |
| 1304 | break |
| 1305 | } |
| 1306 | const url = output.slice(closeBracket + 2, closeParen) |
| 1307 | if (/^https?:\/\//.test(url)) result += output.slice(start, closeParen + 1) |
| 1308 | pos = closeParen + 1 |
| 1309 | } |
| 1310 | return result |
| 1311 | } |
| 1312 | |
| 1313 | /** |
| 1314 | * Extract output from array |
no test coverage detected