From 88712f46bf0e521bbaf42acb50079063f38221b2 Mon Sep 17 00:00:00 2001 From: Chris LaFreniere <40371649+chlafreniere@users.noreply.github.com> Date: Wed, 10 Apr 2019 11:35:06 -0700 Subject: [PATCH] Fix for relative markdown image paths (#4889) * Fix for relative markdown image paths * PR comments --- .../src/markdownEngine.ts | 6 +++-- .../notebook/cellViews/textCell.component.ts | 24 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/extensions/markdown-language-features/src/markdownEngine.ts b/extensions/markdown-language-features/src/markdownEngine.ts index 9b6c292728..16f2d5350e 100644 --- a/extensions/markdown-language-features/src/markdownEngine.ts +++ b/extensions/markdown-language-features/src/markdownEngine.ts @@ -139,8 +139,10 @@ export class MarkdownEngine { // {{SQL CARBON EDIT}} - Add renderText method public async renderText(document: vscode.Uri, text: string): Promise { - const engine = await this.getEngine(this.getConfig(document)); - return engine.render(text); + const config = this.getConfig(document); + const engine = await this.getEngine(config); + this.currentDocument = document; + return engine.render(text, config); } // {{SQL CARBON EDIT}} - End diff --git a/src/sql/workbench/parts/notebook/cellViews/textCell.component.ts b/src/sql/workbench/parts/notebook/cellViews/textCell.component.ts index 0a7186a313..0013824d25 100644 --- a/src/sql/workbench/parts/notebook/cellViews/textCell.component.ts +++ b/src/sql/workbench/parts/notebook/cellViews/textCell.component.ts @@ -5,6 +5,7 @@ import 'vs/css!./textCell'; import { OnInit, Component, Input, Inject, forwardRef, ElementRef, ChangeDetectorRef, OnDestroy, ViewChild, OnChanges, SimpleChange } from '@angular/core'; +import * as path from 'path'; import { localize } from 'vs/nls'; import { IColorTheme, IWorkbenchThemeService } from 'vs/workbench/services/themes/common/workbenchThemeService'; @@ -133,8 +134,9 @@ export class TextCellComponent extends CellView implements OnInit, OnChanges { } else { this._content = this.sanitizeContent(this.cellModel.source); } - // todo: pass in the notebook filename instead of undefined value - this._commandService.executeCommand('notebook.showPreview', undefined, this._content).then((htmlcontent) => { + + this._commandService.executeCommand('notebook.showPreview', this.cellModel.notebookModel.notebookUri, this._content).then((htmlcontent) => { + htmlcontent = this.convertVscodeResourceToFileInSubDirectories(htmlcontent); let outputElement = this.output.nativeElement; outputElement.innerHTML = htmlcontent; }); @@ -149,6 +151,24 @@ export class TextCellComponent extends CellView implements OnInit, OnChanges { return content; } + // Only replace vscode-resource with file when in the same (or a sub) directory + // This matches Jupyter Notebook viewer behavior + private convertVscodeResourceToFileInSubDirectories(htmlContent: string): string { + let htmlContentCopy = htmlContent; + while (htmlContentCopy.search('(?<=img src=\"vscode-resource:)') > 0) { + let pathStartIndex = htmlContentCopy.search('(?<=img src=\"vscode-resource:)'); + let pathEndIndex = htmlContentCopy.indexOf('\" ', pathStartIndex); + let filePath = htmlContentCopy.substring(pathStartIndex, pathEndIndex); + // If the asset is in the same folder or a subfolder, replace 'vscode-resource:' with 'file:', so the image is visible + if (!path.relative(path.dirname(this.cellModel.notebookModel.notebookUri.fsPath), filePath).includes('..')) { + // ok to change from vscode-resource: to file: + htmlContent = htmlContent.replace('vscode-resource:'+ filePath, 'file:' + filePath); + } + htmlContentCopy = htmlContentCopy.slice(pathEndIndex); + } + return htmlContent; + } + // Todo: implement layout public layout() {