提交 4d42f3d7 编写于 作者: M Matt Bierner

Support extension-less links in the markdown preview

Fixes #39945
上级 40c8d176
......@@ -2,7 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// @ts-check
'use strict';
(function () {
......@@ -29,6 +29,13 @@
};
}
function postMessage(command, args) {
window.parent.postMessage({
command: 'did-click-link',
data: `command:${command}?${encodeURIComponent(JSON.stringify(args))}`
}, 'file://');
}
/**
* Find the html elements that map to a specific target line in the editor.
*
......@@ -196,14 +203,34 @@
const offset = event.pageY;
const line = getEditorLineNumberForPageOffset(offset);
if (!isNaN(line)) {
const args = [settings.source, line];
window.parent.postMessage({
command: "did-click-link",
data: `command:_markdown.didClick?${encodeURIComponent(JSON.stringify(args))}`
}, "file://");
postMessage('_markdown.didClick', [settings.source, line]);
}
});
document.addEventListener('click', event => {
if (!event) {
return;
}
const baseElement = document.getElementsByTagName('base')[0];
/** @type {any} */
let node = event.target;
while (node) {
if (node.tagName && node.tagName.toLowerCase() === 'a' && node.href) {
if (node.href.startsWith('file://')) {
const [path, frag] = node.href.replace(/^file:\/\//i, '').split('#');
postMessage('_markdown.openDocumentLink', { path, frag });
event.preventDefault();
event.stopPropagation();
break;
}
break;
}
node = node.parentNode;
}
}, true);
if (settings.scrollEditorWithPreview) {
window.addEventListener('scroll', throttle(() => {
if (scrollDisabled) {
......@@ -211,11 +238,7 @@
} else {
const line = getEditorLineNumberForPageOffset(window.scrollY);
if (!isNaN(line)) {
const args = [settings.source, line];
window.parent.postMessage({
command: 'did-click-link',
data: `command:_markdown.revealLine?${encodeURIComponent(JSON.stringify(args))}`
}, 'file://');
postMessage('_markdown.revealLine', [settings.source, line]);
}
}
}, 50));
......
......@@ -137,8 +137,9 @@ export class MarkdownEngine {
md.normalizeLink = (link: string) => {
try {
let uri = vscode.Uri.parse(link);
if (!uri.scheme && uri.path && !uri.fragment) {
if (!uri.scheme && uri.path) {
// Assume it must be a file
const fragment = uri.fragment;
if (uri.path[0] === '/') {
const root = vscode.workspace.getWorkspaceFolder(this.currentDocument);
if (root) {
......@@ -147,6 +148,10 @@ export class MarkdownEngine {
} else {
uri = vscode.Uri.file(path.join(path.dirname(this.currentDocument.path), uri.path));
}
if (fragment) {
uri = uri.with({ fragment });
}
return normalizeLink(uri.toString(true));
}
} catch (e) {
......
......@@ -46,11 +46,12 @@
if (!event || !event.view || !event.view.document) {
return;
}
var baseElement = event.view.document.getElementsByTagName('base')[0];
/** @type {any} */
var node = event.target;
while (node) {
if (node.tagName && node.tagName.toLowerCase() === 'a' && node.href) {
var baseElement = event.view.document.getElementsByTagName('base')[0];
if (node.getAttribute('href') === '#') {
event.view.scrollTo(0, 0);
} else if (node.hash && (node.getAttribute('href') === node.hash || (baseElement && node.href.indexOf(baseElement.href) >= 0))) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册