rikukissa
8/22/2014 - 10:52 AM

index.js

.config(function(MessageFormatterProvider) {

  // Initial formatters and example usage

  // Removes all tags
  MessageFormatterProvider.addFormatter(function(message) {
    return message.replace(/<[^>]+>/gm, '');
  });

  // Replace line breaks with br - tag
  MessageFormatterProvider.addFormatter(function(message) {
    return message.replace(/\r\n?|\n/g, '<br />');
  });

  // Replaces urls with anchor tags
  MessageFormatterProvider.addFormatter(function(message) {
    return autolinker.link(message);
  });

  // Replaces anchor text with image if url extension matches image extensions
  var imageExtensionRegex = /(?:jpe?g|gif|png)$/i;
  MessageFormatterProvider.addFormatter(function(message) {
    var messageElem = angular.element('<span>' + message + '</span>');

    _.forEach(messageElem.find('a'), function(node) {
      var el = angular.element(node);
      var href = el.attr('href');

      if(!href.match(imageExtensionRegex)) {
        return;
      }
      el.html('<img src="' + href + '" alt="" />')
    });

    return messageElem.html();
  });
});