| ... | ... | @@ -322,13 +322,76 @@ export function extractInlineCodeSpans(text: string): string[] { |
| 322 | 322 | return spans; |
| 323 | 323 | } |
| 324 | 324 | |
| 325 | | const MARKDOWN_LINK_HREF_PATTERN = |
| 326 | | /\[[^\]]*]\(\s*(?:<([^>\n]+)>|([^\s)]+))(?:\s+["'][^"']*["'])?\s*\)/g; |
| 325 | /** Reuses delimiter searches when successive candidates fall in the same run. */ |
| 326 | function markdownRunEndFinder(markdown: string, delimiter: RegExp) { |
| 327 | let runStart = -1; |
| 328 | let runEnd = -1; |
| 329 | return (start: number) => { |
| 330 | if (start >= runStart && start <= runEnd) return runEnd; |
| 331 | delimiter.lastIndex = start; |
| 332 | runStart = start; |
| 333 | runEnd = delimiter.exec(markdown)?.index ?? markdown.length; |
| 334 | return runEnd; |
| 335 | }; |
| 336 | } |
| 337 | |
| 338 | function markdownLinkTailScanner(markdown: string) { |
| 339 | const whitespaceEnd = markdownRunEndFinder(markdown, /\S/g); |
| 340 | const titleEnd = markdownRunEndFinder(markdown, /["']/g); |
| 341 | const titleWhitespaceEnd = markdownRunEndFinder(markdown, /\S/g); |
| 342 | return (start: number) => { |
| 343 | const end = whitespaceEnd(start); |
| 344 | if (markdown[end] === ")") return end + 1; |
| 345 | if (end === start || (markdown[end] !== '"' && markdown[end] !== "'")) return null; |
| 346 | const quoteEnd = titleEnd(end + 1); |
| 347 | if (quoteEnd === markdown.length) return null; |
| 348 | const close = titleWhitespaceEnd(quoteEnd + 1); |
| 349 | return markdown[close] === ")" ? close + 1 : null; |
| 350 | }; |
| 351 | } |
| 352 | |
| 353 | function markdownLinkDestinationScanner(markdown: string) { |
| 354 | const whitespaceEnd = markdownRunEndFinder(markdown, /\S/g); |
| 355 | const angleEnd = markdownRunEndFinder(markdown, /[>\n]/g); |
| 356 | const bareEnd = markdownRunEndFinder(markdown, /[\s)]/g); |
| 357 | // Separate caches keep each branch's searches moving forward even when an |
| 358 | // angle destination fails and falls back to an ordinary destination. |
| 359 | const angleTail = markdownLinkTailScanner(markdown); |
| 360 | const bareTail = markdownLinkTailScanner(markdown); |
| 361 | return (start: number) => { |
| 362 | if (markdown[start] !== "(") return null; |
| 363 | const hrefStart = whitespaceEnd(start + 1); |
| 364 | if (markdown[hrefStart] === "<") { |
| 365 | const hrefEnd = angleEnd(hrefStart + 1); |
| 366 | if (hrefEnd > hrefStart + 1 && markdown[hrefEnd] === ">") { |
| 367 | const end = angleTail(hrefEnd + 1); |
| 368 | if (end !== null) return { hrefStart: hrefStart + 1, hrefEnd, end }; |
| 369 | } |
| 370 | } |
| 371 | const hrefEnd = bareEnd(hrefStart); |
| 372 | if (hrefEnd === hrefStart) return null; |
| 373 | const end = bareTail(hrefEnd); |
| 374 | return end === null ? null : { hrefStart, hrefEnd, end }; |
| 375 | }; |
| 376 | } |
| 327 | 377 | |
| 328 | 378 | export function extractMarkdownLinkHrefs(markdown: string): string[] { |
| 329 | 379 | const hrefs: string[] = []; |
| 330 | | for (const match of markdown.matchAll(MARKDOWN_LINK_HREF_PATTERN)) { |
| 331 | | const href = (match[1] ?? match[2])?.trim(); |
| 380 | const scanDestination = markdownLinkDestinationScanner(markdown); |
| 381 | let cursor = 0; |
| 382 | while (cursor < markdown.length) { |
| 383 | const labelStart = markdown.indexOf("[", cursor); |
| 384 | if (labelStart === -1) break; |
| 385 | const labelEnd = markdown.indexOf("]", labelStart + 1); |
| 386 | if (labelEnd === -1) break; |
| 387 | |
| 388 | // Nested opening brackets share this closing bracket. On failure, skip the |
| 389 | // whole label instead of rescanning its suffix from each opening bracket. |
| 390 | cursor = labelEnd + 1; |
| 391 | const destination = scanDestination(cursor); |
| 392 | if (!destination) continue; |
| 393 | cursor = destination.end; |
| 394 | const href = markdown.slice(destination.hrefStart, destination.hrefEnd).trim(); |
| 332 | 395 | if (href) hrefs.push(href); |
| 333 | 396 | } |
| 334 | 397 | return hrefs; |