Skip to content

Commit 2cf7f94

Browse files
authored
Merge pull request #65 from ipc103/update-paste-links
Only paste links when pasted text is only a URL
2 parents a329717 + dedae8d commit 2cf7f94

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: src/paste-markdown-link.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function linkify(selectedText: string, text: string): string {
5555
return `[${selectedText}](${text})`
5656
}
5757

58+
const URL_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\/?\s*?$/i
5859
function isURL(url: string): boolean {
59-
return /^https?:\/\//i.test(url)
60+
return URL_REGEX.test(url)
6061
}

Diff for: test/test.js

+17
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ describe('paste-markdown', function () {
4343
assert.equal(textarea.value, 'The examples can be found [here](https://github.com).')
4444
})
4545

46+
it('creates a markdown link when the pasted url includes a trailing slash', function () {
47+
// eslint-disable-next-line i18n-text/no-en
48+
textarea.value = 'The examples can be found here.'
49+
textarea.setSelectionRange(26, 30)
50+
paste(textarea, {'text/plain': 'https://www.github.com/'})
51+
assert.equal(textarea.value, 'The examples can be found [here](https://www.github.com/).')
52+
})
53+
4654
it("doesn't paste a markdown URL when pasting over a selected URL", function () {
4755
// eslint-disable-next-line i18n-text/no-en
4856
textarea.value = 'The examples can be found here: https://docs.github.com'
@@ -66,6 +74,15 @@ describe('paste-markdown', function () {
6674
assert.equal(textarea.value, '@')
6775
})
6876

77+
it("doesn't paste markdown URL when additional text is being copied", function () {
78+
textarea.value = 'github'
79+
textarea.setSelectionRange(0, 6)
80+
paste(textarea, {'text/plain': 'https://github.com plus some other content'})
81+
// Synthetic paste events don't manipulate the DOM. The same textarea value
82+
// means that the event handler didn't fire and normal paste happened.
83+
assert.equal(textarea.value, 'github')
84+
})
85+
6986
it('turns html tables into markdown', function () {
7087
const data = {
7188
'text/html': tableHtml

0 commit comments

Comments
 (0)