Skip to content

Commit 6670358

Browse files
committed
Prevent linkification when replacing an URL
1 parent a447563 commit 6670358

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/paste-markdown-link.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ function onPaste(event: ClipboardEvent) {
2222

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

2629
event.stopPropagation()
2730
event.preventDefault()

test/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ describe('paste-markdown', function () {
3131
assert.equal(textarea.value, 'The examples can be found [here](https://github.com).')
3232
})
3333

34+
it('turns pasted urls on selected urls not into markdown links', function () {
35+
// eslint-disable-next-line i18n-text/no-en
36+
textarea.value = 'The examples can be found here: https://docs.github.com'
37+
textarea.setSelectionRange(32, 55)
38+
paste(textarea, {'text/plain': 'https://github.com'})
39+
// Synthetic paste events don't manipulate the DOM. The same textarea value
40+
// means that the event handler didn't fire and normal paste happened.
41+
assert.equal(textarea.value, 'The examples can be found here: https://docs.github.com')
42+
})
43+
3444
it('turns html tables into markdown', function () {
3545
const data = {
3646
'text/html': `

0 commit comments

Comments
 (0)