Skip to content

Commit 3f8d57f

Browse files
committed
fix(web): prevent long links from creating per-character elements
1 parent dac8b11 commit 3f8d57f

2 files changed

Lines changed: 54 additions & 11 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { expect, it } from "vite-plus/test";
2+
3+
import { render } from "../../../test/render";
4+
import "../../index.css";
5+
6+
import { MarkdownExternalLinkContent } from "./MarkdownExternalLink";
7+
8+
it.each(["plain", "formatted"] as const)(
9+
"keeps %s long links bounded in the DOM without losing text or wrapping",
10+
async (kind) => {
11+
const link = (url: string) => (
12+
<div className="chat-markdown w-64">
13+
<table>
14+
<tbody>
15+
<tr>
16+
<td>
17+
<a href={url}>
18+
<MarkdownExternalLinkContent
19+
host="github.com"
20+
plainText={kind === "plain" ? url : null}
21+
>
22+
{url}
23+
{kind === "formatted" ? <strong> details</strong> : null}
24+
</MarkdownExternalLinkContent>
25+
</a>
26+
</td>
27+
</tr>
28+
</tbody>
29+
</table>
30+
</div>
31+
);
32+
const url = (length: number) => `https://github.com/search?q=${"a".repeat(length)}`;
33+
await using screen = await render(link(url(128)));
34+
const initialElementCount = screen.container.querySelectorAll("a *").length;
35+
36+
const longUrl = url(16_000);
37+
await screen.rerender(link(longUrl));
38+
const anchor = screen.container.querySelector("a")!;
39+
const table = screen.container.querySelector("table")!;
40+
expect(anchor.getAttribute("href")).toBe(longUrl);
41+
expect(anchor.textContent).toBe(longUrl + (kind === "formatted" ? " details" : ""));
42+
expect(anchor.querySelectorAll("*")).toHaveLength(initialElementCount);
43+
expect(table.scrollWidth).toBeLessThanOrEqual(screen.container.firstElementChild!.clientWidth);
44+
},
45+
);

‎apps/web/src/components/chat/MarkdownExternalLink.tsx‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { faviconUrlForOrigin } from "@t3tools/shared/favicon";
22
import { GlobeIcon } from "lucide-react";
3-
import { Children, Fragment, memo, useState, type ReactNode } from "react";
3+
import { Children, memo, useState, type ReactNode } from "react";
44

55
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
66

@@ -54,18 +54,16 @@ function leadingExternalLinkTextLength(text: string): number {
5454
return Math.min(text.length, 1);
5555
}
5656

57-
// Long words such as URLs can break after any character so they fit narrow
58-
// table columns. Short ones, like a PR number, stay whole.
57+
// Keep short labels such as PR numbers intact in narrow table columns.
5958
function breakableExternalLinkText(text: string): ReactNode[] {
6059
return Array.from(text.matchAll(/\s+|\S+/g), ({ 0: word, index: start }) =>
61-
word.length <= 16
62-
? word
63-
: Array.from(word, (character, offset) => (
64-
<Fragment key={`${start + offset}:${character}`}>
65-
{character}
66-
<wbr />
67-
</Fragment>
68-
)),
60+
word.length <= 16 ? (
61+
word
62+
) : (
63+
<span key={start} className="[overflow-wrap:anywhere]">
64+
{word}
65+
</span>
66+
),
6967
);
7068
}
7169

0 commit comments

Comments
 (0)