Skip to content

Commit 1c90905

Browse files
committed
fix pasting of html with links
1 parent 7186fa0 commit 1c90905

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/paste-markdown-html.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
6666
? (currentNode.textContent || '').replace(/[\t\n\r ]+/g, ' ')
6767
: (currentNode.firstChild as Text)?.wholeText || ''
6868

69+
// // update current index without link
70+
if (!isLink(currentNode)) {
71+
markdownIgnoreBeforeIndex += text.replace(/[\t\n\r ]+/g, ' ').trimStart().length
72+
}
73+
6974
// No need to transform whitespace
7075
if (isEmptyString(text)) {
7176
currentNode = walker.nextNode()
@@ -83,8 +88,6 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
8388
markdown =
8489
markdown.slice(0, markdownFoundIndex) + markdownLink + markdown.slice(markdownFoundIndex + text.length)
8590
markdownIgnoreBeforeIndex = markdownFoundIndex + markdownLink.length
86-
} else {
87-
markdownIgnoreBeforeIndex = markdownFoundIndex + text.length
8891
}
8992
}
9093

test/test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,30 @@ describe('paste-markdown', function () {
360360
paste(textarea, data)
361361
assert.include(textarea.value, tableMarkdown)
362362
})
363+
364+
it('pastes markdown with links correctly when identical labels are present', function () {
365+
// eslint-disable-next-line github/unescaped-html-literal
366+
const sentence = `<meta charset='utf-8'><span>
367+
foo bar baz <a href="https://www.abcxyz.com/">bar</a></span>`
368+
const plaintextSentence = 'foo bar baz bar'
369+
const markdownSentence = 'foo bar baz [bar](https://www.abcxyz.com/)'
370+
371+
paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
372+
assert.equal(textarea.value, markdownSentence)
373+
})
374+
375+
it('pastes markdown with line breaks and links correctly when identical labels are present', function () {
376+
// eslint-disable-next-line github/unescaped-html-literal
377+
const sentence = `<meta charset='utf-8'>
378+
<p>foo bar
379+
bar baz <a href="https://www.abcxyz.org/">bar</a> </p>
380+
<p>baz <a href="https://www.abcxyz.com/">baz</a> foo</p>`
381+
const plaintextSentence = 'foo bar bar baz bar baz baz foo'
382+
const markdownSentence = 'foo bar bar baz [bar](https://www.abcxyz.org/) baz [baz](https://www.abcxyz.com/) foo'
383+
384+
paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
385+
assert.equal(textarea.value, markdownSentence)
386+
})
363387
})
364388
})
365389

0 commit comments

Comments
 (0)