Clean up document link resolve (#154959)

- Move vscode scheme normalization to the DocumentLinkProvider
- Remove extra function since we already recognize uri-like links
This commit is contained in:
Matt Bierner 2022-07-12 15:55:21 -07:00 committed by GitHub
parent 66d8947f84
commit 75e231ad82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 27 deletions

View file

@ -13,7 +13,7 @@ import { getLine, ITextDocument } from '../types/textDocument';
import { coalesce } from '../util/arrays';
import { noopToken } from '../util/cancellation';
import { Disposable } from '../util/dispose';
import { getUriForLinkWithKnownExternalScheme, isOfScheme, Schemes } from '../util/schemes';
import { Schemes } from '../util/schemes';
import { MdDocumentInfoCache } from '../util/workspaceCache';
import { IMdWorkspace } from '../workspace';
@ -43,14 +43,6 @@ function resolveLink(
link: string,
): ExternalHref | InternalHref | undefined {
const cleanLink = stripAngleBrackets(link);
const externalSchemeUri = getUriForLinkWithKnownExternalScheme(cleanLink);
if (externalSchemeUri) {
// Normalize VS Code links to target currently running version
if (isOfScheme(Schemes.vscode, link) || isOfScheme(Schemes['vscode-insiders'], link)) {
return { kind: 'external', uri: vscode.Uri.parse(link).with({ scheme: vscode.env.uriScheme }) };
}
return { kind: 'external', uri: externalSchemeUri };
}
if (/^[a-z\-][a-z\-]+:/i.test(cleanLink)) {
// Looks like a uri
@ -573,6 +565,11 @@ export class MdVsCodeLinkProvider implements vscode.DocumentLinkProvider {
private toValidDocumentLink(link: MdLink, definitionSet: LinkDefinitionSet): vscode.DocumentLink | undefined {
switch (link.href.kind) {
case 'external': {
let target = link.href.uri;
// Normalize VS Code links to target currently running version
if (link.href.uri.scheme === Schemes.vscode || link.href.uri.scheme === Schemes['vscode-insiders']) {
target = target.with({ scheme: vscode.env.uriScheme });
}
return new vscode.DocumentLink(link.source.hrefRange, link.href.uri);
}
case 'internal': {

View file

@ -3,33 +3,15 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
export const Schemes = Object.freeze({
http: 'http',
https: 'https',
file: 'file',
untitled: 'untitled',
mailto: 'mailto',
data: 'data',
vscode: 'vscode',
'vscode-insiders': 'vscode-insiders',
notebookCell: 'vscode-notebook-cell',
});
const knownSchemes = [
...Object.values(Schemes),
`${vscode.env.uriScheme}`
];
export function getUriForLinkWithKnownExternalScheme(link: string): vscode.Uri | undefined {
if (knownSchemes.some(knownScheme => isOfScheme(knownScheme, link))) {
return vscode.Uri.parse(link);
}
return undefined;
}
export function isOfScheme(scheme: string, link: string): boolean {
return link.toLowerCase().startsWith(scheme + ':');
}