-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Open links in new tab & disable links if needed
- Loading branch information
Showing
6 changed files
with
119 additions
and
6 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
x-pack/plugins/security_solution/public/common/components/markdown_editor/markdown_link.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React, { memo } from 'react'; | ||
import { EuiLink, EuiLinkAnchorProps } from '@elastic/eui'; | ||
|
||
type MarkdownLinkProps = { disableLinks?: boolean } & EuiLinkAnchorProps; | ||
|
||
const MarkdownLinkComponent: React.FC<MarkdownLinkProps> = ({ | ||
disableLinks, | ||
href, | ||
target, | ||
children, | ||
...props | ||
}) => ( | ||
<> | ||
{disableLinks ? ( | ||
<span>{children}</span> | ||
) : ( | ||
<EuiLink | ||
{...props} | ||
target="_blank" | ||
data-test-subj="markdown-link" | ||
href={disableLinks ? undefined : href} | ||
rel="nofollow" | ||
> | ||
{children} | ||
</EuiLink> | ||
)} | ||
</> | ||
); | ||
|
||
export const MarkdownLink = memo(MarkdownLinkComponent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
x-pack/plugins/security_solution/public/common/components/markdown_editor/renderer.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
|
||
import { MarkdownRenderer } from './renderer'; | ||
|
||
describe('Markdown', () => { | ||
describe('markdown links', () => { | ||
const markdownWithLink = 'A link to an external site [External Site](https://google.com)'; | ||
|
||
test('it renders the expected link text', () => { | ||
const wrapper = mount(<MarkdownRenderer>{markdownWithLink}</MarkdownRenderer>); | ||
|
||
expect(wrapper.find('[data-test-subj="markdown-link"]').first().text()).toEqual( | ||
'External Site' | ||
); | ||
}); | ||
|
||
test('it renders the expected href', () => { | ||
const wrapper = mount(<MarkdownRenderer>{markdownWithLink}</MarkdownRenderer>); | ||
|
||
expect(wrapper.find('[data-test-subj="markdown-link"]').first().getDOMNode()).toHaveProperty( | ||
'href', | ||
'https://google.com/' | ||
); | ||
}); | ||
|
||
test('it does NOT render the href if links are disabled', () => { | ||
const wrapper = mount( | ||
<MarkdownRenderer disableLinks={true}>{markdownWithLink}</MarkdownRenderer> | ||
); | ||
|
||
expect(wrapper.find('[data-test-subj="markdown-link"]').exists()).toBeFalsy(); | ||
}); | ||
|
||
test('it opens links in a new tab via target="_blank"', () => { | ||
const wrapper = mount(<MarkdownRenderer>{markdownWithLink}</MarkdownRenderer>); | ||
|
||
expect(wrapper.find('[data-test-subj="markdown-link"]').first().getDOMNode()).toHaveProperty( | ||
'target', | ||
'_blank' | ||
); | ||
}); | ||
|
||
test('it sets the link `rel` attribute to `noopener` to prevent the new page from accessing `window.opener`, `nofollow` to note the link is not endorsed by us, and noreferrer to prevent the browser from sending the current address', () => { | ||
const wrapper = mount(<MarkdownRenderer>{markdownWithLink}</MarkdownRenderer>); | ||
|
||
expect(wrapper.find('[data-test-subj="markdown-link"]').first().getDOMNode()).toHaveProperty( | ||
'rel', | ||
'nofollow noopener noreferrer' | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters