Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update changes to fix pasting of markdown
  • Loading branch information
Susan Were Susan Were
Susan Were authored and Susan Were committed Mar 20, 2024
commit 7bbe4ac48037bb1926fa4e064837e9e49c36b62a
13 changes: 5 additions & 8 deletions src/paste-markdown-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,11 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
const markdownFoundIndex = markdown.indexOf(text, markdownIgnoreBeforeIndex)

if (markdownFoundIndex >= 0) {
if (isLink(currentNode)) {
const markdownLink = linkify(currentNode, text)
// Transform 'example link plus more text' into 'example [link](example link) plus more text'
// Method: 'example [link](example link) plus more text' = 'example ' + '[link](example link)' + ' plus more text'
markdown =
markdown.slice(0, markdownFoundIndex) + markdownLink + markdown.slice(markdownFoundIndex + text.length)
markdownIgnoreBeforeIndex = markdownFoundIndex + markdownLink.length
}
const markdownLink = linkify(currentNode, text)
// Transform 'example link plus more text' into 'example [link](example link) plus more text'
// Method: 'example [link](example link) plus more text' = 'example ' + '[link](example link)' + ' plus more text'
markdown = markdown.slice(0, markdownFoundIndex) + markdownLink + markdown.slice(markdownFoundIndex + text.length)
markdownIgnoreBeforeIndex = markdownFoundIndex + markdownLink.length
}

currentNode = walker.nextNode()
Expand Down
78 changes: 39 additions & 39 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,45 +358,6 @@ describe('paste-markdown', function () {
assert.equal(textarea.value, markdownSentence)
})

it('finds the right link when identical labels are present chrome and edge', function () {
// eslint-disable-next-line github/unescaped-html-literal
const sentence = `<meta charset='utf-8'>
<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>`

const markdownSentence = `foo bar
bar [bar](https://www.abcxyz.org/)

bar [bar](https://www.abcxyz.com/) foo`

const plaintextSentence = `foo bar
bar bar

bar bar foo`

paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
assert.equal(textarea.value, markdownSentence)
})

it('finds the right link when identical labels are present firefox', function () {
// eslint-disable-next-line github/unescaped-html-literal
const sentence = `<meta charset='utf-8'>
<div><div><p>foo bar<br>
bar <a href="https://www.abcxyz.org/">bar</a></p>
<p>bar <a href="https://www.abcxyz.com/">bar</a> foo</p></div></div>`
const markdownSentence = `foo bar
bar [bar](https://www.abcxyz.org/)

bar [bar](https://www.abcxyz.com/) foo`

const plaintextSentence = `foo bar
bar bar

bar bar foo`

paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
assert.equal(textarea.value, markdownSentence)
})

it('pastes markdown with links correctly when identical labels are present', function () {
Comment thread
susanwere marked this conversation as resolved.
// eslint-disable-next-line github/unescaped-html-literal
const sentence = `<meta charset='utf-8'><span>
Expand Down Expand Up @@ -445,6 +406,45 @@ bar bar foo`
assert.equal(textarea.value, markdownSentence)
})

it('finds the right link when identical labels are present chrome and edge', function () {
// eslint-disable-next-line github/unescaped-html-literal
const sentence = `<meta charset='utf-8'>
<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>`

const markdownSentence = `foo bar
bar [bar](https://www.abcxyz.org/)

bar [bar](https://www.abcxyz.com/) foo`

const plaintextSentence = `foo bar
bar bar

bar bar foo`

paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
assert.equal(textarea.value, markdownSentence)
})

it('finds the right link when identical labels are present firefox', function () {
// eslint-disable-next-line github/unescaped-html-literal
const sentence = `<meta charset='utf-8'>
<div><div><p>foo bar<br>
bar <a href="https://www.abcxyz.org/">bar</a></p>
<p>bar <a href="https://www.abcxyz.com/">bar</a> foo</p></div></div>`
const markdownSentence = `foo bar
bar [bar](https://www.abcxyz.org/)

bar [bar](https://www.abcxyz.com/) foo`

const plaintextSentence = `foo bar
bar bar

bar bar foo`

paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
assert.equal(textarea.value, markdownSentence)
})

it('skip markdown formatting with (Ctrl+Shift+v)', function () {
const data = {
'text/html': tableHtml
Expand Down