Add Markdown Document Link Provider To Support Local Links (#18821)

* Support []() links

* Fix for file switching

* Fix local links with hash fragments
This commit is contained in:
Matt Bierner 2017-01-20 11:36:34 -08:00 committed by GitHub
parent 3f72812d61
commit b5ae881b26
3 changed files with 55 additions and 1 deletions

View file

@ -16,7 +16,8 @@
"onLanguage:markdown",
"onCommand:markdown.showPreview",
"onCommand:markdown.showPreviewToSide",
"onCommand:markdown.showSource"
"onCommand:markdown.showSource",
"onLanguage:markdown"
],
"contributes": {
"languages": [

View file

@ -0,0 +1,50 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as vscode from 'vscode';
import * as path from 'path';
export default class MarkdownDocumentLinkProvider implements vscode.DocumentLinkProvider {
private _linkPattern = /(\[[^\]]*\]\(\s*?)(\S+?)(\s+[^\)]*)?\)/g;
public provideDocumentLinks(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.DocumentLink[] {
const results: vscode.DocumentLink[] = [];
const base = path.dirname(document.uri.fsPath);
const text = document.getText();
this._linkPattern.lastIndex = 0;
let match: RegExpMatchArray | null;
while ((match = this._linkPattern.exec(text))) {
const pre = match[1];
const link = match[2];
const offset = match.index + pre.length;
const linkStart = document.positionAt(offset);
const linkEnd = document.positionAt(offset + link.length);
try {
let uri = vscode.Uri.parse(link);
if (!uri.scheme) {
// assume it must be a file
let file;
if (uri.path[0] === '/') {
file = path.join(vscode.workspace.rootPath, uri.path);
} else {
file = path.join(base, uri.path);
}
uri = vscode.Uri.file(file);
}
results.push(new vscode.DocumentLink(
new vscode.Range(linkStart, linkEnd),
uri));
} catch (e) {
// noop
}
}
return results;
}
};

View file

@ -8,6 +8,7 @@
import * as vscode from 'vscode';
import * as path from 'path';
import TelemetryReporter from 'vscode-extension-telemetry';
import DocumentLinkProvider from './documentLinkProvider';
interface IPackageInfo {
name: string;
@ -104,6 +105,8 @@ export function activate(context: vscode.ExtensionContext) {
const symbolsProviderRegistration = vscode.languages.registerDocumentSymbolProvider({ language: 'markdown' }, symbolsProvider);
context.subscriptions.push(contentProviderRegistration, symbolsProviderRegistration);
context.subscriptions.push(vscode.languages.registerDocumentLinkProvider('markdown', new DocumentLinkProvider()));
context.subscriptions.push(vscode.commands.registerCommand('markdown.showPreview', showPreview));
context.subscriptions.push(vscode.commands.registerCommand('markdown.showPreviewToSide', uri => showPreview(uri, true)));
context.subscriptions.push(vscode.commands.registerCommand('markdown.showSource', showSource));