Prevent autolinker to process insice multiline block codes and only link urls containing :// or www.

pull/499/head
Rodrigo Nascimento 11 years ago
parent 8063ba1a17
commit c6d3ddbdb7
  1. 20
      packages/rocketchat-autolinker/autolinker.coffee

@ -6,8 +6,24 @@
class AutoLinker
constructor: (message) ->
if _.trim message.html
message.html = Autolinker.link(message.html, { stripPrefix: false, twitter: false })
# Separate text in code blocks and non code blocks
msgParts = message.html.split(/(```\w*[\n\ ]?[\s\S]*?```+?)/)
for part, index in msgParts
# Verify if this part is code
codeMatch = part.match(/```(\w*)[\n\ ]?([\s\S]*?)```+?/)
if not codeMatch?
msgParts[index] = Autolinker.link part,
stripPrefix: false
twitter: false
replaceFn: (autolinker, match) ->
if match.getType() is 'url'
return /(:\/\/|www\.).+/.test match.matchedText
return true
# Re-mount message
message.html = msgParts.join('')
return message
RocketChat.callbacks.add 'renderMessage', AutoLinker

Loading…
Cancel
Save