Skip to content

Fix: Pasting of html with similar matches that have links #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -360,6 +360,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