diff --git a/src/paste-markdown-html.ts b/src/paste-markdown-html.ts index ed9cb1d..eec451d 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 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 + } + // 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..e385020 100644 --- a/test/test.js +++ b/test/test.js @@ -360,6 +360,66 @@ 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) + }) + + 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 = ` + ` + 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) + }) + + 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) + }) }) })