Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #729: Open external domain links in a new tab #827

Merged
merged 2 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Documentation/Markdown/Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,27 @@ CodeBlock.propTypes = {
value: PropTypes.node.isRequired
}

const Link = ({ children, ...props }) => {
const externalLink = props.href.match(/^(\/\/|http(s)?:\/\/)/)
const showIcon =
externalLink && children && typeof children[0].props.children === 'string'

const modifiedProps = externalLink
? { ...props, target: '_blank', rel: 'noopener nofollow' }
shcheklein marked this conversation as resolved.
Show resolved Hide resolved
: props

if (showIcon) {
return <ExternalLink {...modifiedProps}>{children}</ExternalLink>
}

return <a {...modifiedProps}>{children}</a>
}

Link.propTypes = {
children: PropTypes.node.isRequired,
href: PropTypes.string.isRequired
}

export default class Markdown extends React.PureComponent {
constructor() {
super()
Expand Down Expand Up @@ -167,6 +188,7 @@ export default class Markdown extends React.PureComponent {
escapeHtml={false}
source={markdown}
renderers={{
link: Link,
code: CodeBlock,
heading: HeadingRenderer,
virtualHtml: HtmlRenderer
Expand Down Expand Up @@ -366,3 +388,16 @@ export const GithubLink = styled(LightButton)`
background-image: url(/static/img/github_icon.svg);
}
`

const ExternalLink = styled.a`
&:after {
position: relative;
top: 1px;
right: 0;
width: 12px;
height: 12px;
margin-left: 1px;
/* Icon source https://en.wikipedia.org/w/skins/Vector/images/external-link-ltr-icon.svg */
content: url(/static/img/external-link.svg);
}
`
6 changes: 6 additions & 0 deletions static/img/external-link.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.