// Copyright (c) 2016-present 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() { 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' +
'
' +
'' +
'
[link](example.com
' ); done(); }); it('External links', function(done) { assert.equal( Markdown.format('http://example.com').trim(), '' ); assert.equal( Markdown.format('https://example.com').trim(), '' ); assert.equal( Markdown.format('www.example.com').trim(), '' ); assert.equal( Markdown.format('www.example.com/index').trim(), '' ); assert.equal( Markdown.format('www.example.com/index.html').trim(), '' ); assert.equal( Markdown.format('www.example.com/index/sub').trim(), '' ); assert.equal( Markdown.format('www1.example.com').trim(), '' ); assert.equal( Markdown.format('example.com/index').trim(), '' ); done(); }); it('IP addresses', function(done) { assert.equal( Markdown.format('http://127.0.0.1').trim(), '' ); assert.equal( Markdown.format('http://192.168.1.1:4040').trim(), '' ); assert.equal( Markdown.format('http://[::1]:80').trim(), '' ); assert.equal( Markdown.format('http://[::1]:8065').trim(), '' ); assert.equal( Markdown.format('https://[::1]:80').trim(), '' ); 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(), '' ); 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(), '' ); assert.equal( Markdown.format('www.example.com/_/page').trim(), '' ); assert.equal( Markdown.format('https://en.wikipedia.org/wiki/🐬').trim(), 'https://en.wikipedia.org/wiki/🐬
' ); assert.equal( Markdown.format('http://✪df.ws/1234').trim(), '' ); 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(), '' ); assert.equal( Markdown.format('test_underscore@example.com').trim(), '' ); assert.equal( Markdown.format('mailto:test@example.com').trim(), '' ); done(); }); it('Formatted links', function(done) { assert.equal( Markdown.format('*https://example.com*').trim(), '' ); assert.equal( Markdown.format('_https://example.com_').trim(), '' ); assert.equal( Markdown.format('**https://example.com**').trim(), '' ); assert.equal( Markdown.format('__https://example.com__').trim(), '' ); assert.equal( Markdown.format('***https://example.com***').trim(), '' ); assert.equal( Markdown.format('___https://example.com___').trim(), '' ); assert.equal( Markdown.format('https://en.wikipedia.org/wiki/Rendering_(computer_graphics)
' ); done(); }); it('Links with text', function(done) { assert.equal( Markdown.format('[example link](example.com)').trim(), '' ); assert.equal( Markdown.format('[example.com](example.com)').trim(), '' ); assert.equal( Markdown.format('[example.com/other](example.com)').trim(), '' ); assert.equal( Markdown.format('[example.com/other_link](example.com/example)').trim(), '' ); assert.equal( Markdown.format('[link with spaces](example.com/ spaces in the url)').trim(), '' ); 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(), '' ); assert.equal( Markdown.format('[other link](ts3server://example.com)').trim(), '' ); done(); }); it('Links with tooltips', function(done) { assert.equal( Markdown.format('[link](example.com "catch phrase!")').trim(), '' ); assert.equal( Markdown.format('[link](example.com "title with "quotes"")').trim(), '' ); assert.equal( Markdown.format('[link with spaces](example.com/ spaces in the url "and a title")').trim(), '' ); 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(), '' ); 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(), '' ); 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(), '' ); 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:/