/* Copyright 2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import * as linkifyjs from 'linkifyjs'; import Markdown from "../src/Markdown"; import matrixLinkify from '../src/linkify-matrix'; beforeAll(() => { // We need to call linkifier plugins before running those tests matrixLinkify(linkifyjs); }); describe("Markdown parser test", () => { describe("fixing HTML links", () => { const testString = [ "Test1:", "#_foonetic_xkcd:matrix.org", "http://google.com/_thing_", "https://matrix.org/_matrix/client/foo/123_", "#_foonetic_xkcd:matrix.org", "", "Test1A:", "#_foonetic_xkcd:matrix.org", "http://google.com/_thing_", "https://matrix.org/_matrix/client/foo/123_", "#_foonetic_xkcd:matrix.org", "", "Test2:", "http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg", "http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg", "", "Test3:", "https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org", "https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org", ].join("\n"); it('tests that links with markdown empasis in them are getting properly HTML formatted', () => { /* eslint-disable max-len */ const expectedResult = [ "

Test1:
#_foonetic_xkcd:matrix.org
http://google.com/_thing_
https://matrix.org/_matrix/client/foo/123_
#_foonetic_xkcd:matrix.org

", "

Test1A:
#_foonetic_xkcd:matrix.org
http://google.com/_thing_
https://matrix.org/_matrix/client/foo/123_
#_foonetic_xkcd:matrix.org

", "

Test2:
http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg
http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg

", "

Test3:
https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org
https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org

", "", ].join("\n"); /* eslint-enable max-len */ const md = new Markdown(testString); expect(md.toHTML()).toEqual(expectedResult); }); it('tests that links with autolinks are not touched at all and are still properly formatted', () => { const test = [ "Test1:", "<#_foonetic_xkcd:matrix.org>", "", "", "<#_foonetic_xkcd:matrix.org>", "", "Test1A:", "<#_foonetic_xkcd:matrix.org>", "", "", "<#_foonetic_xkcd:matrix.org>", "", "Test2:", "", "", "", "Test3:", "", "", ].join("\n"); /* eslint-disable max-len */ /** * NOTE: I'm not entirely sure if those "<"" and ">" should be visible in here for #_foonetic_xkcd:matrix.org * but it seems to be actually working properly */ const expectedResult = [ "

Test1:
<#_foonetic_xkcd:matrix.org>
http://google.com/_thing_
https://matrix.org/_matrix/client/foo/123_
<#_foonetic_xkcd:matrix.org>

", "

Test1A:
<#_foonetic_xkcd:matrix.org>
http://google.com/_thing_
https://matrix.org/_matrix/client/foo/123_
<#_foonetic_xkcd:matrix.org>

", "

Test2:
http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg
http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg

", "

Test3:
https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org
https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org

", "", ].join("\n"); /* eslint-enable max-len */ const md = new Markdown(test); expect(md.toHTML()).toEqual(expectedResult); }); it('expects that links in codeblock are not modified', () => { const expectedResult = [ '
#_foonetic_xkcd:matrix.org',
                'http://google.com/_thing_',
                'https://matrix.org/_matrix/client/foo/123_',
                '#_foonetic_xkcd:matrix.org',
                '',
                'Test1A:',
                '#_foonetic_xkcd:matrix.org',
                'http://google.com/_thing_',
                'https://matrix.org/_matrix/client/foo/123_',
                '#_foonetic_xkcd:matrix.org',
                '',
                'Test2:',
                'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg',
                'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg',
                '',
                'Test3:',
                'https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org',
                'https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org```',
                '
', '', ].join('\n'); const md = new Markdown("```" + testString + "```"); expect(md.toHTML()).toEqual(expectedResult); }); it('expects that links with emphasis are "escaped" correctly', () => { /* eslint-disable max-len */ const testString = [ 'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg' + " " + 'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg', 'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg' + " " + 'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg', ].join('\n'); const expectedResult = [ "http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg", "http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg", ].join('
'); /* eslint-enable max-len */ const md = new Markdown(testString); expect(md.toHTML()).toEqual(expectedResult); }); it('expects that the link part will not be accidentally added to ', () => { /* eslint-disable max-len */ const testString = `https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py`; const expectedResult = 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py'; /* eslint-enable max-len */ const md = new Markdown(testString); expect(md.toHTML()).toEqual(expectedResult); }); it('expects that the link part will not be accidentally added to for multiline links', () => { /* eslint-disable max-len */ const testString = [ 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py' + " " + 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py', 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py' + " " + 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py', ].join('\n'); const expectedResult = [ 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py' + " " + 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py', 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py' + " " + 'https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py', ].join('
'); /* eslint-enable max-len */ const md = new Markdown(testString); expect(md.toHTML()).toEqual(expectedResult); }); }); });