Skip to content

Commit

Permalink
[markdown-preview] fix #6396: resolve relative links properly
Browse files Browse the repository at this point in the history
Latest version of vscode-uir makes sure that URI always has a scheme  or fall backs to `file` scheme and that URI is always absolute by adding leading `/` to the relative path. It broke an assumption that a URI can be relative in the markdown preview.

Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed Oct 17, 2019
1 parent e0074c5 commit 5cd87bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { injectable, inject } from 'inversify';
import URI from '@theia/core/lib/common/uri';
import { OpenerService } from '@theia/core/lib/browser';
import { isOSX } from '@theia/core/lib/common';
import { Path } from '@theia/core/lib/common/path';

import * as hljs from 'highlight.js';
import * as markdownit from 'markdown-it';
Expand Down Expand Up @@ -91,11 +92,12 @@ export class MarkdownPreviewHandler implements PreviewHandler {

protected resolveUri(link: string, uri: URI, preview: boolean): URI {
const linkURI = new URI(link);
if (!linkURI.path.isAbsolute && (
!(linkURI.scheme || linkURI.authority) ||
(linkURI.scheme === uri.scheme && linkURI.authority === uri.authority)
)) {
const resolvedUri = uri.parent.resolve(linkURI.path).withFragment(linkURI.fragment).withQuery(linkURI.query);
// URIs are always absolute, check link as a path whether it is relative
if (!new Path(link).isAbsolute && linkURI.scheme === uri.scheme &&
(!linkURI.authority || linkURI.authority === uri.authority)) {
// get a relative path from URI by trimming leading `/`
const relativePath = linkURI.path.toString().substring(1);
const resolvedUri = uri.parent.resolve(relativePath).withFragment(linkURI.fragment).withQuery(linkURI.query);
return preview ? PreviewUri.encode(resolvedUri) : resolvedUri;
}
return linkURI;
Expand Down
6 changes: 5 additions & 1 deletion packages/preview/src/browser/preview-uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export namespace PreviewUri {
if (match(uri)) {
return uri;
}
const query = [param, ...uri.query.split('&')].join('&');
const params = [param];
if (uri.query) {
params.push(...uri.query.split('&'));
}
const query = params.join('&');
return uri.withQuery(query);
}
export function decode(uri: URI): URI {
Expand Down

0 comments on commit 5cd87bf

Please sign in to comment.