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
This commit is contained in:
Barbara Valdez
2020-10-16 17:51:14 -07:00
committed by GitHub
parent 39b6cc193f
commit 2801e59edc

View File

@@ -473,7 +473,9 @@ export class TextCellComponent extends CellView implements OnInit, OnChanges {
filter: 'img', filter: 'img',
replacement: (content, node) => { replacement: (content, node) => {
if (node?.src) { 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) { if (relativePath) {
return `![${node.alt}](${relativePath})`; return `![${node.alt}](${relativePath})`;
} }
@@ -484,12 +486,14 @@ export class TextCellComponent extends CellView implements OnInit, OnChanges {
this.turndownService.addRule('a', { this.turndownService.addRule('a', {
filter: 'a', filter: 'a',
replacement: (content, node) => { replacement: (content, node) => {
if (node?.href) { //On Windows, if notebook is not trusted then the href attr is removed for all non-web URL links
let relativePath = this.findPathRelativeToContent(node.href); // 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) { if (relativePath) {
return `[${node.innerText}](${relativePath})`; return `[${node.innerText}](${relativePath})`;
} }
}
return `[${node.innerText}](${node.href})`; return `[${node.innerText}](${node.href})`;
} }
}); });
@@ -503,19 +507,24 @@ export class TextCellComponent extends CellView implements OnInit, OnChanges {
this.cellModel.active = true; this.cellModel.active = true;
this._model.updateActiveCell(this.cellModel); this._model.updateActiveCell(this.cellModel);
} }
}
private findPathRelativeToContent(elementContent: string): string { export function findPathRelativeToContent(notebookFolder: string, contentPath: URI | undefined): string {
let notebookFolder = this.notebookUri ? path.join(path.dirname(this.notebookUri.fsPath), path.sep) : '';
if (notebookFolder) { if (notebookFolder) {
let absolutePathURI = URI.parse(elementContent); if (contentPath?.scheme === 'file') {
if (absolutePathURI?.scheme === 'file') { let relativePath = path.relative(notebookFolder, contentPath.fsPath);
let relativePath = path.relative(notebookFolder, absolutePathURI.fsPath); //if path contains whitespaces then it's not identified as a link
return relativePath ? relativePath : ''; 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) { function preventDefaultAndExecCommand(e: KeyboardEvent, commandId: string) {
// use preventDefault() to avoid invoking the editor's select all // use preventDefault() to avoid invoking the editor's select all