Skip to content

Commit dc262b3

Browse files
Wraientclaude
andcommitted
fix(mobile): link bare URLs containing ... or : in full
Bare URLs on mobile are detected by MD4C's permissive-autolink scanner, which only accepts / . - _ in paths, so a URL like .../compare/main...Wraient:patch-1?expand=1 links only up to /compare/main while desktop (remark-gfm) links it whole. Rewrite bare http(s) URLs as explicit <url> autolinks before handing markdown to the native parser. Rendering is unchanged and fenced code, inline code, existing autolinks, and HTML tags are left alone. Co-Authored-By: Claude Code <[email protected]>
1 parent b5a0f81 commit dc262b3

3 files changed

Lines changed: 152 additions & 1 deletion

File tree

‎apps/mobile/modules/t3-markdown-text/src/SelectableMarkdownText.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
nativeMarkdownDocumentRuns,
99
nativeMarkdownWithPreservedSoftBreaks,
1010
} from "./nativeMarkdownText";
11+
import { wrapBareUrlsForNativeParser } from "./bareUrlAutolinks";
1112
import { MarkdownImageRendererContext, NativeMarkdownBlock } from "./NativeMarkdownBlock";
1213
import {
1314
MarkdownContextClipboardContext,
@@ -51,7 +52,7 @@ export function SelectableMarkdownText({
5152
marginBottom = 0,
5253
}: SelectableMarkdownTextProps) {
5354
const chunks = useMemo(() => {
54-
const parsedDocument = parseMarkdownWithOptions(markdown, {
55+
const parsedDocument = parseMarkdownWithOptions(wrapBareUrlsForNativeParser(markdown), {
5556
gfm: true,
5657
html: true,
5758
math: false,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { wrapBareUrlsForNativeParser } from "./bareUrlAutolinks";
4+
5+
describe("wrapBareUrlsForNativeParser", () => {
6+
it("wraps the full compare URL that MD4C truncates at `...`", () => {
7+
const url = "https://github.com/google/ax/compare/main...Wraient:patch-1?expand=1";
8+
expect(wrapBareUrlsForNativeParser(`see ${url} thanks`)).toBe(`see <${url}> thanks`);
9+
});
10+
11+
it("wraps plain bare URLs", () => {
12+
expect(wrapBareUrlsForNativeParser("see https://example.com/a/b?c=1 nice")).toBe(
13+
"see <https://example.com/a/b?c=1> nice",
14+
);
15+
});
16+
17+
it("keeps GFM trailing punctuation outside the brackets", () => {
18+
expect(wrapBareUrlsForNativeParser("see https://example.com/a.")).toBe(
19+
"see <https://example.com/a>.",
20+
);
21+
expect(wrapBareUrlsForNativeParser("see (https://example.com/a), ok")).toBe(
22+
"see (<https://example.com/a>), ok",
23+
);
24+
expect(wrapBareUrlsForNativeParser("see https://example.com/a?")).toBe(
25+
"see <https://example.com/a>?",
26+
);
27+
});
28+
29+
it("keeps balanced parens but drops unbalanced ones", () => {
30+
expect(wrapBareUrlsForNativeParser("(see https://en.wikipedia.org/wiki/Link_(film))")).toBe(
31+
"(see <https://en.wikipedia.org/wiki/Link_(film)>)",
32+
);
33+
});
34+
35+
it("leaves fenced code blocks alone", () => {
36+
const input = "```\nhttps://example.com/a...b:c?d=1\n```";
37+
expect(wrapBareUrlsForNativeParser(input)).toBe(input);
38+
});
39+
40+
it("leaves inline code spans alone", () => {
41+
expect(wrapBareUrlsForNativeParser("run `https://example.com/a...b:c?d=1` now")).toBe(
42+
"run `https://example.com/a...b:c?d=1` now",
43+
);
44+
});
45+
46+
it("leaves existing autolinks and HTML tags alone", () => {
47+
expect(wrapBareUrlsForNativeParser("see <https://example.com/a...b:c?d=1> ok")).toBe(
48+
"see <https://example.com/a...b:c?d=1> ok",
49+
);
50+
expect(wrapBareUrlsForNativeParser('<a href="https://example.com/a...b:c?d=1">x</a>')).toBe(
51+
'<a href="https://example.com/a...b:c?d=1">x</a>',
52+
);
53+
});
54+
55+
it("wraps link destinations, which stays valid markdown", () => {
56+
expect(wrapBareUrlsForNativeParser("[pr](https://example.com/a...b:c?d=1)")).toBe(
57+
"[pr](<https://example.com/a...b:c?d=1>)",
58+
);
59+
});
60+
61+
it("leaves text without URLs, emails, and bare schemes alone", () => {
62+
expect(wrapBareUrlsForNativeParser("no links here")).toBe("no links here");
63+
expect(wrapBareUrlsForNativeParser("mail [email protected]")).toBe("mail [email protected]");
64+
expect(wrapBareUrlsForNativeParser("see https:// ok")).toBe("see https:// ok");
65+
});
66+
});
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)