Skip to content

Commit

Permalink
Fixes #1695: properly encodes hover links
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Nov 17, 2021
1 parent a262341 commit 967dcb6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/annotations/autolinks.ts
Expand Up @@ -5,7 +5,7 @@ import { GlyphChars } from '../constants';
import { Container } from '../container';
import { GitRemote, IssueOrPullRequest } from '../git/git';
import { Logger } from '../logger';
import { Dates, debug, Iterables, Promises, Strings } from '../system';
import { Dates, debug, Encoding, Iterables, Promises, Strings } from '../system';

const numRegex = /<num>/g;

Expand Down Expand Up @@ -157,7 +157,7 @@ export class Autolinks implements Disposable {
}

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

const issueUrl = ref.url.replace(numRegex, num);
const issueUrl = Encoding.encodeUrl(ref.url.replace(numRegex, num));

let title = '';
if (ref.title) {
Expand Down
8 changes: 2 additions & 6 deletions src/git/remotes/provider.ts
Expand Up @@ -14,7 +14,7 @@ import { AutolinkReference } from '../../config';
import { WorkspaceState } from '../../constants';
import { Container } from '../../container';
import { Logger } from '../../logger';
import { debug, gate, log, Promises } from '../../system';
import { debug, Encoding, gate, log, Promises } from '../../system';
import {
Account,
DefaultBranch,
Expand Down Expand Up @@ -234,11 +234,7 @@ export abstract class RemoteProvider implements RemoteProviderReference {
protected encodeUrl(url: string): string;
protected encodeUrl(url: string | undefined): string | undefined;
protected encodeUrl(url: string | undefined): string | undefined {
if (url == null) return undefined;

// Not a fan of this, but it's hard to gauge previous encoding and this is the most common case
url = url.replace(/%20/g, ' ');
return encodeURI(url).replace(/#/g, '%23');
return Encoding.encodeUrl(url);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/system.ts
Expand Up @@ -24,6 +24,7 @@ export * from './system/decorators/gate';
export * from './system/decorators/log';
export * from './system/decorators/memoize';
export * from './system/decorators/timeout';
export * as Encoding from './system/encoding';
export * as Functions from './system/function';
export * as Iterables from './system/iterable';
export * as Objects from './system/object';
Expand Down
11 changes: 11 additions & 0 deletions src/system/encoding.ts
@@ -0,0 +1,11 @@
'use strict';

export function encodeUrl(url: string): string;
export function encodeUrl(url: string | undefined): string | undefined;
export function encodeUrl(url: string | undefined): string | undefined {
if (url == null) return undefined;

// Not a fan of this, but it's hard to gauge previous encoding and this is the most common case
url = url.replace(/%20/g, ' ');
return encodeURI(url).replace(/#/g, '%23');
}

0 comments on commit 967dcb6

Please sign in to comment.