From 6b01b4c0d8389f66acad134277f0133c4a973883 Mon Sep 17 00:00:00 2001 From: kieferrm Date: Thu, 30 Jun 2016 14:50:22 -0700 Subject: [PATCH] lookup of custom style sheets, fixes #8287 --- extensions/markdown/package.json | 2 +- extensions/markdown/src/extension.ts | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) 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;