I need to be able to linkify a hashtag by going through the content being sent from the server and replacing each hashtag to a url that points to our site. That was easy to do, but I need to have it insert a link-to component for each hashtag so that a full page refresh does not occur.
To give an example, lets say the content is “this is a #cool #ember post” I need to be able to turn that into
"this is a <a href="/hashtags/cool">#cool</a> <a href="/hashtags/ember">#ember</a> post"
Here is my helper.
import Ember from 'ember';
export function linkifyText(params, hash) {
var regex = /(^|\W)(#[a-z\d][\w-]*)/ig;
var text = params[0];
text = text.replace(regex, function (hashtag) {
hashtag = hashtag.trim();
var word = hashtag.split('#')[1];
return ' <a href="/hashtag/' + word + '">' + hashtag + '</a> ';
});
return Ember.String.htmlSafe(text);
}
export default Ember.Helper.helper(linkifyText);
This works but it causes a full page refresh because we are not using link-to components. Is there a way I can insert a link-to component in the helper for each hashtag?
Another option would be to turn this into a component and use actions on each link that then go to a action handler in the component, but I can’t seem to grasp how I would generate the html to add the actions.