2021-11-30 21:09:05 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2021-11-30 21:09:05 +03:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 16:57:16 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-11-30 21:09:05 +03:00
|
|
|
*/
|
|
|
|
|
2021-12-03 17:00:56 +03:00
|
|
|
import Markdown from "../src/Markdown";
|
2021-11-30 21:09:05 +03:00
|
|
|
|
|
|
|
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");
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("tests that links with markdown empasis in them are getting properly HTML formatted", () => {
|
2021-11-30 21:09:05 +03:00
|
|
|
/* eslint-disable max-len */
|
|
|
|
const expectedResult = [
|
|
|
|
"<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>",
|
|
|
|
"<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>",
|
|
|
|
"<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>",
|
|
|
|
"<p>Test3:<br />https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org<br />https://riot.im/app/#/room/#_foonetic_xkcd:matrix.org</p>",
|
|
|
|
"",
|
|
|
|
].join("\n");
|
|
|
|
/* eslint-enable max-len */
|
|
|
|
const md = new Markdown(testString);
|
|
|
|
expect(md.toHTML()).toEqual(expectedResult);
|
|
|
|
});
|
2022-12-12 14:24:14 +03:00
|
|
|
it("tests that links with autolinks are not touched at all and are still properly formatted", () => {
|
2021-11-30 21:09:05 +03:00
|
|
|
const test = [
|
|
|
|
"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-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 = [
|
2022-12-12 14:24:14 +03:00
|
|
|
'<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>',
|
|
|
|
'<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>',
|
|
|
|
'<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>',
|
|
|
|
'<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>',
|
2021-11-30 21:09:05 +03:00
|
|
|
"",
|
|
|
|
].join("\n");
|
|
|
|
/* eslint-enable max-len */
|
|
|
|
const md = new Markdown(test);
|
|
|
|
expect(md.toHTML()).toEqual(expectedResult);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("expects that links in codeblock are not modified", () => {
|
2021-11-30 21:09:05 +03:00
|
|
|
const expectedResult = [
|
|
|
|
'<pre><code class="language-Test1:">#_foonetic_xkcd:matrix.org',
|
2022-12-12 14:24:14 +03:00
|
|
|
"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```",
|
|
|
|
"</code></pre>",
|
|
|
|
"",
|
|
|
|
].join("\n");
|
2021-11-30 21:09:05 +03:00
|
|
|
const md = new Markdown("```" + testString + "```");
|
|
|
|
expect(md.toHTML()).toEqual(expectedResult);
|
|
|
|
});
|
|
|
|
|
2021-12-02 13:56:23 +03:00
|
|
|
it('expects that links with emphasis are "escaped" correctly', () => {
|
2021-11-30 21:09:05 +03:00
|
|
|
/* eslint-disable max-len */
|
|
|
|
const testString = [
|
2022-12-12 14:24:14 +03:00
|
|
|
"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",
|
2022-11-14 12:52:42 +03:00
|
|
|
"https://example.com/_test_test2_-test3",
|
|
|
|
"https://example.com/_test_test2_test3_",
|
|
|
|
"https://example.com/_test__test2_test3_",
|
|
|
|
"https://example.com/_test__test2__test3_",
|
|
|
|
"https://example.com/_test__test2_test3__",
|
|
|
|
"https://example.com/_test__test2",
|
2022-12-12 14:24:14 +03:00
|
|
|
].join("\n");
|
2021-11-30 21:09:05 +03:00
|
|
|
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",
|
2022-11-14 12:52:42 +03:00
|
|
|
"https://example.com/_test_test2_-test3",
|
|
|
|
"https://example.com/_test_test2_test3_",
|
|
|
|
"https://example.com/_test__test2_test3_",
|
|
|
|
"https://example.com/_test__test2__test3_",
|
|
|
|
"https://example.com/_test__test2_test3__",
|
|
|
|
"https://example.com/_test__test2",
|
2022-12-12 14:24:14 +03:00
|
|
|
].join("<br />");
|
2021-11-30 21:09:05 +03:00
|
|
|
/* eslint-enable max-len */
|
|
|
|
const md = new Markdown(testString);
|
|
|
|
expect(md.toHTML()).toEqual(expectedResult);
|
|
|
|
});
|
2021-12-02 13:56:23 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("expects that the link part will not be accidentally added to <strong>", () => {
|
2021-12-02 13:56:23 +03:00
|
|
|
/* eslint-disable max-len */
|
|
|
|
const testString = `https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py`;
|
2022-12-12 14:24:14 +03:00
|
|
|
const expectedResult = "https://github.com/matrix-org/synapse/blob/develop/synapse/module_api/__init__.py";
|
2021-12-02 13:56:23 +03:00
|
|
|
/* eslint-enable max-len */
|
|
|
|
const md = new Markdown(testString);
|
|
|
|
expect(md.toHTML()).toEqual(expectedResult);
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("expects that the link part will not be accidentally added to <strong> for multiline links", () => {
|
2021-12-02 13:56:23 +03:00
|
|
|
/* eslint-disable max-len */
|
|
|
|
const testString = [
|
2022-12-12 14:24:14 +03:00
|
|
|
"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");
|
2021-12-02 13:56:23 +03:00
|
|
|
const expectedResult = [
|
2022-12-12 14:24:14 +03:00
|
|
|
"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("<br />");
|
2021-12-02 13:56:23 +03:00
|
|
|
/* eslint-enable max-len */
|
|
|
|
const md = new Markdown(testString);
|
|
|
|
expect(md.toHTML()).toEqual(expectedResult);
|
|
|
|
});
|
2022-03-08 16:12:36 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("resumes applying formatting to the rest of a message after a link", () => {
|
|
|
|
const testString = "http://google.com/_thing_ *does* __not__ exist";
|
|
|
|
const expectedResult = "http://google.com/_thing_ <em>does</em> <strong>not</strong> exist";
|
2022-03-08 16:12:36 +03:00
|
|
|
const md = new Markdown(testString);
|
|
|
|
expect(md.toHTML()).toEqual(expectedResult);
|
|
|
|
});
|
2021-11-30 21:09:05 +03:00
|
|
|
});
|
|
|
|
});
|