Welcome to mirror list, hosted at ThFree Co, Russian Federation.

openDocumentLink.ts « util « src « markdown-language-features « extensions - github.com/microsoft/vscode.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18b8e5f4c79533ff35408d853b2d2b172d79dc12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as path from 'path';
import * as vscode from 'vscode';
import * as uri from 'vscode-uri';
import { MarkdownEngine } from '../markdownEngine';
import { TableOfContents } from '../tableOfContents';
import { isMarkdownFile } from './file';

export interface OpenDocumentLinkArgs {
	readonly parts: vscode.Uri;
	readonly fragment: string;
	readonly fromResource: vscode.Uri;
}

enum OpenMarkdownLinks {
	beside = 'beside',
	currentGroup = 'currentGroup',
}

export function resolveDocumentLink(href: string, markdownFile: vscode.Uri): vscode.Uri {
	let [hrefPath, fragment] = href.split('#').map(c => decodeURIComponent(c));

	if (hrefPath[0] === '/') {
		// Absolute path. Try to resolve relative to the workspace
		const workspace = vscode.workspace.getWorkspaceFolder(markdownFile);
		if (workspace) {
			return vscode.Uri.joinPath(workspace.uri, hrefPath.slice(1)).with({ fragment });
		}
	}

	// Relative path. Resolve relative to the md file
	const dirnameUri = markdownFile.with({ path: path.dirname(markdownFile.path) });
	return vscode.Uri.joinPath(dirnameUri, hrefPath).with({ fragment });
}

export async function openDocumentLink(engine: MarkdownEngine, targetResource: vscode.Uri, fromResource: vscode.Uri): Promise<void> {
	const column = getViewColumn(fromResource);

	if (await tryNavigateToFragmentInActiveEditor(engine, targetResource)) {
		return;
	}

	let targetResourceStat: vscode.FileStat | undefined;
	try {
		targetResourceStat = await vscode.workspace.fs.stat(targetResource);
	} catch {
		// noop
	}

	if (typeof targetResourceStat === 'undefined') {
		// We don't think the file exists. If it doesn't already have an extension, try tacking on a `.md` and using that instead
		if (uri.Utils.extname(targetResource) === '') {
			const dotMdResource = targetResource.with({ path: targetResource.path + '.md' });
			try {
				const stat = await vscode.workspace.fs.stat(dotMdResource);
				if (stat.type === vscode.FileType.File) {
					await tryOpenMdFile(engine, dotMdResource, column);
					return;
				}
			} catch {
				// noop
			}
		}
	} else if (targetResourceStat.type === vscode.FileType.Directory) {
		return vscode.commands.executeCommand('revealInExplorer', targetResource);
	}

	await tryOpenMdFile(engine, targetResource, column);
}

async function tryOpenMdFile(engine: MarkdownEngine, resource: vscode.Uri, column: vscode.ViewColumn): Promise<boolean> {
	await vscode.commands.executeCommand('vscode.open', resource.with({ fragment: '' }), column);
	return tryNavigateToFragmentInActiveEditor(engine, resource);
}

async function tryNavigateToFragmentInActiveEditor(engine: MarkdownEngine, resource: vscode.Uri): Promise<boolean> {
	const activeEditor = vscode.window.activeTextEditor;
	if (activeEditor?.document.uri.fsPath === resource.fsPath) {
		if (isMarkdownFile(activeEditor.document)) {
			if (await tryRevealLineUsingTocFragment(engine, activeEditor, resource.fragment)) {
				return true;
			}
		}
		tryRevealLineUsingLineFragment(activeEditor, resource.fragment);
		return true;
	}
	return false;
}

function getViewColumn(resource: vscode.Uri): vscode.ViewColumn {
	const config = vscode.workspace.getConfiguration('markdown', resource);
	const openLinks = config.get<OpenMarkdownLinks>('links.openLocation', OpenMarkdownLinks.currentGroup);
	switch (openLinks) {
		case OpenMarkdownLinks.beside:
			return vscode.ViewColumn.Beside;
		case OpenMarkdownLinks.currentGroup:
		default:
			return vscode.ViewColumn.Active;
	}
}

async function tryRevealLineUsingTocFragment(engine: MarkdownEngine, editor: vscode.TextEditor, fragment: string): Promise<boolean> {
	const toc = await TableOfContents.create(engine, editor.document);
	const entry = toc.lookup(fragment);
	if (entry) {
		const lineStart = new vscode.Range(entry.line, 0, entry.line, 0);
		editor.selection = new vscode.Selection(lineStart.start, lineStart.end);
		editor.revealRange(lineStart, vscode.TextEditorRevealType.AtTop);
		return true;
	}
	return false;
}

function tryRevealLineUsingLineFragment(editor: vscode.TextEditor, fragment: string): boolean {
	const lineNumberFragment = fragment.match(/^L(\d+)$/i);
	if (lineNumberFragment) {
		const line = +lineNumberFragment[1] - 1;
		if (!isNaN(line)) {
			const lineStart = new vscode.Range(line, 0, line, 0);
			editor.selection = new vscode.Selection(lineStart.start, lineStart.end);
			editor.revealRange(lineStart, vscode.TextEditorRevealType.AtTop);
			return true;
		}
	}
	return false;
}

export async function resolveUriToMarkdownFile(resource: vscode.Uri): Promise<vscode.TextDocument | undefined> {
	try {
		const doc = await tryResolveUriToMarkdownFile(resource);
		if (doc) {
			return doc;
		}
	} catch {
		// Noop
	}

	// If no extension, try with `.md` extension
	if (uri.Utils.extname(resource) === '') {
		return tryResolveUriToMarkdownFile(resource.with({ path: resource.path + '.md' }));
	}

	return undefined;
}

async function tryResolveUriToMarkdownFile(resource: vscode.Uri): Promise<vscode.TextDocument | undefined> {
	let document: vscode.TextDocument;
	try {
		document = await vscode.workspace.openTextDocument(resource);
	} catch {
		return undefined;
	}
	if (isMarkdownFile(document)) {
		return document;
	}
	return undefined;
}