diff --git a/src/annotations/autolinks.ts b/src/annotations/autolinks.ts index cb1e05a..8fbf222 100644 --- a/src/annotations/autolinks.ts +++ b/src/annotations/autolinks.ts @@ -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 = //g; @@ -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) => @@ -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) { diff --git a/src/git/remotes/provider.ts b/src/git/remotes/provider.ts index 63cb488..f468e32 100644 --- a/src/git/remotes/provider.ts +++ b/src/git/remotes/provider.ts @@ -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, @@ -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); } } diff --git a/src/system.ts b/src/system.ts index e32f8ae..e5e18db 100644 --- a/src/system.ts +++ b/src/system.ts @@ -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'; diff --git a/src/system/encoding.ts b/src/system/encoding.ts new file mode 100644 index 0000000..f86054d --- /dev/null +++ b/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'); +}