Skip to content

Commit ca6eeb0

Browse files
authored
Merge pull request #88 from susanwere/fix-pasting-html-links
Fix: Pasting of html with similar matches that have links
2 parents 7bc6760 + c687d1b commit ca6eeb0

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

β€Žsrc/paste-markdown-html.ts

+5-2
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 value of markdownIgnoreBeforeIndex with current index if the current node is not a 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

+60
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,66 @@ describe('paste-markdown', function () {
382382
paste(textarea, data)
383383
assert.include(textarea.value, tableMarkdown)
384384
})
385+
386+
it('pastes markdown with links correctly when identical labels are present', function () {
387+
// eslint-disable-next-line github/unescaped-html-literal
388+
const sentence = `<meta charset='utf-8'><span>
389+
foo bar baz <a href="https://www.abcxyz.com/">bar</a></span>`
390+
const plaintextSentence = 'foo bar baz bar'
391+
const markdownSentence = 'foo bar baz [bar](https://www.abcxyz.com/)'
392+
393+
paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
394+
assert.equal(textarea.value, markdownSentence)
395+
})
396+
397+
it('pastes markdown with line breaks and links correctly when identical labels are present', function () {
398+
// eslint-disable-next-line github/unescaped-html-literal
399+
const sentence = `<meta charset='utf-8'>
400+
<p>foo bar
401+
bar baz <a href="https://www.abcxyz.org/">bar</a> </p>
402+
<p>baz <a href="https://www.abcxyz.com/">baz</a> foo</p>`
403+
const plaintextSentence = 'foo bar bar baz bar baz baz foo'
404+
const markdownSentence = 'foo bar bar baz [bar](https://www.abcxyz.org/) baz [baz](https://www.abcxyz.com/) foo'
405+
406+
paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
407+
assert.equal(textarea.value, markdownSentence)
408+
})
409+
410+
it('pastes markdown with multiple links and labels correctly', function () {
411+
// eslint-disable-next-line i18n-text/no-en
412+
const commonSentence = 'Great example for example resources for developers'
413+
// eslint-disable-next-line github/unescaped-html-literal
414+
const sentence = `<meta charset='utf-8'><span>
415+
${commonSentence}: <a href="https://www.example.com/">example</a> and <a href="https://www.example.com/">example</a>.</span>`
416+
const plaintextSentence = `${commonSentence}: example and example.`
417+
const markdownSentence = `${commonSentence}: [example](https://www.example.com/) and [example](https://www.example.com/).`
418+
419+
paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
420+
assert.equal(textarea.value, markdownSentence)
421+
})
422+
423+
it('pastes markdown with link labels that contains special characters in html', function () {
424+
// eslint-disable-next-line github/unescaped-html-literal
425+
const sentence = `<meta charset='utf-8'>
426+
<p>foo&bar <a href="https://www.abcxyz.org/">foo bar</a> <a href="https://example.com/?q=foo&bar=baz">foo&bar</a></p>`
427+
const plaintextSentence = 'foo&bar foo bar foo&bar'
428+
const markdownSentence =
429+
'foo&bar [foo bar](https://www.abcxyz.org/) [foo&bar](https://example.com/?q=foo&bar=baz)'
430+
431+
paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
432+
assert.equal(textarea.value, markdownSentence)
433+
})
434+
435+
it('pastes markdown with link labels that contains emojis in html', function () {
436+
// eslint-disable-next-line github/unescaped-html-literal
437+
const sentence = `<meta charset='utf-8'>
438+
<p>foo bar <a href="https://www.abcxyz.org/">foo</a> bar foo <a href="https://example.com/">πŸš€ bar πŸš€</a></p>`
439+
const plaintextSentence = 'foo bar foo bar foo πŸš€ bar πŸš€'
440+
const markdownSentence = 'foo bar [foo](https://www.abcxyz.org/) bar foo [πŸš€ bar πŸš€](https://example.com/)'
441+
442+
paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
443+
assert.equal(textarea.value, markdownSentence)
444+
})
385445
})
386446
})
387447

0 commit comments

Comments
Β (0)