Skip to content

Instantly share code, notes, and snippets.

@amk221
Created April 5, 2016 08:11
Show Gist options
  • Save amk221/d3fb5eb6e3ef091baea02cd8824123fd to your computer and use it in GitHub Desktop.
Save amk221/d3fb5eb6e3ef091baea02cd8824123fd to your computer and use it in GitHub Desktop.
Ember wrap URLs helper
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