Skip to content

Commit 7bbe4ac

Browse files
Susan WereSusan Were
authored andcommitted
update changes to fix pasting of markdown
1 parent a4ae795 commit 7bbe4ac

File tree

2 files changed

+44
-47
lines changed

2 files changed

+44
-47
lines changed

src/paste-markdown-html.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,11 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
8383
const markdownFoundIndex = markdown.indexOf(text, markdownIgnoreBeforeIndex)
8484

8585
if (markdownFoundIndex >= 0) {
86-
if (isLink(currentNode)) {
87-
const markdownLink = linkify(currentNode, text)
88-
// Transform 'example link plus more text' into 'example [link](example link) plus more text'
89-
// Method: 'example [link](example link) plus more text' = 'example ' + '[link](example link)' + ' plus more text'
90-
markdown =
91-
markdown.slice(0, markdownFoundIndex) + markdownLink + markdown.slice(markdownFoundIndex + text.length)
92-
markdownIgnoreBeforeIndex = markdownFoundIndex + markdownLink.length
93-
}
86+
const markdownLink = linkify(currentNode, text)
87+
// Transform 'example link plus more text' into 'example [link](example link) plus more text'
88+
// Method: 'example [link](example link) plus more text' = 'example ' + '[link](example link)' + ' plus more text'
89+
markdown = markdown.slice(0, markdownFoundIndex) + markdownLink + markdown.slice(markdownFoundIndex + text.length)
90+
markdownIgnoreBeforeIndex = markdownFoundIndex + markdownLink.length
9491
}
9592

9693
currentNode = walker.nextNode()

test/test.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -358,45 +358,6 @@ describe('paste-markdown', function () {
358358
assert.equal(textarea.value, markdownSentence)
359359
})
360360

361-
it('finds the right link when identical labels are present chrome and edge', function () {
362-
// eslint-disable-next-line github/unescaped-html-literal
363-
const sentence = `<meta charset='utf-8'>
364-
<p>foo bar<br>bar<span> </span><a href="https://www.abcxyz.org/">bar</a></p><p>bar<span> </span><a href="https://www.abcxyz.com/">bar</a><span> </span>foo</p>`
365-
366-
const markdownSentence = `foo bar
367-
bar [bar](https://www.abcxyz.org/)
368-
369-
bar [bar](https://www.abcxyz.com/) foo`
370-
371-
const plaintextSentence = `foo bar
372-
bar bar
373-
374-
bar bar foo`
375-
376-
paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
377-
assert.equal(textarea.value, markdownSentence)
378-
})
379-
380-
it('finds the right link when identical labels are present firefox', function () {
381-
// eslint-disable-next-line github/unescaped-html-literal
382-
const sentence = `<meta charset='utf-8'>
383-
<div><div><p>foo bar<br>
384-
bar <a href="https://www.abcxyz.org/">bar</a></p>
385-
<p>bar <a href="https://www.abcxyz.com/">bar</a> foo</p></div></div>`
386-
const markdownSentence = `foo bar
387-
bar [bar](https://www.abcxyz.org/)
388-
389-
bar [bar](https://www.abcxyz.com/) foo`
390-
391-
const plaintextSentence = `foo bar
392-
bar bar
393-
394-
bar bar foo`
395-
396-
paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
397-
assert.equal(textarea.value, markdownSentence)
398-
})
399-
400361
it('pastes markdown with links correctly when identical labels are present', function () {
401362
// eslint-disable-next-line github/unescaped-html-literal
402363
const sentence = `<meta charset='utf-8'><span>
@@ -445,6 +406,45 @@ bar bar foo`
445406
assert.equal(textarea.value, markdownSentence)
446407
})
447408

409+
it('finds the right link when identical labels are present chrome and edge', function () {
410+
// eslint-disable-next-line github/unescaped-html-literal
411+
const sentence = `<meta charset='utf-8'>
412+
<p>foo bar<br>bar<span> </span><a href="https://www.abcxyz.org/">bar</a></p><p>bar<span> </span><a href="https://www.abcxyz.com/">bar</a><span> </span>foo</p>`
413+
414+
const markdownSentence = `foo bar
415+
bar [bar](https://www.abcxyz.org/)
416+
417+
bar [bar](https://www.abcxyz.com/) foo`
418+
419+
const plaintextSentence = `foo bar
420+
bar bar
421+
422+
bar bar foo`
423+
424+
paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
425+
assert.equal(textarea.value, markdownSentence)
426+
})
427+
428+
it('finds the right link when identical labels are present firefox', function () {
429+
// eslint-disable-next-line github/unescaped-html-literal
430+
const sentence = `<meta charset='utf-8'>
431+
<div><div><p>foo bar<br>
432+
bar <a href="https://www.abcxyz.org/">bar</a></p>
433+
<p>bar <a href="https://www.abcxyz.com/">bar</a> foo</p></div></div>`
434+
const markdownSentence = `foo bar
435+
bar [bar](https://www.abcxyz.org/)
436+
437+
bar [bar](https://www.abcxyz.com/) foo`
438+
439+
const plaintextSentence = `foo bar
440+
bar bar
441+
442+
bar bar foo`
443+
444+
paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
445+
assert.equal(textarea.value, markdownSentence)
446+
})
447+
448448
it('skip markdown formatting with (Ctrl+Shift+v)', function () {
449449
const data = {
450450
'text/html': tableHtml

0 commit comments

Comments
 (0)