Created
September 22, 2017 12:48
-
-
Save luizcarraro/b2b85043caf36e9845c1f722cc561d12 to your computer and use it in GitHub Desktop.
Ember Helper para Capturar links e os torna clicáveis
This file contains hidden or 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
| import Ember from 'ember'; | |
| export function captureLinks(params/*, hash*/) { | |
| var expression = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9]\.[^\s]{2,})/; | |
| var regex = new RegExp(expression); | |
| var phrase = params[0] || ''; | |
| var words = phrase.split(' '); | |
| console.log('words', words); | |
| for (var i = 0; i < words.length; i++) { | |
| if (words[i].match(regex)) { | |
| console.log("Successful match: ", words[i]); | |
| var link = words[i]; | |
| if(!words[i].startsWith('http')) { | |
| link = 'http://' + words[i]; | |
| } | |
| phrase = phrase.replace(words[i], '<a href="' + link + '" target="_blank">' + words[i] + '</a>'); | |
| } | |
| } | |
| return phrase; | |
| } | |
| export default Ember.Helper.helper(captureLinks); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment