Created
October 31, 2015 15:42
-
-
Save remiq/608c4b29773a4f7b7cd3 to your computer and use it in GitHub Desktop.
React's Message class for parsing links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Message extends React.Component { | |
parseLinks(body) { | |
body = body.split(/(http[^ ]+)/) | |
body = body.map(function(url) { | |
if (url.match(/(http[^ ]+)/)) { | |
var href = "/r?url=" + encodeURIComponent(url) | |
var key = Math.random() | |
return <a key={key} href={href} target="_blank">{url}</a> | |
} else { | |
return url | |
} | |
}) | |
return body | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment