Replace text with a link-to in a helper


#1

Hi,

I’m new to ember and not sure of the best way to go about this.

Within posted text I want to replace @username with a link to the username’s profile. I want the link to work with a route which uses the username as the parameter. Is this possible?

Here’s where I’m at so far. Obviously at the moment you will leave the app if you click the generated link.

var regexUsername = /\s@([\-_A-Za-z0-9]{3,15})\s/

var replaceUsername = function(match, username) {
  return ' <a href="/profile/' + username + '">@' + username + '</a> ';
}

// Format post text
Ember.Handlebars.helper('formatPost', function(text) {
  var escaped = Handlebars.Utils.escapeExpression(' ' + text + ' ');

  text = text.replace(regexUsername, replaceUsername);

  return new Handlebars.SafeString(text.substring(1, text.length - 1));
});

Greatly appreciate any ideas!

Thanks!