Skip to content

Commit 5ef065c

Browse files
committed
fix(mobile): trim trailing semicolons from chat URLs
1 parent 48ba087 commit 5ef065c

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,20 @@ function decorateMentionRuns(runs: ReadonlyArray<NativeMarkdownTextRun>) {
415415
const WEB_URL_REGEX = /(^|[^\p{L}\p{N}])(https?:\/\/[\p{L}\p{N}_-][^\s<>]*)/gu;
416416

417417
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);
418+
let open = 0;
419+
let close = 0;
420+
for (const char of url) {
421+
if (char === "(") open += 1;
422+
else if (char === ")") close += 1;
424423
}
424+
let end = url.length;
425+
while (end > 0) {
426+
const char = url.charAt(end - 1);
427+
if (char === ")" && close > open) close -= 1;
428+
else if (!`?!.,:;*_~'"`.includes(char)) break;
429+
end -= 1;
430+
}
431+
return url.slice(0, end);
425432
}
426433

427434
function decorateUrlRuns(runs: ReadonlyArray<NativeMarkdownTextRun>) {

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,40 @@ describe("nativeMarkdownDocumentRuns", () => {
321321
]);
322322
});
323323

324+
it("keeps trailing semicolons and unmatched parentheses out of a web URL", () => {
325+
const runs = nativeMarkdownDocumentRuns({
326+
type: "document",
327+
children: [
328+
{
329+
type: "paragraph",
330+
children: [
331+
{
332+
type: "text",
333+
content: "Try http://localhost:3000; see http://devbox:8080/A_(b)). Done",
334+
},
335+
],
336+
},
337+
],
338+
});
339+
expect(runs).toEqual([
340+
{ text: "Try ", role: "body" },
341+
{
342+
text: "http://localhost:3000",
343+
role: "body",
344+
href: "http://localhost:3000/",
345+
externalHost: "localhost",
346+
},
347+
{ text: "; see ", role: "body" },
348+
{
349+
text: "http://devbox:8080/A_(b)",
350+
role: "body",
351+
href: "http://devbox:8080/A_(b)",
352+
externalHost: "devbox",
353+
},
354+
{ text: "). Done", role: "body" },
355+
]);
356+
});
357+
324358
it("links a bold web URL with a port", () => {
325359
const runs = nativeMarkdownDocumentRuns({
326360
type: "document",

0 commit comments

Comments
 (0)