mirror of
https://github.com/Microsoft/vscode
synced 2024-09-13 21:55:38 +00:00
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:
parent
3f72812d61
commit
b5ae881b26
|
@ -16,7 +16,8 @@
|
|||
"onLanguage:markdown",
|
||||
"onCommand:markdown.showPreview",
|
||||
"onCommand:markdown.showPreviewToSide",
|
||||
"onCommand:markdown.showSource"
|
||||
"onCommand:markdown.showSource",
|
||||
"onLanguage:markdown"
|
||||
],
|
||||
"contributes": {
|
||||
"languages": [
|
||||
|
|
50
extensions/markdown/src/documentLinkProvider.ts
Normal file
50
extensions/markdown/src/documentLinkProvider.ts
Normal 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;
|
||||
}
|
||||
};
|
|
@ -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));
|
||||
|
|
Loading…
Reference in a new issue