Skip to content

Commit 967dcb6

Browse files
committed
Fixes #1695: properly encodes hover links
1 parent a262341 commit 967dcb6

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

src/annotations/autolinks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { GlyphChars } from '../constants';
55
import { Container } from '../container';
66
import { GitRemote, IssueOrPullRequest } from '../git/git';
77
import { Logger } from '../logger';
8-
import { Dates, debug, Iterables, Promises, Strings } from '../system';
8+
import { Dates, debug, Encoding, Iterables, Promises, Strings } from '../system';
99

1010
const numRegex = /<num>/g;
1111

@@ -157,7 +157,7 @@ export class Autolinks implements Disposable {
157157
}
158158

159159
if (issuesOrPullRequests == null || issuesOrPullRequests.size === 0) {
160-
const replacement = `[$1](${ref.url.replace(numRegex, '$2')}${
160+
const replacement = `[$1](${Encoding.encodeUrl(ref.url.replace(numRegex, '$2'))}${
161161
ref.title ? ` "${ref.title.replace(numRegex, '$2')}"` : ''
162162
})`;
163163
ref.linkify = (text: string, markdown: boolean) =>
@@ -174,7 +174,7 @@ export class Autolinks implements Disposable {
174174
return text.replace(ref.messageMarkdownRegex!, (_substring, linkText, num) => {
175175
const issue = issuesOrPullRequests?.get(num);
176176

177-
const issueUrl = ref.url.replace(numRegex, num);
177+
const issueUrl = Encoding.encodeUrl(ref.url.replace(numRegex, num));
178178

179179
let title = '';
180180
if (ref.title) {

src/git/remotes/provider.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { AutolinkReference } from '../../config';
1414
import { WorkspaceState } from '../../constants';
1515
import { Container } from '../../container';
1616
import { Logger } from '../../logger';
17-
import { debug, gate, log, Promises } from '../../system';
17+
import { debug, Encoding, gate, log, Promises } from '../../system';
1818
import {
1919
Account,
2020
DefaultBranch,
@@ -234,11 +234,7 @@ export abstract class RemoteProvider implements RemoteProviderReference {
234234
protected encodeUrl(url: string): string;
235235
protected encodeUrl(url: string | undefined): string | undefined;
236236
protected encodeUrl(url: string | undefined): string | undefined {
237-
if (url == null) return undefined;
238-
239-
// Not a fan of this, but it's hard to gauge previous encoding and this is the most common case
240-
url = url.replace(/%20/g, ' ');
241-
return encodeURI(url).replace(/#/g, '%23');
237+
return Encoding.encodeUrl(url);
242238
}
243239
}
244240

src/system.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export * from './system/decorators/gate';
2424
export * from './system/decorators/log';
2525
export * from './system/decorators/memoize';
2626
export * from './system/decorators/timeout';
27+
export * as Encoding from './system/encoding';
2728
export * as Functions from './system/function';
2829
export * as Iterables from './system/iterable';
2930
export * as Objects from './system/object';

src/system/encoding.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
export function encodeUrl(url: string): string;
4+
export function encodeUrl(url: string | undefined): string | undefined;
5+
export function encodeUrl(url: string | undefined): string | undefined {
6+
if (url == null) return undefined;
7+
8+
// Not a fan of this, but it's hard to gauge previous encoding and this is the most common case
9+
url = url.replace(/%20/g, ' ');
10+
return encodeURI(url).replace(/#/g, '%23');
11+
}

0 commit comments

Comments
 (0)