|
| 1 | +/** |
| 2 | + * Why this exists: the native parser behind SelectableMarkdownText |
| 3 | + * (react-native-nitro-markdown, a Nitro wrapper around MD4C) detects bare URLs |
| 4 | + * with MD4C's permissive-autolink scanner, which only accepts `/ . - _` inside |
| 5 | + * URL paths and requires every other non-alphanumeric to sit between |
| 6 | + * alphanumerics. A bare URL such as |
| 7 | + * `https://github.com/google/ax/compare/main...Wraient:patch-1?expand=1` |
| 8 | + * therefore links only up to `/compare/main` on mobile, while web/desktop |
| 9 | + * (remark-gfm) links it whole. |
| 10 | + * |
| 11 | + * Explicit `<url>` autolinks are core markdown and accept everything up to |
| 12 | + * whitespace, so rewriting bare URLs into that form before parsing closes the |
| 13 | + * gap without touching the vendored native parser. Rendering is unchanged: |
| 14 | + * an explicit autolink displays the URL itself, exactly like a bare autolink. |
| 15 | + */ |
| 16 | + |
| 17 | +// Code spans ride along untouched so `` `https://...` `` keeps rendering as |
| 18 | +// code instead of gaining visible angle brackets. |
| 19 | +const PROSE_OR_CODE_PATTERN = /(`+[^`]*?`+)|(https?:\/\/[^\s<>\]`"'*]+)/g; |
| 20 | +const FENCE_PATTERN = /^\s*(`{3,}|~{3,})/; |
| 21 | +// Trailing punctuation stripped from GFM autolinks; anything past it stays |
| 22 | +// plain text so parity with web holds (e.g. the `.` in "see https://x/y."). |
| 23 | +const TRAILING_PUNCTUATION_PATTERN = /[?!.,:*_~]+$/u; |
| 24 | + |
| 25 | +/** True when `index` sits inside an unclosed `<...>` on the line: an existing |
| 26 | + * autolink (`<https://...>`) or an HTML tag (`<a href="...">`). */ |
| 27 | +function isInsideAngleSegment(text: string, index: number): boolean { |
| 28 | + return /<[^<>]*$/.test(text.slice(0, index)); |
| 29 | +} |
| 30 | + |
| 31 | +/** Drop GFM trailing punctuation and unbalanced `)` from a matched URL. */ |
| 32 | +function trimTrailingPunctuation(url: string): { url: string; tail: string } { |
| 33 | + const withoutPunct = url.replace(TRAILING_PUNCTUATION_PATTERN, ""); |
| 34 | + let open = 0; |
| 35 | + for (const char of withoutPunct) { |
| 36 | + if (char === "(") open += 1; |
| 37 | + else if (char === ")") open -= 1; |
| 38 | + } |
| 39 | + let end = withoutPunct.length; |
| 40 | + while (end > 0 && withoutPunct[end - 1] === ")" && open < 0) { |
| 41 | + open += 1; |
| 42 | + end -= 1; |
| 43 | + } |
| 44 | + return { url: withoutPunct.slice(0, end), tail: url.slice(end) }; |
| 45 | +} |
| 46 | + |
| 47 | +function wrapBareUrlsInProse(prose: string): string { |
| 48 | + return prose.replace( |
| 49 | + PROSE_OR_CODE_PATTERN, |
| 50 | + ( |
| 51 | + match: string, |
| 52 | + code: string | undefined, |
| 53 | + url: string | undefined, |
| 54 | + offset: number, |
| 55 | + whole: string, |
| 56 | + ) => { |
| 57 | + if (code !== undefined || url === undefined) return match; |
| 58 | + if (isInsideAngleSegment(whole, offset)) return match; |
| 59 | + const { url: trimmed, tail } = trimTrailingPunctuation(url); |
| 60 | + if (trimmed.endsWith("://")) return match; |
| 61 | + return `<${trimmed}>${tail}`; |
| 62 | + }, |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Rewrite bare `http(s)://` URLs as explicit `<url>` autolinks so the native |
| 68 | + * parser links them whole. Leaves fenced code blocks, inline code spans, and |
| 69 | + * existing `<...>` segments untouched. |
| 70 | + */ |
| 71 | +export function wrapBareUrlsForNativeParser(markdown: string): string { |
| 72 | + let inFence = false; |
| 73 | + return markdown |
| 74 | + .split("\n") |
| 75 | + .map((line) => { |
| 76 | + if (FENCE_PATTERN.test(line)) { |
| 77 | + inFence = !inFence; |
| 78 | + return line; |
| 79 | + } |
| 80 | + if (inFence) return line; |
| 81 | + return wrapBareUrlsInProse(line); |
| 82 | + }) |
| 83 | + .join("\n"); |
| 84 | +} |
0 commit comments