From 2a7b90fd70378d0e9429747841a5c1efc09f4219 Mon Sep 17 00:00:00 2001 From: Vasu Bhog Date: Wed, 25 Nov 2020 12:42:07 -0600 Subject: [PATCH] Fix WYSIWYG text + image paste (#13542) * Fix WYSIWYG text + image paste * add test for a link and text --- .../notebook/browser/htmlMarkdownConverter.ts | 13 +++++++++++-- .../test/browser/htmlMarkdownConverter.test.ts | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/sql/workbench/contrib/notebook/browser/htmlMarkdownConverter.ts b/src/sql/workbench/contrib/notebook/browser/htmlMarkdownConverter.ts index 2eba697968..ba8cb7c1fb 100644 --- a/src/sql/workbench/contrib/notebook/browser/htmlMarkdownConverter.ts +++ b/src/sql/workbench/contrib/notebook/browser/htmlMarkdownConverter.ts @@ -101,10 +101,11 @@ export class HTMLMarkdownConverter { const notebookFolder = this.notebookUri ? path.join(path.dirname(this.notebookUri.fsPath), path.sep) : ''; let relativePath = findPathRelativeToContent(notebookFolder, notebookLink); node.innerText = escapeAngleBrackets(node.innerText); + content = escapeAngleBrackets(content); if (relativePath) { return `[${node.innerText}](${relativePath})`; } - return `[${node.innerText}](${node.href})`; + return `[${content}](${node.href})`; } }); this.turndownService.addRule('listItem', { @@ -137,14 +138,22 @@ export class HTMLMarkdownConverter { this.turndownService.addRule('p', { filter: 'p', replacement: function (content, node) { + let isAnchorElement: boolean = false; node.childNodes.forEach(c => { if (c.nodeType === Node.TEXT_NODE) { c.nodeValue = escapeAngleBrackets(c.textContent); } else if (c.nodeType === Node.ELEMENT_NODE) { c.innerText = escapeAngleBrackets(c.textContent); + if (c.nodeName === 'A') { + isAnchorElement = true; + } } }); - return '\n\n' + node.innerHTML.replace(/</gi, '<').replace(/>/gi, '>').replace(/ /gi, '') + '\n\n'; + if (isAnchorElement) { + return content; + } else { + return '\n\n' + node.innerHTML.replace(/</gi, '<').replace(/>/gi, '>').replace(/ /gi, '') + '\n\n'; + } } }); this.turndownService.addRule('heading', { diff --git a/src/sql/workbench/contrib/notebook/test/browser/htmlMarkdownConverter.test.ts b/src/sql/workbench/contrib/notebook/test/browser/htmlMarkdownConverter.test.ts index f09fcadffd..241401e143 100644 --- a/src/sql/workbench/contrib/notebook/test/browser/htmlMarkdownConverter.test.ts +++ b/src/sql/workbench/contrib/notebook/test/browser/htmlMarkdownConverter.test.ts @@ -137,6 +137,8 @@ suite('HTML Markdown Converter', function (): void { assert.equal(htmlMarkdownConverter.convert(htmlString), '[msft](https://www.microsoft.com/images/msft.png)', 'Basic https link test failed'); htmlString = 'msft'; assert.equal(htmlMarkdownConverter.convert(htmlString), '[msft](http://www.microsoft.com/images/msft.png)', 'Basic http link test failed'); + htmlString = 'Test msft'; + assert.equal(htmlMarkdownConverter.convert(htmlString), 'Test [msft](http://www.microsoft.com/images/msft.png)', 'Basic http link + text test failed'); }); test('Should transform
  • tags', () => {