From 1c90905391fe537b8ce016523782388478e9717f Mon Sep 17 00:00:00 2001 From: Susan Were Date: Mon, 31 Jul 2023 13:38:20 +0000 Subject: [PATCH 1/3] fix pasting of html with links --- src/paste-markdown-html.ts | 7 +++++-- test/test.js | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/paste-markdown-html.ts b/src/paste-markdown-html.ts index ed9cb1d..61e0986 100644 --- a/src/paste-markdown-html.ts +++ b/src/paste-markdown-html.ts @@ -66,6 +66,11 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string { ? (currentNode.textContent || '').replace(/[\t\n\r ]+/g, ' ') : (currentNode.firstChild as Text)?.wholeText || '' + // // update current index without link + if (!isLink(currentNode)) { + markdownIgnoreBeforeIndex += text.replace(/[\t\n\r ]+/g, ' ').trimStart().length + } + // No need to transform whitespace if (isEmptyString(text)) { currentNode = walker.nextNode() @@ -83,8 +88,6 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string { markdown = markdown.slice(0, markdownFoundIndex) + markdownLink + markdown.slice(markdownFoundIndex + text.length) markdownIgnoreBeforeIndex = markdownFoundIndex + markdownLink.length - } else { - markdownIgnoreBeforeIndex = markdownFoundIndex + text.length } } diff --git a/test/test.js b/test/test.js index f2ee67a..6d12254 100644 --- a/test/test.js +++ b/test/test.js @@ -360,6 +360,30 @@ describe('paste-markdown', function () { paste(textarea, data) assert.include(textarea.value, tableMarkdown) }) + + it('pastes markdown with links correctly when identical labels are present', function () { + // eslint-disable-next-line github/unescaped-html-literal + const sentence = ` + foo bar baz bar` + const plaintextSentence = 'foo bar baz bar' + const markdownSentence = 'foo bar baz [bar](https://www.abcxyz.com/)' + + paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence}) + assert.equal(textarea.value, markdownSentence) + }) + + it('pastes markdown with line breaks and links correctly when identical labels are present', function () { + // eslint-disable-next-line github/unescaped-html-literal + const sentence = ` +

foo bar + bar baz bar

+

baz baz foo

` + const plaintextSentence = 'foo bar bar baz bar baz baz foo' + const markdownSentence = 'foo bar bar baz [bar](https://www.abcxyz.org/) baz [baz](https://www.abcxyz.com/) foo' + + paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence}) + assert.equal(textarea.value, markdownSentence) + }) }) }) From 812ff10016869bdb512e2d9dd1ec091e41dc8ce2 Mon Sep 17 00:00:00 2001 From: Susan Were Date: Tue, 1 Aug 2023 11:23:12 +0000 Subject: [PATCH 2/3] add more tests --- src/paste-markdown-html.ts | 2 +- test/test.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/paste-markdown-html.ts b/src/paste-markdown-html.ts index 61e0986..eec451d 100644 --- a/src/paste-markdown-html.ts +++ b/src/paste-markdown-html.ts @@ -66,7 +66,7 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string { ? (currentNode.textContent || '').replace(/[\t\n\r ]+/g, ' ') : (currentNode.firstChild as Text)?.wholeText || '' - // // update current index without link + // update value of markdownIgnoreBeforeIndex with current index if the current node is not a link if (!isLink(currentNode)) { markdownIgnoreBeforeIndex += text.replace(/[\t\n\r ]+/g, ' ').trimStart().length } diff --git a/test/test.js b/test/test.js index 6d12254..6995a5f 100644 --- a/test/test.js +++ b/test/test.js @@ -384,6 +384,41 @@ describe('paste-markdown', function () { paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence}) assert.equal(textarea.value, markdownSentence) }) + + it('pastes markdown with multiple links and labels correctly', function () { + // eslint-disable-next-line i18n-text/no-en + const commonSentence = 'Great example for example resources for developers' + // eslint-disable-next-line github/unescaped-html-literal + const sentence = ` + ${commonSentence}: example and example.` + const plaintextSentence = `${commonSentence}: example and example.` + const markdownSentence = `${commonSentence}: [example](https://www.example.com/) and [example](https://www.example.com/).` + + paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence}) + assert.equal(textarea.value, markdownSentence) + }) + + it('pastes markdown with link labels that contains special characters in html', function () { + // eslint-disable-next-line github/unescaped-html-literal + const sentence = ` + foo bar foo&bar` + const plaintextSentence = 'foo bar foo&bar' + const markdownSentence = '[foo bar](https://www.abcxyz.org/) [foo&bar](https://example.com/?q=foo&bar=baz)' + + paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence}) + assert.equal(textarea.value, markdownSentence) + }) + + it('pastes markdown with link labels that contains emojis in html', function () { + // eslint-disable-next-line github/unescaped-html-literal + const sentence = ` +

foo bar foo bar foo 🚀 bar 🚀

` + const plaintextSentence = 'foo bar foo bar foo 🚀 bar 🚀' + const markdownSentence = 'foo bar [foo](https://www.abcxyz.org/) bar foo [🚀 bar 🚀](https://example.com/)' + + paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence}) + assert.equal(textarea.value, markdownSentence) + }) }) }) From c687d1b21559fb52527e501ef391f2c1d608b411 Mon Sep 17 00:00:00 2001 From: Susan Were Date: Tue, 1 Aug 2023 11:27:26 +0000 Subject: [PATCH 3/3] improve tests --- test/test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/test.js b/test/test.js index 6995a5f..e385020 100644 --- a/test/test.js +++ b/test/test.js @@ -401,9 +401,10 @@ describe('paste-markdown', function () { it('pastes markdown with link labels that contains special characters in html', function () { // eslint-disable-next-line github/unescaped-html-literal const sentence = ` - foo bar foo&bar` - const plaintextSentence = 'foo bar foo&bar' - const markdownSentence = '[foo bar](https://www.abcxyz.org/) [foo&bar](https://example.com/?q=foo&bar=baz)' +

foo&bar foo bar foo&bar

` + const plaintextSentence = 'foo&bar foo bar foo&bar' + const markdownSentence = + 'foo&bar [foo bar](https://www.abcxyz.org/) [foo&bar](https://example.com/?q=foo&bar=baz)' paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence}) assert.equal(textarea.value, markdownSentence)