Skip to content

Commit 5595c42

Browse files
committed
fix(shared): leave GitLab upload links in code as written
Fenced blocks and inline code spans show the author's text verbatim, so resolving a relative /uploads/ link there changed what a reader sees and copies.
1 parent 114857e commit 5595c42

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

‎packages/shared/src/gitlabMedia.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ describe("gitlabMedia", () => {
6464
`[ref]: /uploads/${SECRET}/ref.pdf`,
6565
`Prose mentioning /uploads/${SECRET}/x.png stays prose.`,
6666
`![elsewhere](https://cdn.example.com/uploads/${SECRET}/a.png)`,
67+
"```md",
68+
`![in a fence](/uploads/${SECRET}/fenced.png)`,
69+
"```",
70+
`Inline \`![code](/uploads/${SECRET}/inline-code.png)\` stays as written.`,
6771
].join("\n");
6872
expect(resolveGitLabUploadLinks(body, "https://gitlab.com/team/app/")).toBe(
6973
[
@@ -73,6 +77,10 @@ describe("gitlabMedia", () => {
7377
`[ref]: https://gitlab.com/team/app/uploads/${SECRET}/ref.pdf`,
7478
`Prose mentioning /uploads/${SECRET}/x.png stays prose.`,
7579
`![elsewhere](https://cdn.example.com/uploads/${SECRET}/a.png)`,
80+
"```md",
81+
`![in a fence](/uploads/${SECRET}/fenced.png)`,
82+
"```",
83+
`Inline \`![code](/uploads/${SECRET}/inline-code.png)\` stays as written.`,
7684
].join("\n"),
7785
);
7886
});

‎packages/shared/src/gitlabMedia.ts‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
const UPLOAD_PATH_PATTERN =
88
/^\/(?:-\/project\/(\d+)|((?:[\w.-]+\/)*[\w.-]+))\/uploads\/([0-9a-f]{32})\/([^/]+)$/iu;
9-
/** A link destination, an HTML `src`/`href`, or a reference definition pointing at an upload. */
9+
/**
10+
* An inline code span, kept as written, or a link destination, HTML `src`/`href`, or reference
11+
* definition pointing at an upload.
12+
*/
1013
const RELATIVE_UPLOAD_PATTERN =
11-
/(\]\(\s*<?|\b(?:src|href)\s*=\s*["']?|^ {0,3}\[[^\]\n]+\]:\s*<?)\/uploads\/(?=[0-9a-f]{32}\/)/gimu;
14+
/(`+)[^`]*?\1|(\]\(\s*<?|\b(?:src|href)\s*=\s*["']?|^ {0,3}\[[^\]\n]+\]:\s*<?)\/uploads\/(?=[0-9a-f]{32}\/)/giu;
15+
const FENCE_PATTERN = /^\s{0,3}(`{3,}|~{3,})(.*)$/u;
1216

1317
export interface GitLabUpload {
1418
readonly origin: string;
@@ -65,11 +69,30 @@ export function gitlabMediaFileName(fetchUrl: string): string {
6569
return name.length > 0 ? name : "gitlab-upload";
6670
}
6771

68-
/** Rewrites GitLab's project-relative upload links against the project's web URL. */
72+
/** Rewrites GitLab's project-relative upload links against the project's web URL, outside code. */
6973
export function resolveGitLabUploadLinks(markdown: string, projectUrl: string): string {
7074
const base = projectUrl.replace(/\/+$/u, "");
71-
return markdown.replace(
72-
RELATIVE_UPLOAD_PATTERN,
73-
(_, prefix: string) => `${prefix}${base}/uploads/`,
74-
);
75+
let openFence: string | null = null;
76+
return markdown
77+
.split("\n")
78+
.map((line) => {
79+
const fence = FENCE_PATTERN.exec(line);
80+
if (fence !== null) {
81+
const marker = fence[1]!;
82+
if (openFence === null) openFence = marker;
83+
else if (
84+
marker[0] === openFence[0] &&
85+
marker.length >= openFence.length &&
86+
fence[2]!.trim() === ""
87+
) {
88+
openFence = null;
89+
}
90+
return line;
91+
}
92+
if (openFence !== null) return line;
93+
return line.replace(RELATIVE_UPLOAD_PATTERN, (match, _code, prefix: string | undefined) =>
94+
prefix === undefined ? match : `${prefix}${base}/uploads/`,
95+
);
96+
})
97+
.join("\n");
7598
}

0 commit comments

Comments
 (0)