diff --git a/src/paste-markdown-link.ts b/src/paste-markdown-link.ts index 1c9674d..34111ee 100644 --- a/src/paste-markdown-link.ts +++ b/src/paste-markdown-link.ts @@ -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) } diff --git a/test/test.js b/test/test.js index b88f5e0..f5c74f8 100644 --- a/test/test.js +++ b/test/test.js @@ -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' @@ -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