Addd option to open markdown preview links in markdown preview

Fixes #19339
This commit is contained in:
Matt Bierner
2018-09-21 15:07:48 -07:00
parent a101ececf4
commit e19c9ba82d
5 changed files with 85 additions and 17 deletions

View File

@@ -252,6 +252,20 @@
"description": "%markdown.preview.doubleClickToSwitchToEditor.desc%",
"scope": "resource"
},
"markdown.preview.openMarkdownLinks": {
"type": "string",
"default": "inPreview",
"description": "%configuration.markdown.preview.openMarkdownLinks.description%",
"scope": "resource",
"enum": [
"inPreview",
"inEditor"
],
"enumDescriptions": [
"%configuration.markdown.preview.openMarkdownLinks.inPreview%",
"%configuration.markdown.preview.openMarkdownLinks.inEditor%"
]
},
"markdown.trace": {
"type": "string",
"enum": [
@@ -312,4 +326,4 @@
"webpack": "^4.1.0",
"webpack-cli": "^2.0.10"
}
}
}

View File

@@ -12,14 +12,17 @@
"markdown.preview.scrollPreviewWithEditor.desc": "When a markdown editor is scrolled, update the view of the preview.",
"markdown.preview.scrollPreviewWithEditorSelection.desc": "[Deprecated] Scrolls the markdown preview to reveal the currently selected line from the editor.",
"markdown.preview.scrollPreviewWithEditorSelection.deprecationMessage": "This setting has been replaced by 'markdown.preview.scrollPreviewWithEditor' and no longer has any effect.",
"markdown.preview.title" : "Open Preview",
"markdown.preview.title": "Open Preview",
"markdown.previewFrontMatter.dec": "Sets how YAML front matter should be rendered in the markdown preview. 'hide' removes the front matter. Otherwise, the front matter is treated as markdown content.",
"markdown.previewSide.title" : "Open Preview to the Side",
"markdown.previewSide.title": "Open Preview to the Side",
"markdown.showLockedPreviewToSide.title": "Open Locked Preview to the Side",
"markdown.showSource.title" : "Show Source",
"markdown.showSource.title": "Show Source",
"markdown.styles.dec": "A list of URLs or local paths to CSS style sheets to use from the markdown preview. Relative paths are interpreted relative to the folder open in the explorer. If there is no open folder, they are interpreted relative to the location of the markdown file. All '\\' need to be written as '\\\\'.",
"markdown.showPreviewSecuritySelector.title": "Change Preview Security Settings",
"markdown.trace.desc": "Enable debug logging for the markdown extension.",
"markdown.preview.refresh.title": "Refresh Preview",
"markdown.preview.toggleLock.title": "Toggle Preview Locking"
}
"markdown.preview.toggleLock.title": "Toggle Preview Locking",
"configuration.markdown.preview.openMarkdownLinks.description": "How should clicking on links to markdown files be handled in the preview.",
"configuration.markdown.preview.openMarkdownLinks.inEditor": "Try to open links in the editor",
"configuration.markdown.preview.openMarkdownLinks.inPreview": "Try to open links in the the markdown preview"
}

View File

@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as path from 'path';
import { extname } from 'path';
import { Command } from '../commandManager';
import { MarkdownEngine } from '../markdownEngine';
@@ -35,7 +35,7 @@ export class OpenDocumentLinkCommand implements Command {
public execute(args: OpenDocumentLinkArgs) {
const p = decodeURIComponent(args.path);
return this.tryOpen(p, args).catch(() => {
if (path.extname(p) === '') {
if (extname(p) === '') {
return this.tryOpen(p + '.md', args);
}
const resource = vscode.Uri.file(p);
@@ -73,3 +73,37 @@ export class OpenDocumentLinkCommand implements Command {
}
}
}
export async function resolveLinkToMarkdownFile(path: string): Promise<vscode.Uri | undefined> {
try {
const standardLink = await tryResolveLinkToMarkdownFile(path);
if (standardLink) {
return standardLink;
}
} catch {
// Noop
}
// If no extension, try with `.md` extension
if (extname(path) === '') {
return tryResolveLinkToMarkdownFile(path + '.md');
}
return undefined;
}
async function tryResolveLinkToMarkdownFile(path: string): Promise<vscode.Uri | undefined> {
const resource = vscode.Uri.file(path);
let document: vscode.TextDocument;
try {
document = await vscode.workspace.openTextDocument(resource);
} catch {
return undefined;
}
if (isMarkdownFile(document)) {
return document.uri;
}
return undefined;
}

View File

@@ -15,6 +15,7 @@ import { getVisibleLine, MarkdownFileTopmostLineMonitor } from '../util/topmostL
import { MarkdownPreviewConfigurationManager } from './previewConfig';
import { MarkdownContributions } from '../markdownExtensions';
import { isMarkdownFile } from '../util/file';
import { resolveLinkToMarkdownFile } from '../commands/openDocumentLink';
const localize = nls.loadMessageBundle();
export class MarkdownPreview {
@@ -137,7 +138,7 @@ export class MarkdownPreview {
break;
case 'clickLink':
vscode.commands.executeCommand('_markdown.openDocumentLink', e.body);
this.onDidClickPreviewLink(e.body.path, e.body.fragement);
break;
case 'showPreviewSecuritySelector':
@@ -414,6 +415,20 @@ export class MarkdownPreview {
vscode.workspace.openTextDocument(this._resource).then(vscode.window.showTextDocument);
}
private async onDidClickPreviewLink(path: string, fragment: string | undefined) {
const config = vscode.workspace.getConfiguration('markdown', this.resource);
const openLinks = config.get<string>('preview.openMarkdownLinks', 'inPreview');
if (openLinks === 'inPreview') {
const markdownLink = await resolveLinkToMarkdownFile(path);
if (markdownLink) {
this.update(markdownLink);
return;
}
}
vscode.commands.executeCommand('_markdown.openDocumentLink', { path, fragment });
}
private async onCacheImageSizes(imageInfo: { id: string, width: number, height: number }[]) {
this.imageInfo = imageInfo;
}