Skip to content

Commit

Permalink
Merge pull request #88 from susanwere/fix-pasting-html-links
Browse files Browse the repository at this point in the history
Fix: Pasting of html with similar matches that have links
  • Loading branch information
keithamus authored Feb 19, 2024
2 parents 7bc6760 + c687d1b commit ca6eeb0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/paste-markdown-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
}

Expand Down
60 changes: 60 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,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 = `<meta charset='utf-8'><span>
foo bar baz <a href="https://www.abcxyz.com/">bar</a></span>`
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 = `<meta charset='utf-8'>
<p>foo bar
bar baz <a href="https://www.abcxyz.org/">bar</a> </p>
<p>baz <a href="https://www.abcxyz.com/">baz</a> foo</p>`
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 = `<meta charset='utf-8'><span>
${commonSentence}: <a href="https://www.example.com/">example</a> and <a href="https://www.example.com/">example</a>.</span>`
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 = `<meta charset='utf-8'>
<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>`
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 = `<meta charset='utf-8'>
<p>foo bar <a href="https://www.abcxyz.org/">foo</a> bar foo <a href="https://example.com/">πŸš€ bar πŸš€</a></p>`
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)
})
})
})

Expand Down

0 comments on commit ca6eeb0

Please sign in to comment.