From 1b92704afc190cbb9b5c8e575e5e98a4f6a590c9 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Tue, 2 Aug 2016 12:16:14 +0300 Subject: [PATCH] Set target="_blank" for external links --- frontend/components/Document/Document.js | 45 ++++++++++++++++++------ 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/frontend/components/Document/Document.js b/frontend/components/Document/Document.js index 93ea3969a..1dd87f0ee 100644 --- a/frontend/components/Document/Document.js +++ b/frontend/components/Document/Document.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { PropTypes } from 'react'; import { observer } from 'mobx-react'; import moment from 'moment'; @@ -7,15 +7,40 @@ import PublishingInfo from 'components/PublishingInfo'; import styles from './Document.scss'; -const DocumentHtml = observer((props) => { - return ( -
- ); -}); +@observer +class DocumentHtml extends React.Component { + static propTypes = { + html: PropTypes.string.isRequired, + } + + componentDidMount = () => { + this.setExternalLinks(); + } + + componentDidUpdate = () => { + this.setExternalLinks(); + } + + setExternalLinks = () => { + const links = this.refs.content.querySelectorAll('a'); + links.forEach(link => { + if (link.hostname !== window.location.hostname) { + link.target = '_blank'; // eslint-disable-line no-param-reassign + } + }); + } + + render() { + return ( +
+ ); + } +} @observer class Document extends React.Component {