diff --git a/extensions/markdown/package.json b/extensions/markdown/package.json index f66f8028f65..98808a51a41 100644 --- a/extensions/markdown/package.json +++ b/extensions/markdown/package.json @@ -117,7 +117,7 @@ "markdown.styles": { "type": ["array"], "default": [], - "description": "A list of URLs or local paths to CSS style sheets to use from the markdown preview." + "description": "A list of URLs or local paths to CSS style sheets to use from the markdown preview. Relative paths are interpreted relative to the folder open in the explorer. If there is no open folder, they are interpreted relative to the location of the markdown file." } } } diff --git a/extensions/markdown/src/extension.ts b/extensions/markdown/src/extension.ts index ef1ac74e725..37c788fa4e2 100644 --- a/extensions/markdown/src/extension.ts +++ b/extensions/markdown/src/extension.ts @@ -183,17 +183,33 @@ class MDDocumentContentProvider implements TextDocumentContentProvider { return md; } - private getMediaPath(mediaFile) { + private getMediaPath(mediaFile) : string { return this._context.asAbsolutePath(path.join('media', mediaFile)); } - private fixHref(resource: Uri, href: string) { + private isAbsolute(p) : boolean { + return path.normalize(p + '/') === path.normalize(path.resolve(p) + '/'); + } + + private fixHref(resource: Uri, href: string) : string { if (href) { - // Return early if href is already a URL + // Use href if it is already an URL if (Uri.parse(href).scheme) { return href; } - // Otherwise convert to a file URI by joining the href with the resource location + + // Use href as file URI if it is absolute + if (this.isAbsolute(href)) { + return Uri.file(href).toString(); + } + + // use a workspace relative path if there is a workspace + let rootPath = vscode.workspace.rootPath; + if (rootPath) { + return Uri.file(path.join(rootPath, href)).toString(); + } + + // otherwise look relative to the markdown file return Uri.file(path.join(path.dirname(resource.fsPath), href)).toString(); } return href;