Skip to content
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

Prevent linkification when replacing a URL #30

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions src/paste-markdown-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ function onPaste(event: ClipboardEvent) {

const selectedText = field.value.substring(field.selectionStart, field.selectionEnd)
if (!selectedText.length) return
// Prevent linkification when replacing an URL
// Trim whitespace in case whitespace is selected by mistake or by intention
if (isURL(selectedText.trim())) return

event.stopPropagation()
event.preventDefault()
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ describe('paste-markdown', function () {
assert.equal(textarea.value, 'The examples can be found [here](https://github.com).')
})

it("doesn't paste a markdown URL when pasting over a selected URL", function () {
// eslint-disable-next-line i18n-text/no-en
textarea.value = 'The examples can be found here: https://docs.github.com'
textarea.setSelectionRange(32, 55)
paste(textarea, {'text/plain': 'https://github.com'})
// Synthetic paste events don't manipulate the DOM. The same textarea value
// means that the event handler didn't fire and normal paste happened.
Comment on lines +39 to +40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good comment since the test behaviour is weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koddsson 💯 "Stole" it from

paste-markdown/test/test.js

Lines 102 to 103 in a447563

// Synthetic paste events don't manipulate the DOM. A empty textarea
// means that the event handler didn't fire and normal paste happened.
😊

assert.equal(textarea.value, 'The examples can be found here: https://docs.github.com')
})

it('turns html tables into markdown', function () {
const data = {
'text/html': `
Expand Down