From e4390db7792a1948311423e599cebe82c14ade31 Mon Sep 17 00:00:00 2001 From: Chris LaFreniere <40371649+chlafreniere@users.noreply.github.com> Date: Thu, 5 Nov 2020 18:21:33 -0800 Subject: [PATCH] WYSIWYG fix list nesting (#13251) * Improvements to nested lists * OL tests and PR feedback --- .../notebook/browser/htmlMarkdownConverter.ts | 26 +++++++++++++++++++ .../browser/htmlMarkdownConverter.test.ts | 14 ++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/sql/workbench/contrib/notebook/browser/htmlMarkdownConverter.ts b/src/sql/workbench/contrib/notebook/browser/htmlMarkdownConverter.ts index e88f86ba5f..63411d9bf8 100644 --- a/src/sql/workbench/contrib/notebook/browser/htmlMarkdownConverter.ts +++ b/src/sql/workbench/contrib/notebook/browser/htmlMarkdownConverter.ts @@ -105,6 +105,32 @@ export class HTMLMarkdownConverter { return `[${node.innerText}](${node.href})`; } }); + this.turndownService.addRule('listItem', { + filter: 'li', + replacement: function (content, node, options) { + content = content + .replace(/^\n+/, '') // remove leading newlines + .replace(/\n+$/, '\n') // replace trailing newlines with just a single one + .replace(/\n/gm, '\n '); // indent + let prefix = options.bulletListMarker + ' '; + let parent = node.parentNode; + let nestedCount = 0; + if (parent.nodeName === 'OL') { + let start = parent.getAttribute('start'); + let index = Array.prototype.indexOf.call(parent.children, node); + prefix = (start ? Number(start) + index : index + 1) + '. '; + } else if (parent.nodeName === 'UL') { + while (parent?.nodeName === 'UL') { + nestedCount++; + parent = parent?.parentNode; + } + prefix = (' '.repeat(nestedCount - 1)) + options.bulletListMarker + ' '; + } + return ( + prefix + content + (node.nextSibling && !/\n$/.test(content) ? '\n' : '') + ); + } + }); } } 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 d09ad07e55..c3333b8b2b 100644 --- a/src/sql/workbench/contrib/notebook/test/browser/htmlMarkdownConverter.test.ts +++ b/src/sql/workbench/contrib/notebook/test/browser/htmlMarkdownConverter.test.ts @@ -138,4 +138,18 @@ suite('HTML Markdown Converter', function (): void { htmlString = 'msft'; assert.equal(htmlMarkdownConverter.convert(htmlString), '[msft](http://www.microsoft.com/images/msft.png)', 'Basic http link test failed'); }); + test('Should transform