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

Only paste links when pasted text is only a URL #65

Merged
merged 1 commit into from
Aug 11, 2022
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
3 changes: 2 additions & 1 deletion src/paste-markdown-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function linkify(selectedText: string, text: string): string {
return `[${selectedText}](${text})`
}

const URL_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\/?\s*?$/i
function isURL(url: string): boolean {
return /^https?:\/\//i.test(url)
return URL_REGEX.test(url)
}
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ describe('paste-markdown', function () {
assert.equal(textarea.value, 'The examples can be found [here](https://github.com).')
})

it('creates a markdown link when the pasted url includes a trailing slash', function () {
// eslint-disable-next-line i18n-text/no-en
textarea.value = 'The examples can be found here.'
textarea.setSelectionRange(26, 30)
paste(textarea, {'text/plain': 'https://www.github.com/'})
assert.equal(textarea.value, 'The examples can be found [here](https://www.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'
Expand All @@ -66,6 +74,15 @@ describe('paste-markdown', function () {
assert.equal(textarea.value, '@')
})

it("doesn't paste markdown URL when additional text is being copied", function () {
textarea.value = 'github'
textarea.setSelectionRange(0, 6)
paste(textarea, {'text/plain': 'https://github.com plus some other content'})
// Synthetic paste events don't manipulate the DOM. The same textarea value
// means that the event handler didn't fire and normal paste happened.
assert.equal(textarea.value, 'github')
})

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