Skip to content

Commit 2edaf70

Browse files
fix(ui): humanize bare GitHub item links
1 parent edb7a16 commit 2edaf70

5 files changed

Lines changed: 70 additions & 41 deletions

File tree

‎ui/src/components/github-link-hovercard.ts‎

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ControlUiGitHubPreview } from "../../../src/gateway/control-ui-con
55
import type { GatewayBrowserClient } from "../api/gateway.ts";
66
import { i18n, t } from "../i18n/index.ts";
77
import { formatRelativeTimestamp } from "../lib/format.ts";
8+
import { parseGitHubItemPath, type GitHubItemTarget } from "./github-link-target.ts";
89

910
const GITHUB_HOST = "github.com";
1011
const OPEN_DELAY_MS = 250;
@@ -14,14 +15,8 @@ const CACHE_LIMIT = 100;
1415
const VIEWPORT_PADDING = 12;
1516
const CARD_GAP = 10;
1617

17-
type GitHubLinkKind = "issue" | "pull";
18-
19-
type GitHubLinkTarget = {
18+
type GitHubLinkTarget = GitHubItemTarget & {
2019
href: string;
21-
kind: GitHubLinkKind;
22-
number: number;
23-
owner: string;
24-
repo: string;
2520
};
2621

2722
type GitHubPreview = GitHubLinkTarget & ControlUiGitHubPreview;
@@ -59,15 +54,6 @@ function optionalNumber(record: Record<string, unknown>, key: string): number |
5954
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
6055
}
6156

62-
function decodePathSegment(value: string): string | null {
63-
try {
64-
const decoded = decodeURIComponent(value).trim();
65-
return decoded && decoded !== "." && decoded !== ".." ? decoded : null;
66-
} catch {
67-
return null;
68-
}
69-
}
70-
7157
function parseGitHubIssueOrPullRequestLink(href: string): GitHubLinkTarget | null {
7258
let url: URL;
7359
try {
@@ -81,19 +67,8 @@ function parseGitHubIssueOrPullRequestLink(href: string): GitHubLinkTarget | nul
8167
if (url.username || url.password || (url.port && url.port !== "443")) {
8268
return null;
8369
}
84-
const segments = url.pathname.split("/").filter(Boolean);
85-
const owner = decodePathSegment(segments[0] ?? "");
86-
const repo = decodePathSegment(segments[1] ?? "");
87-
const surface = segments[2];
88-
const numberText = segments[3] ?? "";
89-
if (!owner || !repo || !/^[1-9]\d{0,9}$/.test(numberText)) {
90-
return null;
91-
}
92-
const kind = surface === "issues" ? "issue" : surface === "pull" ? "pull" : null;
93-
if (!kind) {
94-
return null;
95-
}
96-
return { href: url.href, kind, number: Number(numberText), owner, repo };
70+
const target = parseGitHubItemPath(url);
71+
return target ? { ...target, href: url.href } : null;
9772
}
9873

9974
export function isGitHubPullRequestLink(href: string): boolean {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export type GitHubItemTarget = {
2+
kind: "issue" | "pull";
3+
number: number;
4+
owner: string;
5+
repo: string;
6+
};
7+
8+
function decodePathSegment(value: string): string | null {
9+
try {
10+
const decoded = decodeURIComponent(value).trim();
11+
return decoded && decoded !== "." && decoded !== ".." ? decoded : null;
12+
} catch {
13+
return null;
14+
}
15+
}
16+
17+
export function parseGitHubItemPath(url: URL): GitHubItemTarget | null {
18+
const segments = url.pathname.split("/").filter(Boolean);
19+
const owner = decodePathSegment(segments[0] ?? "");
20+
const repo = decodePathSegment(segments[1] ?? "");
21+
const surface = segments[2];
22+
const numberText = segments[3] ?? "";
23+
if (!owner || !repo || !/^[1-9]\d{0,9}$/.test(numberText)) {
24+
return null;
25+
}
26+
const kind = surface === "issues" ? "issue" : surface === "pull" ? "pull" : null;
27+
return kind ? { kind, number: Number(numberText), owner, repo } : null;
28+
}
29+
30+
export function formatGitHubItemReference(target: GitHubItemTarget): string {
31+
return `${target.owner}/${target.repo}#${target.number}`;
32+
}

‎ui/src/components/markdown-links.test.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -478,10 +478,11 @@ describe("toSanitizedMarkdownHtml links", () => {
478478

479479
describe("github link marks", () => {
480480
it.each([
481+
["bare autolink", "https://github.com/openclaw/openclaw/pull/3434", "openclaw/openclaw#3434"],
481482
[
482-
"bare autolink",
483-
"https://github.com/openclaw/openclaw/pull/3434",
484-
"https://github.com/openclaw/openclaw/pull/3434",
483+
"bare issue autolink",
484+
"https://github.com/openclaw/openclaw/issues/3435",
485+
"openclaw/openclaw#3435",
485486
],
486487
["issue shorthand", "[#3434](https://github.com/openclaw/openclaw/pull/3434)", "#3434"],
487488
["labelled link", "[the fix](https://github.com/openclaw/openclaw/pull/3434)", "the fix"],
@@ -492,8 +493,6 @@ describe("toSanitizedMarkdownHtml links", () => {
492493
const fragment = htmlFragment(toSanitizedMarkdownHtml(input));
493494
const link = fragment.querySelector<HTMLAnchorElement>("a");
494495
expect(link?.classList.contains("markdown-github-link")).toBe(true);
495-
// The mark is CSS-only: the anchor keeps its authored text so copied text
496-
// and screen-reader output stay unchanged.
497496
expect(link?.textContent).toBe(expectedText);
498497
});
499498

‎ui/src/components/markdown-parser.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import markdownItTaskLists from "markdown-it-task-lists";
33
import type Token from "markdown-it/lib/token.mjs";
44
import { t } from "../i18n/index.ts";
55
import { fileKindForPath, shortestFileLabels } from "./file-kind.ts";
6+
import { formatGitHubItemReference, parseGitHubItemPath } from "./github-link-target.ts";
67
import {
78
installAssistantTranscriptRoleImageRenderer,
89
installAssistantTranscriptRoleMarkdown,
@@ -473,13 +474,17 @@ export function createMarkdownParser(): MarkdownIt {
473474
if (!url) {
474475
continue;
475476
}
476-
if (open.markup === "linkify" || open.markup === "autolink") {
477+
const generatedUrlLabel = open.markup === "linkify" || open.markup === "autolink";
478+
const host = url.hostname.toLowerCase();
479+
const githubLink = host === "github.com" || host === "www.github.com";
480+
const itemTarget = githubLink ? parseGitHubItemPath(url) : null;
481+
if (generatedUrlLabel && !itemTarget) {
477482
open.attrJoin("class", BARE_URL_CLASS);
478483
}
479-
const host = url.hostname.toLowerCase();
480-
if (host !== "github.com" && host !== "www.github.com") {
484+
if (!githubLink) {
481485
continue;
482486
}
487+
let labelToken: Token | null = null;
483488
for (let cursor = index + 1; cursor < children.length; cursor++) {
484489
const token = children[cursor];
485490
if (!token || token.type === "link_close") {
@@ -490,9 +495,13 @@ export function createMarkdownParser(): MarkdownIt {
490495
token.content.trim() !== ""
491496
) {
492497
open.attrJoin("class", GITHUB_LINK_CLASS);
498+
labelToken = token;
493499
break;
494500
}
495501
}
502+
if (generatedUrlLabel && itemTarget && labelToken) {
503+
labelToken.content = formatGitHubItemReference(itemTarget);
504+
}
496505
}
497506
}
498507
});

‎ui/src/e2e/github-link-hovercard.e2e.test.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Control UI tests cover GitHub link hover card behavior.
2+
import { mkdir } from "node:fs/promises";
3+
import path from "node:path";
24
import { chromium, type Browser, type BrowserContext, type Locator } from "playwright";
35
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
46
import {
@@ -119,8 +121,8 @@ describeControlUiE2e("GitHub link hover cards", () => {
119121
{
120122
type: "text",
121123
text: [
122-
"Review [#99816](https://github.com/openclaw/openclaw/pull/99816),",
123-
"then [#99815](https://github.com/openclaw/openclaw/issues/99815).",
124+
"Review https://github.com/openclaw/openclaw/pull/99816,",
125+
"then https://github.com/openclaw/openclaw/issues/99815.",
124126
"A [missing item](https://github.com/openclaw/openclaw/issues/999999) stays usable.",
125127
"The [repository](https://github.com/openclaw/openclaw) has no item preview.",
126128
"Styling notes live in [the docs](https://docs.openclaw.ai/web/control-ui).",
@@ -134,7 +136,19 @@ describeControlUiE2e("GitHub link hover cards", () => {
134136
});
135137
await page.goto(`${server.baseUrl}chat`);
136138

137-
const pullLink = page.getByRole("link", { name: "#99816" });
139+
const message = page.locator(".chat-text").filter({ hasText: "Review" });
140+
const artifactDir = process.env.OPENCLAW_UI_E2E_ARTIFACT_DIR?.trim();
141+
if (artifactDir) {
142+
await mkdir(artifactDir, { recursive: true });
143+
await message.screenshot({ path: path.join(artifactDir, "github-references-light.png") });
144+
await page.emulateMedia({ colorScheme: "dark" });
145+
await expect.poll(() => page.locator("html").getAttribute("data-theme-mode")).toBe("dark");
146+
await message.screenshot({ path: path.join(artifactDir, "github-references-dark.png") });
147+
await page.emulateMedia({ colorScheme: "light" });
148+
await expect.poll(() => page.locator("html").getAttribute("data-theme-mode")).toBe("light");
149+
}
150+
151+
const pullLink = page.getByRole("link", { name: "openclaw/openclaw#99816" });
138152

139153
// The mark carries the link signal at rest, so the underline only returns on
140154
// hover. Non-GitHub links keep the base underline, which keeps the rule scoped.
@@ -160,7 +174,7 @@ describeControlUiE2e("GitHub link hover cards", () => {
160174
expect(pullBox!.x + pullBox!.width).toBeLessThanOrEqual(1180);
161175
expect(pullBox!.y + pullBox!.height).toBeLessThanOrEqual(800);
162176

163-
const issueLink = page.getByRole("link", { name: "#99815" });
177+
const issueLink = page.getByRole("link", { name: "openclaw/openclaw#99815" });
164178
await issueLink.hover();
165179
await expectText(card, "Keep hover previews compact");
166180
await expectText(card, "octocat");

0 commit comments

Comments
 (0)