Packages: highlight, autolinker, markdown, emojione

pull/176/head
Marcelo Schmidt 10 years ago
parent cca26bac54
commit 96702b920a
  1. 4
      .meteor/packages
  2. 4
      .meteor/versions
  3. 49
      client/views/app/chatMessageDashboard.coffee
  4. 2
      client/views/app/chatMessageDashboard.html
  5. 13
      packages/rocketchat-autolinker/autolinker.coffee
  6. 22
      packages/rocketchat-autolinker/package.js
  7. 13
      packages/rocketchat-emojione/emojione.coffee
  8. 22
      packages/rocketchat-emojione/package.js
  9. 38
      packages/rocketchat-highlight/highlight.coffee
  10. 22
      packages/rocketchat-highlight/package.js
  11. 19
      packages/rocketchat-markdown/markdown.coffee
  12. 21
      packages/rocketchat-markdown/package.js
  13. 7
      packages/rocketchat-me/me.coffee
  14. 23
      packages/rocketchat-mentions/client.coffee
  15. 1
      packages/rocketchat-mentions/package.js
  16. 4
      packages/rocketchat-mentions/server.coffee

@ -45,6 +45,10 @@ rocketchat:file
rocketchat:lib
rocketchat:me
rocketchat:mentions
rocketchat:highlight
rocketchat:autolinker
rocketchat:markdown
rocketchat:emojione
simple:highlight.js
tap:i18n
tmeasday:crypto-md5

@ -91,8 +91,12 @@ reactive-dict@1.1.0
reactive-var@1.0.5
reload@1.1.3
retry@1.0.3
rocketchat:autolinker@0.0.1
rocketchat:emojione@0.0.1
rocketchat:file@0.0.1
rocketchat:highlight@0.0.1
rocketchat:lib@0.0.1
rocketchat:markdown@0.0.1
rocketchat:me@0.0.1
rocketchat:mentions@0.0.1
routepolicy@1.0.5

@ -16,52 +16,9 @@ Template.chatMessageDashboard.helpers
preProcessingMessage: ->
if _.trim(this.msg) isnt ''
msg = this.html
# Separate text in code blocks and non code blocks
msgParts = msg.split(/(```.*\n[\s\S]*?\n```)/)
for part, index in msgParts
# Verify if this part is code
codeMatch = part.match(/```(.*)\n([\s\S]*?)\n```/)
if codeMatch?
# Process highlight if this part is code
lang = codeMatch[1]
code = codeMatch[2]
if lang not in hljs.listLanguages()
result = hljs.highlightAuto code
else
result = hljs.highlight lang, code
msgParts[index] = "<pre><code class='hljs " + result.language + "'>" + result.value + "</code></pre>"
else
# Escape html and fix line breaks for non code blocks
part = _.escapeHTML part
part = part.replace /\n/g, '<br/>'
msgParts[index] = part
# Re-mount message
msg = msgParts.join('')
# Process links in message
msg = Autolinker.link(msg, { stripPrefix: false, twitter: false })
# Process MD like for strong, italic and strike
msg = msg.replace(/\*([^*]+)\*/g, '<strong>$1</strong>')
msg = msg.replace(/\_([^_]+)\_/g, '<i>$1</i>')
msg = msg.replace(/\~([^_]+)\~/g, '<strike>$1</strike>')
# Highlight mentions
if not this.mentions? or this.mentions.length is 0
mentions = _.map this.mentions, (mention) ->
return mention.username or mention
mentions = mentions.join('|')
msg = msg.replace new RegExp("(?:^|\\s)(@(#{mentions}))(?:\\s|$)", 'g'), (match, mention, username) ->
return match.replace mention, "<a href=\"\" class=\"mention-link\" data-username=\"#{username}\">#{mention}</a>"
return msg
this.html = this.msg
msg = RocketChat.callbacks.run 'renderMessage', this
return msg.html
message: ->
switch this.t

@ -42,7 +42,7 @@
</span>
{{/if}}
<div>
{{#emojione}}{{preProcessingMessage}}{{/emojione}}
{{{preProcessingMessage}}}
</div>
{{/if}}
{{/if}}

@ -0,0 +1,13 @@
###
# AutoLinker is a named function that will replace links on messages
# @param {Object} message - The message object
###
class AutoLinker
constructor: (message) ->
if _.trim message.html
message.html = Autolinker.link(message.html, { stripPrefix: false, twitter: false })
return message
RocketChat.callbacks.add 'renderMessage', AutoLinker

@ -0,0 +1,22 @@
Package.describe({
name: 'rocketchat:autolinker',
version: '0.0.1',
summary: 'Message pre-processor that will translate links on messages',
git: ''
});
Package.onUse(function(api) {
api.versionsFrom('1.0');
api.use([
'coffeescript',
'konecty:autolinker',
'rocketchat:lib@0.0.1'
]);
api.addFiles('autolinker.coffee', ['server','client']);
});
Package.onTest(function(api) {
});

@ -0,0 +1,13 @@
###
# Emojione is a named function that will replace emojis
# @param {Object} message - The message object
###
class Emojione
constructor: (message) ->
if _.trim message.html
message.html = emojione.toImage(message.html)
return message
RocketChat.callbacks.add 'renderMessage', Emojione

@ -0,0 +1,22 @@
Package.describe({
name: 'rocketchat:emojione',
version: '0.0.1',
summary: 'Message pre-processor that will translate emojis',
git: ''
});
Package.onUse(function(api) {
api.versionsFrom('1.0');
api.use([
'coffeescript',
'qnub:emojione',
'rocketchat:lib@0.0.1'
]);
api.addFiles('emojione.coffee', ['server','client']);
});
Package.onTest(function(api) {
});

@ -0,0 +1,38 @@
###
# Highlight is a named function that will highlight ``` messages
# @param {Object} message - The message object
###
class Highlight
# If message starts with ```, replace it for text formatting
constructor: (message) ->
if _.trim message.html
# Separate text in code blocks and non code blocks
msgParts = message.html.split(/(```.*\n[\s\S]*?\n```)/)
for part, index in msgParts
# Verify if this part is code
codeMatch = part.match(/```(.*)\n([\s\S]*?)\n```/)
if codeMatch?
# Process highlight if this part is code
lang = codeMatch[1]
code = codeMatch[2]
if lang not in hljs.listLanguages()
result = hljs.highlightAuto code
else
result = hljs.highlight lang, code
msgParts[index] = "<pre><code class='hljs " + result.language + "'>" + result.value + "</code></pre>"
else
# Escape html and fix line breaks for non code blocks
part = _.escapeHTML part
part = part.replace /\n/g, '<br/>'
msgParts[index] = part
# Re-mount message
message.html = msgParts.join('')
return message
RocketChat.callbacks.add 'renderMessage', Highlight, RocketChat.callbacks.priority.HIGH

@ -0,0 +1,22 @@
Package.describe({
name: 'rocketchat:highlight',
version: '0.0.1',
summary: 'Message pre-processor that will highlight code syntax',
git: ''
});
Package.onUse(function(api) {
api.versionsFrom('1.0');
api.use([
'coffeescript',
'simple:highlight.js',
'rocketchat:lib@0.0.1'
]);
api.addFiles('highlight.coffee', ['server','client']);
});
Package.onTest(function(api) {
});

@ -0,0 +1,19 @@
###
# Markdown is a named function that will parse markdown syntax
# @param {Object} message - The message object
###
class Markdown
constructor: (message) ->
msg = message.html or ''
# Process MD like for strong, italic and strike
msg = msg.replace(/\*([^*]+)\*/g, '<strong>$1</strong>')
msg = msg.replace(/\_([^_]+)\_/g, '<i>$1</i>')
msg = msg.replace(/\~([^_]+)\~/g, '<strike>$1</strike>')
message.html = msg
return message
RocketChat.callbacks.add 'renderMessage', Markdown

@ -0,0 +1,21 @@
Package.describe({
name: 'rocketchat:markdown',
version: '0.0.1',
summary: 'Message pre-processor that will parse markdown syntax',
git: ''
});
Package.onUse(function(api) {
api.versionsFrom('1.0');
api.use([
'coffeescript',
'rocketchat:lib@0.0.1'
]);
api.addFiles('markdown.coffee', ['server','client']);
});
Package.onTest(function(api) {
});

@ -5,9 +5,10 @@
class Me
constructor: (message) ->
# If message starts with /me, replace it for text formatting
if message.msg.indexOf('/me ') is 0
message.html = '_' + message.html.replace('/me ','') + '_'
if _.trim message.html
# If message starts with /me, replace it for text formatting
if message.html.indexOf('/me ') is 0
message.html = '_' + message.html.replace('/me ','') + '_'
return message
RocketChat.callbacks.add 'renderMessage', Me

@ -0,0 +1,23 @@
###
# Mentions is a named function that will process Mentions
# @param {Object} message - The message object
###
class MentionsClient
constructor: (message) ->
if _.trim message.html
msg = message.html
mentions = []
message.msg.replace /(?:^|\s|\n)(?:@)([A-Za-z0-9-_.]+)/g, (match, mention) ->
mentions.push mention
if mentions.length isnt 0
mentions = _.unique mentions
mentions = mentions.join('|')
msg = msg.replace new RegExp("(?:^|\\s)(@(#{mentions}))(?:\\s|$)", 'g'), (match, mention, username) ->
return match.replace mention, "<a href=\"\" class=\"mention-link\" data-username=\"#{username}\">#{mention}</a>"
message.html = msg
return message
RocketChat.callbacks.add 'renderMessage', MentionsClient

@ -14,6 +14,7 @@ Package.onUse(function(api) {
]);
api.addFiles('server.coffee', 'server');
api.addFiles('client.coffee', 'client');
});
Package.onTest(function(api) {

@ -3,7 +3,7 @@
# @param {Object} message - The message object
###
class Mentions
class MentionsServer
constructor: (message) ->
# If message starts with /me, replace it for text formatting
mentions = []
@ -19,4 +19,4 @@ class Mentions
message.mentions = verifiedMentions
return message
RocketChat.callbacks.add 'beforeSaveMessage', Mentions
RocketChat.callbacks.add 'beforeSaveMessage', MentionsServer
Loading…
Cancel
Save