Skip to content

Instantly share code, notes, and snippets.

@mathieuancelin
Last active April 7, 2016 16:02
Show Gist options
  • Select an option

  • Save mathieuancelin/57e275f1d6d0a100069699c5f246ec5f to your computer and use it in GitHub Desktop.

Select an option

Save mathieuancelin/57e275f1d6d0a100069699c5f246ec5f to your computer and use it in GitHub Desktop.
var AutoImage = function AutoImage(message) {
if (_.trim(message.html)) {
message.html = message.html.split(/ /).map(function (part, index) {
if (part && part.length && part.length > 0) {
var codeMatch = part.match(/(?:```(\w*)[\n ]?([\s\S]*?)```+?)|(?:`(?:[^`]+)`)/);
var imgHrefMatch = part.match(/href=/i);
if (!codeMatch && !imgHrefMatch) {
return part.replace(/(https?:\/\/.*\.(?:png|jpeg|jpg|gif))/i, '<img src="$1" />');
} else {
return part;
}
}
}).join(' ');
return message;
}
};
RocketChat.callbacks.add('renderMessage', AutoImage);
Package.describe({
name: 'rocketchat:autoimage',
version: '0.0.1',
summary: 'Message pre-processor that will transform image links on messages',
git: ''
});
Package.onUse(function(api) {
api.versionsFrom('1.0');
api.use([
'rocketchat:lib'
]);
api.addFiles('autoimage.js', ['server','client']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment