Created
April 5, 2016 08:11
-
-
Save amk221/d3fb5eb6e3ef091baea02cd8824123fd to your computer and use it in GitHub Desktop.
Ember wrap URLs helper
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
import Ember from 'ember'; | |
import { helper } from 'ember-helper'; | |
import { htmlSafe } from 'ember-string'; | |
const { escapeExpression } = Ember.Handlebars.Utils; | |
export const urlRegex = /(https?|file|ftp):\/\/([a-zA-Z0-9~!@#$%\^&*()_\-=+\/?.:;',]*)?/g; | |
/** | |
* This helper wraps URLs in a string with a span, to give us control over | |
* their appearance. | |
* | |
* @example | |
* {{wrap-urls 'http://example.com'}} | |
* Results in: | |
* <span class="url">http://example.com</span> | |
*/ | |
export default helper(function(args = []) { | |
let [ string ] = args; | |
string = escapeExpression(string); | |
string = string.replace(urlRegex, (url) => { | |
return `<span class="url">${url}</span>`; | |
}); | |
return htmlSafe(string); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment