From c6d3ddbdb7856dbdda352eee5d40f90b27bf3876 Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento <rodrigoknascimento@gmail.com> Date: Tue, 18 Aug 2015 19:16:07 -0300 Subject: [PATCH] Prevent autolinker to process insice multiline block codes and only link urls containing :// or www. --- .../rocketchat-autolinker/autolinker.coffee | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-autolinker/autolinker.coffee b/packages/rocketchat-autolinker/autolinker.coffee index 6dac64a8547..06ccd95416a 100644 --- a/packages/rocketchat-autolinker/autolinker.coffee +++ b/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 -- GitLab