Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
[MM-28790] allow plugins to open in a different tab (#6476)
Browse files Browse the repository at this point in the history
* [MM-28790] allow plugins to open in a different tab

* add test for plugins route

* adding tests

* Update utils/markdown/renderer.tsx

Co-authored-by: Jesse Hallam <[email protected]>

* fix linting

Co-authored-by: Jesse Hallam <[email protected]>
Co-authored-by: Mattermod <[email protected]>
  • Loading branch information
3 people authored and calebroseland committed Oct 27, 2020
1 parent c40fdba commit 43e243e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 9 additions & 0 deletions utils/markdown/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ this is long text this is long text this is long text this is long text this is
expect(output).toContain('<a class="theme markdown__link" href="http:https://localhost/example" rel="noreferrer" data-link="/example">internal_link</a>');
});

test('<a> should contain target=_blank for internal links that live under /plugins', () => {
const output = format('[internal_link](http:https://localhost/plugins/example)', {siteURL: 'http:https://localhost'});

expect(output).toContain('<a class="theme markdown__link" href="http:https://localhost/plugins/example" rel="noreferrer" target="_blank">internal_link</a>');
});

test('<a> should not contain target=_blank for pl|channels|messages links', () => {
const pl = format('[thread](/reiciendis-0/pl/b3hrs3brjjn7fk4kge3xmeuffc))', {siteURL: 'http:https://localhost'});
expect(pl).toContain('<a class="theme markdown__link" href="/reiciendis-0/pl/b3hrs3brjjn7fk4kge3xmeuffc" rel="noreferrer" data-link="/reiciendis-0/pl/b3hrs3brjjn7fk4kge3xmeuffc">thread</a>');
Expand All @@ -153,5 +159,8 @@ this is long text this is long text this is long text this is long text this is

const messages = format('[thread](/reiciendis-0/messages/b3hrs3brjjn7fk4kge3xmeuffc))', {siteURL: 'http:https://localhost'});
expect(messages).toContain('<a class="theme markdown__link" href="/reiciendis-0/messages/b3hrs3brjjn7fk4kge3xmeuffc" rel="noreferrer" data-link="/reiciendis-0/messages/b3hrs3brjjn7fk4kge3xmeuffc">thread</a>');

const plugin = format('[plugin](/reiciendis-0/plugins/example))', {siteURL: 'http:https://localhost'});
expect(plugin).toContain('<a class="theme markdown__link" href="/reiciendis-0/plugins/example" rel="noreferrer" data-link="/reiciendis-0/plugins/example">plugin</a>');
});
});
9 changes: 6 additions & 3 deletions utils/markdown/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,17 @@ export default class Renderer extends marked.Renderer {

output += `" href="${outHref}" rel="noreferrer"`;

// Any link that begins with siteURL should be opened inside the app
let internalLink = outHref.startsWith(this.formattingOptions.siteURL || '');
const pluginURL = `${this.formattingOptions.siteURL}/plugins`;

// Any link that begins with siteURL should be opened inside the app, except when rooted
// at /plugins, which is logically "outside the app" despite being hosted by a plugin.
let internalLink = outHref.startsWith(this.formattingOptions.siteURL || '') && (!outHref.startsWith(pluginURL));

// special case for team invite links, channel links, and permalinks that are inside the app
const pattern = new RegExp(
'^(' +
TextFormatting.escapeRegex(this.formattingOptions.siteURL) +
')?\\/(?:signup_user_complete|admin_console|[^\\/]+\\/(?:pl|channels|messages))\\/',
')?\\/(?:signup_user_complete|admin_console|[^\\/]+\\/(?:pl|channels|messages|plugins))\\/',
);
internalLink = internalLink || pattern.test(outHref);

Expand Down

0 comments on commit 43e243e

Please sign in to comment.