From 2801e59edc869a27dc1a6232a56d973f901e21ff Mon Sep 17 00:00:00 2001 From: Barbara Valdez <34872381+barbaravaldez@users.noreply.github.com> Date: Fri, 16 Oct 2020 17:51:14 -0700 Subject: [PATCH] Fix links on WYSIWYG (#12952) * fix for removed links in untrusted notebooks * replace whitespaces on link for %20 * remove dot from hyperlinks * Address PR comments * Change name of variable --- .../browser/cellViews/textCell.component.ts | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/sql/workbench/contrib/notebook/browser/cellViews/textCell.component.ts b/src/sql/workbench/contrib/notebook/browser/cellViews/textCell.component.ts index 7904cd951f..0a21a79afe 100644 --- a/src/sql/workbench/contrib/notebook/browser/cellViews/textCell.component.ts +++ b/src/sql/workbench/contrib/notebook/browser/cellViews/textCell.component.ts @@ -473,7 +473,9 @@ export class TextCellComponent extends CellView implements OnInit, OnChanges { filter: 'img', replacement: (content, node) => { if (node?.src) { - let relativePath = this.findPathRelativeToContent(node.src); + let imgPath = URI.parse(node.src); + const notebookFolder: string = this.notebookUri ? path.join(path.dirname(this.notebookUri.fsPath), path.sep) : ''; + let relativePath = findPathRelativeToContent(notebookFolder, imgPath); if (relativePath) { return `![${node.alt}](${relativePath})`; } @@ -484,11 +486,13 @@ export class TextCellComponent extends CellView implements OnInit, OnChanges { this.turndownService.addRule('a', { filter: 'a', replacement: (content, node) => { - if (node?.href) { - let relativePath = this.findPathRelativeToContent(node.href); - if (relativePath) { - return `[${node.innerText}](${relativePath})`; - } + //On Windows, if notebook is not trusted then the href attr is removed for all non-web URL links + // href contains either a hyperlink or a URI-encoded absolute path. (See resolveUrls method in notebookMarkdown.ts) + const notebookLink = node.href ? URI.parse(node.href) : URI.file(node.title); + const notebookFolder = this.notebookUri ? path.join(path.dirname(this.notebookUri.fsPath), path.sep) : ''; + let relativePath = findPathRelativeToContent(notebookFolder, notebookLink); + if (relativePath) { + return `[${node.innerText}](${relativePath})`; } return `[${node.innerText}](${node.href})`; } @@ -503,18 +507,23 @@ export class TextCellComponent extends CellView implements OnInit, OnChanges { this.cellModel.active = true; this._model.updateActiveCell(this.cellModel); } +} - private findPathRelativeToContent(elementContent: string): string { - let notebookFolder = this.notebookUri ? path.join(path.dirname(this.notebookUri.fsPath), path.sep) : ''; - if (notebookFolder) { - let absolutePathURI = URI.parse(elementContent); - if (absolutePathURI?.scheme === 'file') { - let relativePath = path.relative(notebookFolder, absolutePathURI.fsPath); - return relativePath ? relativePath : ''; +export function findPathRelativeToContent(notebookFolder: string, contentPath: URI | undefined): string { + if (notebookFolder) { + if (contentPath?.scheme === 'file') { + let relativePath = path.relative(notebookFolder, contentPath.fsPath); + //if path contains whitespaces then it's not identified as a link + relativePath = relativePath.replace(/\s/g, '%20'); + if (relativePath.startsWith(path.join('..', path.sep) || path.join('.', path.sep))) { + return relativePath; + } else { + // if the relative path does not contain ./ at the beginning, we need to add it so it's recognized as a link + return `.${path.join(path.sep, relativePath)}`; } } - return ''; } + return ''; } function preventDefaultAndExecCommand(e: KeyboardEvent, commandId: string) {