// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import assert from 'assert'; import * as Markdown from 'utils/markdown.jsx'; import * as TextFormatting from 'utils/text_formatting.jsx'; describe('Markdown.Links', function() { this.timeout(10000); it('Not links', function(done) { assert.equal( Markdown.format('example.com').trim(), '

example.com

' ); assert.equal( Markdown.format('readme.md').trim(), '

readme.md

' ); assert.equal( Markdown.format('@example.com').trim(), '

@example.com

' ); assert.equal( Markdown.format('./make-compiled-client.sh').trim(), '

./make-compiled-client.sh

' ); assert.equal( Markdown.format('test.:test').trim(), '

test.:test

' ); assert.equal( Markdown.format('`https://example.com`').trim(), '

' + '' + '' + 'https://example.com' + '' + '' + '

' ); assert.equal( Markdown.format('[link](example.com').trim(), '

[link](example.com

' ); done(); }); it('External links', function(done) { assert.equal( Markdown.format('http://example.com').trim(), '

http://example.com

' ); assert.equal( Markdown.format('https://example.com').trim(), '

https://example.com

' ); assert.equal( Markdown.format('www.example.com').trim(), '

www.example.com

' ); assert.equal( Markdown.format('www.example.com/index').trim(), '

www.example.com/index

' ); assert.equal( Markdown.format('www.example.com/index.html').trim(), '

www.example.com/index.html

' ); assert.equal( Markdown.format('www.example.com/index/sub').trim(), '

www.example.com/index/sub

' ); assert.equal( Markdown.format('www1.example.com').trim(), '

www1.example.com

' ); assert.equal( Markdown.format('example.com/index').trim(), '

example.com/index

' ); done(); }); it('IP addresses', function(done) { assert.equal( Markdown.format('http://127.0.0.1').trim(), '

http://127.0.0.1

' ); assert.equal( Markdown.format('http://192.168.1.1:4040').trim(), '

http://192.168.1.1:4040

' ); assert.equal( Markdown.format('http://[::1]:80').trim(), '

http://[::1]:80

' ); assert.equal( Markdown.format('http://[::1]:8065').trim(), '

http://[::1]:8065

' ); assert.equal( Markdown.format('https://[::1]:80').trim(), '

https://[::1]:80

' ); assert.equal( Markdown.format('http://[2001:0:5ef5:79fb:303a:62d5:3312:ff42]:80').trim(), '

http://[2001:0:5ef5:79fb:303a:62d5:3312:ff42]:80

' ); assert.equal( Markdown.format('http://[2001:0:5ef5:79fb:303a:62d5:3312:ff42]:8065').trim(), '

http://[2001:0:5ef5:79fb:303a:62d5:3312:ff42]:8065

' ); assert.equal( Markdown.format('https://[2001:0:5ef5:79fb:303a:62d5:3312:ff42]:443').trim(), '

https://[2001:0:5ef5:79fb:303a:62d5:3312:ff42]:443

' ); assert.equal( Markdown.format('http://username:password@127.0.0.1').trim(), '

http://username:password@127.0.0.1

' ); assert.equal( Markdown.format('http://username:password@[2001:0:5ef5:79fb:303a:62d5:3312:ff42]:80').trim(), '

http://username:password@[2001:0:5ef5:79fb:303a:62d5:3312:ff42]:80

' ); done(); }); it('Links with anchors', function(done) { assert.equal( Markdown.format('https://en.wikipedia.org/wiki/URLs#Syntax').trim(), '

https://en.wikipedia.org/wiki/URLs#Syntax

' ); assert.equal( Markdown.format('https://groups.google.com/forum/#!msg').trim(), '

https://groups.google.com/forum/#!msg

' ); done(); }); it('Links with parameters', function(done) { assert.equal( Markdown.format('www.example.com/index?params=1').trim(), '

www.example.com/index?params=1

' ); assert.equal( Markdown.format('www.example.com/index?params=1&other=2').trim(), '

www.example.com/index?params=1&other=2

' ); assert.equal( Markdown.format('www.example.com/index?params=1;other=2').trim(), '

www.example.com/index?params=1;other=2

' ); assert.equal( Markdown.format('http://example.com:8065').trim(), '

http://example.com:8065

' ); assert.equal( Markdown.format('http://username:password@example.com').trim(), '

http://username:password@example.com

' ); done(); }); it('Special characters', function(done) { assert.equal( Markdown.format('http://www.example.com/_/page').trim(), '

http://www.example.com/_/page

' ); assert.equal( Markdown.format('www.example.com/_/page').trim(), '

www.example.com/_/page

' ); assert.equal( Markdown.format('https://en.wikipedia.org/wiki/🐬').trim(), '

https://en.wikipedia.org/wiki/🐬

' ); assert.equal( Markdown.format('http://✪df.ws/1234').trim(), '

http://✪df.ws/1234

' ); done(); }); it('Brackets', function(done) { assert.equal( Markdown.format('https://en.wikipedia.org/wiki/Rendering_(computer_graphics)').trim(), '

https://en.wikipedia.org/wiki/Rendering_(computer_graphics)

' ); assert.equal( Markdown.format('http://example.com/more_(than)_one_(parens)').trim(), '

http://example.com/more_(than)_one_(parens)

' ); assert.equal( Markdown.format('http://example.com/(something)?after=parens').trim(), '

http://example.com/(something)?after=parens

' ); assert.equal( Markdown.format('http://foo.com/unicode_(✪)_in_parens').trim(), '

http://foo.com/unicode_(✪)_in_parens

' ); done(); }); it('Email addresses', function(done) { assert.equal( Markdown.format('test@example.com').trim(), '

test@example.com

' ); assert.equal( Markdown.format('test_underscore@example.com').trim(), '

test_underscore@example.com

' ); assert.equal( Markdown.format('mailto:test@example.com').trim(), '

mailto:test@example.com

' ); done(); }); it('Formatted links', function(done) { assert.equal( Markdown.format('*https://example.com*').trim(), '

https://example.com

' ); assert.equal( Markdown.format('_https://example.com_').trim(), '

https://example.com

' ); assert.equal( Markdown.format('**https://example.com**').trim(), '

https://example.com

' ); assert.equal( Markdown.format('__https://example.com__').trim(), '

https://example.com

' ); assert.equal( Markdown.format('***https://example.com***').trim(), '

https://example.com

' ); assert.equal( Markdown.format('___https://example.com___').trim(), '

https://example.com

' ); assert.equal( Markdown.format('').trim(), '

https://example.com

' ); assert.equal( Markdown.format('').trim(), '

https://en.wikipedia.org/wiki/Rendering_(computer_graphics)

' ); done(); }); it('Links with text', function(done) { assert.equal( Markdown.format('[example link](example.com)').trim(), '

example link

' ); assert.equal( Markdown.format('[example.com](example.com)').trim(), '

example.com

' ); assert.equal( Markdown.format('[example.com/other](example.com)').trim(), '

example.com/other

' ); assert.equal( Markdown.format('[example.com/other_link](example.com/example)').trim(), '

example.com/other_link

' ); assert.equal( Markdown.format('[link with spaces](example.com/ spaces in the url)').trim(), '

link with spaces

' ); assert.equal( Markdown.format('[This whole #sentence should be a link](https://example.com)').trim(), '

This whole #sentence should be a link

' ); assert.equal( Markdown.format('[email link](mailto:test@example.com)').trim(), '

email link

' ); assert.equal( Markdown.format('[other link](ts3server://example.com)').trim(), '

other link

' ); done(); }); it('Links with tooltips', function(done) { assert.equal( Markdown.format('[link](example.com "catch phrase!")').trim(), '

link

' ); assert.equal( Markdown.format('[link](example.com "title with "quotes"")').trim(), '

link

' ); assert.equal( Markdown.format('[link with spaces](example.com/ spaces in the url "and a title")').trim(), '

link with spaces

' ); done(); }); it('Links with surrounding text', function(done) { assert.equal( Markdown.format('This is a sentence with a http://example.com in it.').trim(), '

This is a sentence with a http://example.com in it.

' ); assert.equal( Markdown.format('This is a sentence with a http://example.com/_/underscore in it.').trim(), '

This is a sentence with a http://example.com/_/underscore in it.

' ); assert.equal( Markdown.format('This is a sentence with a http://192.168.1.1:4040 in it.').trim(), '

This is a sentence with a http://192.168.1.1:4040 in it.

' ); assert.equal( Markdown.format('This is a sentence with a https://[::1]:80 in it.').trim(), '

This is a sentence with a https://[::1]:80 in it.

' ); done(); }); it('Links with trailing punctuation', function(done) { assert.equal( Markdown.format('This is a link to http://example.com.').trim(), '

This is a link to http://example.com.

' ); assert.equal( Markdown.format('This is a link containing http://example.com/something?with,commas,in,url, but not at the end').trim(), '

This is a link containing http://example.com/something?with,commas,in,url, but not at the end

' ); assert.equal( Markdown.format('This is a question about a link http://example.com?').trim(), '

This is a question about a link http://example.com?

' ); done(); }); it('Links with surrounding brackets', function(done) { assert.equal( Markdown.format('(http://example.com)').trim(), '

(http://example.com)

' ); assert.equal( Markdown.format('(see http://example.com)').trim(), '

(see http://example.com)

' ); assert.equal( Markdown.format('(http://example.com watch this)').trim(), '

(http://example.com watch this)

' ); assert.equal( Markdown.format('(www.example.com)').trim(), '

(www.example.com)

' ); assert.equal( Markdown.format('(see www.example.com)').trim(), '

(see www.example.com)

' ); assert.equal( Markdown.format('(www.example.com watch this)').trim(), '

(www.example.com watch this)

' ); assert.equal( Markdown.format('([link](http://example.com))').trim(), '

(link)

' ); assert.equal( Markdown.format('(see [link](http://example.com))').trim(), '

(see link)

' ); assert.equal( Markdown.format('([link](http://example.com) watch this)').trim(), '

(link watch this)

' ); assert.equal( Markdown.format('(test@example.com)').trim(), '

(test@example.com)

' ); assert.equal( Markdown.format('(email test@example.com)').trim(), '

(email test@example.com)

' ); assert.equal( Markdown.format('(test@example.com email)').trim(), '

(test@example.com email)

' ); assert.equal( Markdown.format('This is a sentence with a [link](http://example.com) in it.').trim(), '

This is a sentence with a link in it.

' ); assert.equal( Markdown.format('This is a sentence with a link (http://example.com) in it.').trim(), '

This is a sentence with a link (http://example.com) in it.

' ); assert.equal( Markdown.format('This is a sentence with a (https://en.wikipedia.org/wiki/Rendering_(computer_graphics)) in it.').trim(), '

This is a sentence with a (https://en.wikipedia.org/wiki/Rendering_(computer_graphics)) in it.

' ); done(); }); it('Searching for links', function(done) { assert.equal( TextFormatting.formatText('https://en.wikipedia.org/wiki/Unix', {searchTerm: 'wikipedia'}).trim(), '

https://en.wikipedia.org/wiki/Unix

' ); assert.equal( TextFormatting.formatText('[Link](https://en.wikipedia.org/wiki/Unix)', {searchTerm: 'unix'}).trim(), '

Link

' ); done(); }); });