Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[markdown-preview] resolve relative links properly #6403

Merged
merged 1 commit into from
Oct 17, 2019
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
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