react-format-text
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -9,3 +9,3 @@ 'use strict'; | ||
var format = function format(str) { | ||
var format = function format(str, options) { | ||
var matches = linkify.match(str) || []; | ||
@@ -26,3 +26,4 @@ var index = 0; | ||
var part = str.slice(index, match.index); | ||
var target = /^http(s)?:/.test(match.url) ? '_blank' : null; | ||
var target = 'target' in options ? options.target : /^http(s)?:/.test(match.url) ? '_blank' : null; | ||
var rel = 'rel' in options ? options.rel : target === '_blank' ? 'noopener noreferrer' : null; | ||
@@ -34,3 +35,3 @@ index = match.lastIndex; | ||
'a', | ||
{ key: i, target: target, href: match.url }, | ||
{ key: i, target: target, rel: rel, href: match.url }, | ||
match.text | ||
@@ -49,3 +50,5 @@ )); | ||
propTypes: { | ||
children: React.PropTypes.string | ||
children: React.PropTypes.string, | ||
target: React.PropTypes.string, | ||
rel: React.PropTypes.string | ||
}, | ||
@@ -59,5 +62,5 @@ | ||
null, | ||
text && format(text) | ||
text && format(text, this.props) | ||
); | ||
} | ||
}); |
15
index.js
@@ -7,3 +7,3 @@ var React = require('react'); | ||
var format = function(str) { | ||
var format = function(str, options) { | ||
var matches = linkify.match(str) || []; | ||
@@ -24,3 +24,6 @@ var index = 0; | ||
var part = str.slice(index, match.index); | ||
var target = (/^http(s)?:/).test(match.url) ? '_blank' : null; | ||
var target = ('target' in options) ? options.target : | ||
((/^http(s)?:/).test(match.url) ? '_blank' : null); | ||
var rel = ('rel' in options) ? options.rel : | ||
(target === '_blank' ? 'noopener noreferrer' : null); | ||
@@ -30,3 +33,3 @@ index = match.lastIndex; | ||
newlines(part, i); | ||
elements.push(<a key={i} target={target} href={match.url}>{match.text}</a>); | ||
elements.push(<a key={i} target={target} rel={rel} href={match.url}>{match.text}</a>); | ||
}); | ||
@@ -41,3 +44,5 @@ | ||
propTypes: { | ||
children: React.PropTypes.string | ||
children: React.PropTypes.string, | ||
target: React.PropTypes.string, | ||
rel: React.PropTypes.string | ||
}, | ||
@@ -49,5 +54,5 @@ | ||
return ( | ||
<span>{text && format(text)}</span> | ||
<span>{text && format(text, this.props)}</span> | ||
); | ||
} | ||
}); |
{ | ||
"name": "react-format-text", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Simple link and newline text formatting for react", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -9,3 +9,3 @@ # react-format-text | ||
Newlines are converted to `<br>` elements and words looking like URLs are replaced with appropriate `<a>` elements. | ||
Newlines are converted to `<br>` elements and words looking like URLs are replaced with appropriate `<a>` elements. By default all absolute links have `target` set to `_blank` and `rel` to `noopener noreferrer`. It's possible to override this by passing the `target` and `rel` property to the component. | ||
@@ -12,0 +12,0 @@ ```javascript |
7255
123