Use the document that contains the md link as the resource for getting config, not the target document

This commit is contained in:
Matt Bierner
2019-09-10 16:30:49 -07:00
parent 28c5988f47
commit 2fe62e7dfe
3 changed files with 22 additions and 15 deletions

View File

@@ -13,8 +13,9 @@ import { isMarkdownFile } from '../util/file';
export interface OpenDocumentLinkArgs {
path: string;
fragment: string;
readonly path: string;
readonly fragment: string;
readonly fromResource: any;
}
enum OpenMarkdownLinks {
@@ -27,10 +28,15 @@ export class OpenDocumentLinkCommand implements Command {
public readonly id = OpenDocumentLinkCommand.id;
public static createCommandUri(
fromResource: vscode.Uri,
path: string,
fragment: string
fragment: string,
): vscode.Uri {
return vscode.Uri.parse(`command:${OpenDocumentLinkCommand.id}?${encodeURIComponent(JSON.stringify({ path: encodeURIComponent(path), fragment }))}`);
return vscode.Uri.parse(`command:${OpenDocumentLinkCommand.id}?${encodeURIComponent(JSON.stringify(<OpenDocumentLinkArgs>{
path: encodeURIComponent(path),
fragment,
fromResource: encodeURIComponent(fromResource.toString(true)),
}))}`);
}
public constructor(
@@ -38,19 +44,21 @@ export class OpenDocumentLinkCommand implements Command {
) { }
public execute(args: OpenDocumentLinkArgs) {
const p = decodeURIComponent(args.path);
return this.tryOpen(p, args).catch(() => {
if (p && extname(p) === '') {
return this.tryOpen(p + '.md', args);
const fromResource = vscode.Uri.parse(decodeURIComponent(args.fromResource));
const targetPath = decodeURIComponent(args.path);
const column = this.getViewColumn(fromResource);
return this.tryOpen(targetPath, args, column).catch(() => {
if (targetPath && extname(targetPath) === '') {
return this.tryOpen(targetPath + '.md', args, column);
}
const resource = vscode.Uri.file(p);
const targetResource = vscode.Uri.file(targetPath);
return Promise.resolve(undefined)
.then(() => vscode.commands.executeCommand('vscode.open', resource))
.then(() => vscode.commands.executeCommand('vscode.open', targetResource, column))
.then(() => undefined);
});
}
private async tryOpen(path: string, args: OpenDocumentLinkArgs) {
private async tryOpen(path: string, args: OpenDocumentLinkArgs, column: vscode.ViewColumn) {
const resource = vscode.Uri.file(path);
if (vscode.window.activeTextEditor && isMarkdownFile(vscode.window.activeTextEditor.document)) {
if (!path || vscode.window.activeTextEditor.document.uri.fsPath === resource.fsPath) {
@@ -58,7 +66,6 @@ export class OpenDocumentLinkCommand implements Command {
}
}
const column = this.getViewColumn(resource);
return vscode.workspace.openTextDocument(resource)
.then(document => vscode.window.showTextDocument(document, column))
.then(editor => this.tryRevealLine(editor, args.fragment));

View File

@@ -38,7 +38,7 @@ function parseLink(
}
return {
uri: OpenDocumentLinkCommand.createCommandUri(resourcePath, tempUri.fragment),
uri: OpenDocumentLinkCommand.createCommandUri(document.uri, resourcePath, tempUri.fragment),
tooltip: localize('documentLink.tooltip', 'Follow link')
};
}
@@ -183,4 +183,4 @@ export default class LinkProvider implements vscode.DocumentLinkProvider {
}
return out;
}
}
}

View File

@@ -540,7 +540,7 @@ export class MarkdownPreview extends Disposable {
}
}
vscode.commands.executeCommand('_markdown.openDocumentLink', { path, fragment });
vscode.commands.executeCommand('_markdown.openDocumentLink', { path, fragment, fromResource: this.resource });
}
private async onCacheImageSizes(imageInfo: { id: string, width: number, height: number }[]) {