Skip to content

Commit 48ba087

Browse files
committed
fix(mobile): link chat URLs that have a port or a dotless host
1 parent e5a46d6 commit 48ba087

2 files changed

Lines changed: 102 additions & 1 deletion

File tree

‎apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,43 @@ function decorateMentionRuns(runs: ReadonlyArray<NativeMarkdownTextRun>) {
412412
});
413413
}
414414

415+
const WEB_URL_REGEX = /(^|[^\p{L}\p{N}])(https?:\/\/[\p{L}\p{N}_-][^\s<>]*)/gu;
416+
417+
function trimUrlTrailingPunctuation(url: string): string {
418+
let trimmed = url;
419+
while (true) {
420+
const unbalancedParen =
421+
trimmed.endsWith(")") && trimmed.split(")").length > trimmed.split("(").length;
422+
if (!unbalancedParen && !/[?!.,:*_~'"]$/.test(trimmed)) return trimmed;
423+
trimmed = trimmed.slice(0, -1);
424+
}
425+
}
426+
427+
function decorateUrlRuns(runs: ReadonlyArray<NativeMarkdownTextRun>) {
428+
return runs.flatMap((run) => {
429+
if (run.code || run.href || run.skillName || run.role === "code-block") return [run];
430+
const decorated: NativeMarkdownTextRun[] = [];
431+
let cursor = 0;
432+
for (const match of run.text.matchAll(WEB_URL_REGEX)) {
433+
const start = match.index + (match[1]?.length ?? 0);
434+
const url = trimUrlTrailingPunctuation(match[2] ?? "");
435+
const presentation = resolveMarkdownLinkPresentation(url);
436+
if (presentation.kind !== "external") continue;
437+
if (start > cursor) decorated.push({ ...run, text: run.text.slice(cursor, start) });
438+
decorated.push({
439+
...run,
440+
text: url,
441+
href: presentation.href,
442+
externalHost: presentation.host,
443+
});
444+
cursor = start + url.length;
445+
}
446+
if (cursor === 0) return [run];
447+
if (cursor < run.text.length) decorated.push({ ...run, text: run.text.slice(cursor) });
448+
return decorated;
449+
});
450+
}
451+
415452
function appendChildren(
416453
runs: NativeMarkdownTextRun[],
417454
node: MarkdownNode,
@@ -960,5 +997,5 @@ export function nativeMarkdownDocumentRuns(
960997
runs[lastIndex] = { ...last, text };
961998
}
962999
}
963-
return decorateMentionRuns(decorateSkillRuns(runs, skills));
1000+
return decorateMentionRuns(decorateSkillRuns(decorateUrlRuns(runs), skills));
9641001
}

‎apps/mobile/src/lib/nativeMarkdownText.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,70 @@ describe("nativeMarkdownDocumentRuns", () => {
285285
]);
286286
});
287287

288+
it("links web URLs whose host has no dot", () => {
289+
const runs = nativeMarkdownDocumentRuns({
290+
type: "document",
291+
children: [
292+
{
293+
type: "paragraph",
294+
children: [
295+
{
296+
type: "text",
297+
content: "Open http://localhost:3000/path?x=1. (Or https://devbox:8080)",
298+
},
299+
{ type: "code_inline", content: "http://localhost:5173" },
300+
],
301+
},
302+
],
303+
});
304+
expect(runs).toEqual([
305+
{ text: "Open ", role: "body" },
306+
{
307+
text: "http://localhost:3000/path?x=1",
308+
role: "body",
309+
href: "http://localhost:3000/path?x=1",
310+
externalHost: "localhost",
311+
},
312+
{ text: ". (Or ", role: "body" },
313+
{
314+
text: "https://devbox:8080",
315+
role: "body",
316+
href: "https://devbox:8080/",
317+
externalHost: "devbox",
318+
},
319+
{ text: ")", role: "body" },
320+
{ text: "http://localhost:5173", role: "body", code: true },
321+
]);
322+
});
323+
324+
it("links a bold web URL with a port", () => {
325+
const runs = nativeMarkdownDocumentRuns({
326+
type: "document",
327+
children: [
328+
{
329+
type: "paragraph",
330+
children: [
331+
{ type: "text", content: "Install from " },
332+
{
333+
type: "bold",
334+
children: [{ type: "text", content: "https://devbox.tail1234.ts.net:9443/" }],
335+
},
336+
],
337+
},
338+
],
339+
});
340+
expect(runs).toEqual([
341+
{ text: "Install from ", role: "body" },
342+
{
343+
text: "https://devbox.tail1234.ts.net:9443/",
344+
role: "body",
345+
bold: true,
346+
href: "https://devbox.tail1234.ts.net:9443/",
347+
externalHost: "devbox.tail1234.ts.net",
348+
},
349+
]);
350+
});
351+
288352
it("copies collapsed skill and file chips back to their original references", () => {
289353
expect(
290354
nativeMarkdownContextCopyRanges([

0 commit comments

Comments
 (0)