Skip to content

Commit

Permalink
Open external links in new window
Browse files Browse the repository at this point in the history
  • Loading branch information
iAdramelk committed Nov 29, 2019
1 parent 834d261 commit 967a225
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 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' }
: 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,18 @@ export const GithubLink = styled(LightButton)`
background-image: url(/static/img/github_icon.svg);
}
`

const ExternalLink = styled.a`
position: relative;
padding-right: 21px;
&:after {
position: absolute;
top: 2px;
right: 0;
width: 16px;
height: 16px;
/* Icon source https://www.iconfinder.com/icons/2561428/external_link_icon */
content: url(/static/img/external-link.svg);
}
`
1 change: 1 addition & 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.

0 comments on commit 967a225

Please sign in to comment.