Skip to content

Commit e60ee43

Browse files
committed
fix(shared): resolve GitLab upload references whose destination is on the next line
1 parent 0364863 commit e60ee43

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ describe("gitlabMedia", () => {
6262
`[demo.mp4](</uploads/${SECRET}/demo.mp4>)`,
6363
`<img src="/uploads/${SECRET}/inline.png">`,
6464
`[ref]: /uploads/${SECRET}/ref.pdf`,
65+
`[next-line]:`,
66+
` /uploads/${SECRET}/next-line.png`,
6567
`Prose mentioning /uploads/${SECRET}/x.png stays prose.`,
6668
`![elsewhere](https://cdn.example.com/uploads/${SECRET}/a.png)`,
6769
"```md",
@@ -75,6 +77,8 @@ describe("gitlabMedia", () => {
7577
`[demo.mp4](<https://gitlab.com/team/app/uploads/${SECRET}/demo.mp4>)`,
7678
`<img src="https://gitlab.com/team/app/uploads/${SECRET}/inline.png">`,
7779
`[ref]: https://gitlab.com/team/app/uploads/${SECRET}/ref.pdf`,
80+
`[next-line]:`,
81+
` https://gitlab.com/team/app/uploads/${SECRET}/next-line.png`,
7882
`Prose mentioning /uploads/${SECRET}/x.png stays prose.`,
7983
`![elsewhere](https://cdn.example.com/uploads/${SECRET}/a.png)`,
8084
"```md",

‎packages/shared/src/gitlabMedia.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const UPLOAD_PATH_PATTERN =
1313
const RELATIVE_UPLOAD_PATTERN =
1414
/(`+)[^`]*?\1|(\]\(\s*<?|\b(?:src|href)\s*=\s*["']?|^ {0,3}\[[^\]\n]+\]:\s*<?)\/uploads\/(?=[0-9a-f]{32}\/)/giu;
1515
const FENCE_PATTERN = /^\s{0,3}(`{3,}|~{3,})(.*)$/u;
16+
/** A reference definition may put its destination on the next line. */
17+
const BARE_REFERENCE_LABEL_PATTERN = /^ {0,3}\[[^\]\n]+\]:\s*$/u;
18+
const CONTINUED_UPLOAD_PATTERN = /^(\s*<?)\/uploads\/(?=[0-9a-f]{32}\/)/u;
1619

1720
export interface GitLabUpload {
1821
readonly origin: string;
@@ -73,9 +76,12 @@ export function gitlabMediaFileName(fetchUrl: string): string {
7376
export function resolveGitLabUploadLinks(markdown: string, projectUrl: string): string {
7477
const base = projectUrl.replace(/\/+$/u, "");
7578
let openFence: string | null = null;
79+
let afterBareLabel = false;
7680
return markdown
7781
.split("\n")
7882
.map((line) => {
83+
const continuesLabel = afterBareLabel;
84+
afterBareLabel = false;
7985
const fence = FENCE_PATTERN.exec(line);
8086
if (fence !== null) {
8187
const marker = fence[1]!;
@@ -90,6 +96,12 @@ export function resolveGitLabUploadLinks(markdown: string, projectUrl: string):
9096
return line;
9197
}
9298
if (openFence !== null) return line;
99+
if (continuesLabel) {
100+
line = line.replace(CONTINUED_UPLOAD_PATTERN, (_match, prefix: string) => {
101+
return `${prefix}${base}/uploads/`;
102+
});
103+
}
104+
afterBareLabel = BARE_REFERENCE_LABEL_PATTERN.test(line);
93105
return line.replace(RELATIVE_UPLOAD_PATTERN, (match, _code, prefix: string | undefined) =>
94106
prefix === undefined ? match : `${prefix}${base}/uploads/`,
95107
);

0 commit comments

Comments
 (0)