Skip to content

Commit

Permalink
Validate URLs before converting text to a link (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipc103 authored Aug 17, 2022
1 parent 2cf7f94 commit 6a2f6df
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/paste-markdown-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function onPaste(event: ClipboardEvent) {
event.stopPropagation()
event.preventDefault()

insertText(field, linkify(selectedText, text))
insertText(field, linkify(selectedText, text.trim()))
}

function hasPlainText(transfer: DataTransfer): boolean {
Expand All @@ -55,7 +55,15 @@ 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 URL_REGEX.test(url)
try {
//eslint-disable-next-line no-restricted-syntax
const parsedURL = new URL(url)
return removeTrailingSlash(parsedURL.href).trim() === removeTrailingSlash(url).trim()
} catch {
return false
}
}
function removeTrailingSlash(url: string) {
return url.endsWith('/') ? url.slice(0, url.length - 1) : url
}
51 changes: 51 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,57 @@ describe('paste-markdown', function () {
assert.equal(textarea.value, 'The examples can be found [here](https://www.github.com/).')
})

it('creates a markdown link for longer urls', 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/path_to/something-different/too'})
assert.equal(
textarea.value,
'The examples can be found [here](https://www.github.com/path_to/something-different/too).'
)
})

it('creates a markdown link with query string', 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/path/to/something?query=true'})
assert.equal(
textarea.value,
'The examples can be found [here](https://www.github.com/path/to/something?query=true).'
)
})

it('creates a markdown link with hash params', 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/path/to/something#section'})
assert.equal(
textarea.value,
'The examples can be found [here](https://www.github.com/path/to/something#section).'
)
})

it('creates a link for http urls', function () {
// eslint-disable-next-line i18n-text/no-en
textarea.value = 'Look over here please'
textarea.setSelectionRange(10, 14)
const url = 'http://someotherdomain.org/another/thing'
paste(textarea, {'text/plain': url})
assert.equal(textarea.value, `Look over [here](${url}) please`)
})

it('creates a link when copied content includes spaces and a newline', () => {
// eslint-disable-next-line i18n-text/no-en
textarea.value = 'Look over here please'
textarea.setSelectionRange(10, 14)
const url = 'http://someotherdomain.org/another/thing \n'
paste(textarea, {'text/plain': url})
assert.equal(textarea.value, `Look over [here](${url.trim()}) please`)
})

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 Down

0 comments on commit 6a2f6df

Please sign in to comment.