Skip to content

Commit

Permalink
Merge pull request #827 from iterative/gf-fix_#729
Browse files Browse the repository at this point in the history
Fix #729: Open external domain links in a new tab
  • Loading branch information
jorgeorpinel authored Dec 3, 2019
2 parents 639b152 + dd4cd8a commit 62ebbde
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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' }
: 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.

0 comments on commit 62ebbde

Please sign in to comment.