Skip to content

Commit ac69406

Browse files
fix(ui): keep GitHub deep-link labels destination-specific (#124252)
Gate compact owner/repo#N label rewriting on exact issue/pull root URLs, so links with a trailing path segment, query, or fragment keep their full generated label instead of being collapsed to a generic item reference. Item identity parsing used by the hovercard preview and navigation is unchanged.
1 parent 69c2c06 commit ac69406

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ export function formatGitHubItemReference(target: GitHubItemTarget): string {
5656
return `${target.owner}/${target.repo}#${target.number}`;
5757
}
5858

59+
// Compaction to `owner/repo#N` is only safe when the URL names the generic
60+
// item root: any trailing path segment (/files, /commits, an issue comment
61+
// anchor) or query/fragment is a more specific destination than the compact
62+
// label communicates, even though `parseGitHubItemPath` still resolves an
63+
// identity for it (hovercards and navigation need that deep identity intact).
64+
export function isGitHubItemRootPath(url: URL): boolean {
65+
const segments = url.pathname.split("/").filter(Boolean);
66+
return segments.length === 4 && !url.search && !url.hash;
67+
}
68+
5969
export function githubLinkAnchorFromEvent(event: Event): HTMLAnchorElement | null {
6070
for (const candidate of event.composedPath()) {
6171
if (candidate instanceof HTMLAnchorElement) {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,23 @@ describe("toSanitizedMarkdownHtml links", () => {
509509
expect(link?.classList.contains("markdown-bare-url")).toBe(true);
510510
});
511511

512+
it.each([
513+
["a files-tab path", "https://github.com/openclaw/openclaw/pull/3434/files"],
514+
["a commits path", "https://github.com/openclaw/openclaw/pull/3434/commits"],
515+
[
516+
"an issue comment fragment",
517+
"https://github.com/openclaw/openclaw/issues/3434#issuecomment-1",
518+
],
519+
["a review comment query", "https://github.com/openclaw/openclaw/pull/3434?tab=files"],
520+
["a diff anchor", "https://github.com/openclaw/openclaw/pull/3434/files#diff-abc123"],
521+
])("keeps the specific destination visible for %s", (_kind, input) => {
522+
const fragment = htmlFragment(toSanitizedMarkdownHtml(input));
523+
const link = fragment.querySelector<HTMLAnchorElement>("a");
524+
expect(link?.classList.contains("markdown-github-link")).toBe(true);
525+
expect(link?.textContent).toBe(input);
526+
expect(link?.getAttribute("href")).toBe(input);
527+
});
528+
512529
it.each([
513530
["non-github host", "[docs](https://example.com/openclaw)"],
514531
["lookalike host", "[docs](https://notgithub.com/openclaw)"],

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ 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";
6+
import {
7+
formatGitHubItemReference,
8+
isGitHubItemRootPath,
9+
parseGitHubItemPath,
10+
} from "./github-link-target.ts";
711
import {
812
installAssistantTranscriptRoleImageRenderer,
913
installAssistantTranscriptRoleMarkdown,
@@ -514,7 +518,7 @@ export function createMarkdownParser(): MarkdownIt {
514518
break;
515519
}
516520
}
517-
if (generatedUrlLabel && itemTarget && labelToken) {
521+
if (generatedUrlLabel && itemTarget && labelToken && isGitHubItemRootPath(url)) {
518522
labelToken.content = formatGitHubItemReference(itemTarget);
519523
}
520524
}

0 commit comments

Comments
 (0)