|
| 1 | +/* |
| 2 | +Copyright 2021 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +import * as linkifyjs from 'linkifyjs'; |
| 17 | +import Markdown from "../src/Markdown"; |
| 18 | +import matrixLinkify from '../src/linkify-matrix'; |
| 19 | + |
| 20 | +beforeAll(() => { |
| 21 | + // We need to call linkifier plugins before running those tests |
| 22 | + matrixLinkify(linkifyjs); |
| 23 | +}); |
| 24 | + |
| 25 | +describe("Markdown parser test", () => { |
| 26 | + describe("fixing HTML links", () => { |
| 27 | + const testString = [ |
| 28 | + "Test1:", |
| 29 | + "#_foonetic_xkcd:matrix.org", |
| 30 | + "http://google.com/_thing_", |
| 31 | + "https://matrix.org/_matrix/client/foo/123_", |
| 32 | + "#_foonetic_xkcd:matrix.org", |
| 33 | + "", |
| 34 | + "Test1A:", |
| 35 | + "#_foonetic_xkcd:matrix.org", |
| 36 | + "http://google.com/_thing_", |
| 37 | + "https://matrix.org/_matrix/client/foo/123_", |
| 38 | + "#_foonetic_xkcd:matrix.org", |
| 39 | + "", |
| 40 | + "Test2:", |
| 41 | + "http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg", |
| 42 | + "http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg", |
| 43 | + "", |
| 44 | + "Test3:", |
| 45 | + "https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org", |
| 46 | + "https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org", |
| 47 | + ].join("\n"); |
| 48 | + |
| 49 | + it('tests that links are getting properly HTML formatted', () => { |
| 50 | + /* eslint-disable max-len */ |
| 51 | + const expectedResult = [ |
| 52 | + "<p>Test1:<br />#_foonetic_xkcd:matrix.org<br />http://google.com/_thing_<br />https://matrix.org/_matrix/client/foo/123_<br />#_foonetic_xkcd:matrix.org</p>", |
| 53 | + "<p>Test1A:<br />#_foonetic_xkcd:matrix.org<br />http://google.com/_thing_<br />https://matrix.org/_matrix/client/foo/123_<br />#_foonetic_xkcd:matrix.org</p>", |
| 54 | + "<p>Test2:<br />http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg<br />http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg</p>", |
| 55 | + "<p>Test3:<br />https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org<br />https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org</p>", |
| 56 | + "", |
| 57 | + ].join("\n"); |
| 58 | + /* eslint-enable max-len */ |
| 59 | + const md = new Markdown(testString); |
| 60 | + expect(md.toHTML()).toEqual(expectedResult); |
| 61 | + }); |
| 62 | + it('tests that links with autolinks are not touched at all and are still properly formatted', () => { |
| 63 | + const test = [ |
| 64 | + "Test1:", |
| 65 | + "<#_foonetic_xkcd:matrix.org>", |
| 66 | + "<http://google.com/_thing_>", |
| 67 | + "<https://matrix.org/_matrix/client/foo/123_>", |
| 68 | + "<#_foonetic_xkcd:matrix.org>", |
| 69 | + "", |
| 70 | + "Test1A:", |
| 71 | + "<#_foonetic_xkcd:matrix.org>", |
| 72 | + "<http://google.com/_thing_>", |
| 73 | + "<https://matrix.org/_matrix/client/foo/123_>", |
| 74 | + "<#_foonetic_xkcd:matrix.org>", |
| 75 | + "", |
| 76 | + "Test2:", |
| 77 | + "<http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg>", |
| 78 | + "<http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg>", |
| 79 | + "", |
| 80 | + "Test3:", |
| 81 | + "<https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org>", |
| 82 | + "<https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org>", |
| 83 | + ].join("\n"); |
| 84 | + /* eslint-disable max-len */ |
| 85 | + /** |
| 86 | + * NOTE: I'm not entirely sure if those "<"" and ">" should be visible in here for #_foonetic_xkcd:matrix.org |
| 87 | + * but it seems to be actually working properly |
| 88 | + */ |
| 89 | + const expectedResult = [ |
| 90 | + "<p>Test1:<br /><#_foonetic_xkcd:matrix.org><br /><a href=\"http://google.com/_thing_\">http://google.com/_thing_</a><br /><a href=\"https://matrix.org/_matrix/client/foo/123_\">https://matrix.org/_matrix/client/foo/123_</a><br /><#_foonetic_xkcd:matrix.org></p>", |
| 91 | + "<p>Test1A:<br /><#_foonetic_xkcd:matrix.org><br /><a href=\"http://google.com/_thing_\">http://google.com/_thing_</a><br /><a href=\"https://matrix.org/_matrix/client/foo/123_\">https://matrix.org/_matrix/client/foo/123_</a><br /><#_foonetic_xkcd:matrix.org></p>", |
| 92 | + "<p>Test2:<br /><a href=\"http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg\">http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg</a><br /><a href=\"http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg\">http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg</a></p>", |
| 93 | + "<p>Test3:<br /><a href=\"https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org\">https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org</a><br /><a href=\"https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org\">https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org</a></p>", |
| 94 | + "", |
| 95 | + ].join("\n"); |
| 96 | + /* eslint-enable max-len */ |
| 97 | + const md = new Markdown(test); |
| 98 | + expect(md.toHTML()).toEqual(expectedResult); |
| 99 | + }); |
| 100 | + |
| 101 | + it('expects that links in codeblock are not modified', () => { |
| 102 | + const expectedResult = [ |
| 103 | + '<pre><code class="language-Test1:">#_foonetic_xkcd:matrix.org', |
| 104 | + 'http://google.com/_thing_', |
| 105 | + 'https://matrix.org/_matrix/client/foo/123_', |
| 106 | + '#_foonetic_xkcd:matrix.org', |
| 107 | + '', |
| 108 | + 'Test1A:', |
| 109 | + '#_foonetic_xkcd:matrix.org', |
| 110 | + 'http://google.com/_thing_', |
| 111 | + 'https://matrix.org/_matrix/client/foo/123_', |
| 112 | + '#_foonetic_xkcd:matrix.org', |
| 113 | + '', |
| 114 | + 'Test2:', |
| 115 | + 'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg', |
| 116 | + 'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg', |
| 117 | + '', |
| 118 | + 'Test3:', |
| 119 | + 'https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org', |
| 120 | + 'https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org```', |
| 121 | + '</code></pre>', |
| 122 | + '', |
| 123 | + ].join('\n'); |
| 124 | + const md = new Markdown("```" + testString + "```"); |
| 125 | + expect(md.toHTML()).toEqual(expectedResult); |
| 126 | + }); |
| 127 | + |
| 128 | + it('expects that links in one line will be "escaped" properly', () => { |
| 129 | + /* eslint-disable max-len */ |
| 130 | + const testString = [ |
| 131 | + 'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg' + " " + 'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg', |
| 132 | + 'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg' + " " + 'http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg', |
| 133 | + ].join('\n'); |
| 134 | + const expectedResult = [ |
| 135 | + "http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg", |
| 136 | + "http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg http://domain.xyz/foo/bar-_stuff-like-this_-in-it.jpg", |
| 137 | + ].join('<br />'); |
| 138 | + /* eslint-enable max-len */ |
| 139 | + const md = new Markdown(testString); |
| 140 | + expect(md.toHTML()).toEqual(expectedResult); |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments