Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions extensions/markdown/src/documentLinkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import * as vscode from 'vscode';
import * as path from 'path';

import { MarkdownEngine } from './markdownEngine';
import { TableOfContentProvider } from './tableOfContentsProvider';

export default class MarkdownDocumentLinkProvider implements vscode.DocumentLinkProvider {

Expand All @@ -22,8 +21,6 @@ export default class MarkdownDocumentLinkProvider implements vscode.DocumentLink
const base = path.dirname(document.uri.fsPath);
const text = document.getText();

const toc = new TableOfContentProvider(this.engine, document);

this._linkPattern.lastIndex = 0;
let match: RegExpMatchArray | null;
while ((match = this._linkPattern.exec(text))) {
Expand All @@ -35,7 +32,7 @@ export default class MarkdownDocumentLinkProvider implements vscode.DocumentLink
try {
results.push(new vscode.DocumentLink(
new vscode.Range(linkStart, linkEnd),
this.normalizeLink(link, base, toc)));
this.normalizeLink(document, link, base)));
} catch (e) {
// noop
}
Expand All @@ -44,26 +41,22 @@ export default class MarkdownDocumentLinkProvider implements vscode.DocumentLink
return results;
}

private normalizeLink(link: string, base: string, toc: TableOfContentProvider): vscode.Uri {
let uri = vscode.Uri.parse(link);
if (!uri.scheme) {
if (uri.fragment && !uri.path) {
// local link
const line = toc.lookup(uri.fragment);
if (!isNaN(line)) {
return vscode.Uri.parse(`command:revealLine?${encodeURIComponent(JSON.stringify({ lineNumber: line, at: 'top' }))}`);
}
}
private normalizeLink(document: vscode.TextDocument, link: string, base: string): vscode.Uri {
const uri = vscode.Uri.parse(link);
if (uri.scheme) {
return uri;
}

// 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);
// assume it must be a file
let resourcePath;
if (!uri.path) {
resourcePath = document.uri.path;
} else if (uri.path[0] === '/') {
resourcePath = path.join(vscode.workspace.rootPath, uri.path);
} else {
resourcePath = path.join(base, uri.path);
}
return uri;

return vscode.Uri.parse(`command:_markdown.openDocumentLink?${encodeURIComponent(JSON.stringify({ fragment: uri.fragment, path: resourcePath }))}`);
Copy link
Copy Markdown
Member

@jrieken jrieken Feb 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a lot of code for using an URI that goes like this: file:///files/thisFile.md#L22. Unless there are bugs which I would happily fix, the OpenerService should handle such links correctly.

}
}
24 changes: 23 additions & 1 deletion extensions/markdown/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { MarkdownEngine } from './markdownEngine';
import DocumentLinkProvider from './documentLinkProvider';
import MDDocumentSymbolProvider from './documentSymbolProvider';
import { MDDocumentContentProvider, getMarkdownUri, isMarkdownFile } from './previewContentProvider';

import { TableOfContentProvider } from './tableOfContentsProvider';

interface IPackageInfo {
name: string;
Expand Down Expand Up @@ -70,6 +70,28 @@ export function activate(context: vscode.ExtensionContext) {
});
}));

context.subscriptions.push(vscode.commands.registerCommand('_markdown.openDocumentLink', (args) => {
const tryRevealLine = (editor: vscode.TextEditor) => {
if (editor && args.fragment) {
const toc = new TableOfContentProvider(engine, editor.document);
const line = toc.lookup(args.fragment);
if (!isNaN(line)) {
return editor.revealRange(
new vscode.Range(line, 0, line, 0),
vscode.TextEditorRevealType.AtTop);
}
}
};
if (vscode.window.activeTextEditor && vscode.window.activeTextEditor.document.uri.path === args.path) {
return tryRevealLine(vscode.window.activeTextEditor);
} else {
const resource = vscode.Uri.file(args.path);
vscode.workspace.openTextDocument(resource)
.then(vscode.window.showTextDocument)
.then(tryRevealLine, _ => vscode.commands.executeCommand('vscode.open', resource));
}
}));

context.subscriptions.push(vscode.workspace.onDidSaveTextDocument(document => {
if (isMarkdownFile(document)) {
const uri = getMarkdownUri(document.uri);
Expand Down