diff options
author | Corey Hulen <corey@hulen.com> | 2015-10-28 14:44:53 -0700 |
---|---|---|
committer | Corey Hulen <corey@hulen.com> | 2015-10-28 14:44:53 -0700 |
commit | 5f2e01a93815f6a877b179d04e8f9e89ebd3077b (patch) | |
tree | f1921222e92b90a0df623d579904348fe1caa360 /web | |
parent | 89f67cd11dbb71d42a8809976715d6df86d46c95 (diff) | |
parent | 46613a1679347d633ffbe29d7290c17808211d0d (diff) | |
download | chat-5f2e01a93815f6a877b179d04e8f9e89ebd3077b.tar.gz chat-5f2e01a93815f6a877b179d04e8f9e89ebd3077b.tar.bz2 chat-5f2e01a93815f6a877b179d04e8f9e89ebd3077b.zip |
Merge pull request #1213 from mattermost/plt-836
PLT-836 Prevent markdown styled links from embedding gifs
Diffstat (limited to 'web')
-rw-r--r-- | web/react/components/post_body.jsx | 2 | ||||
-rw-r--r-- | web/react/utils/utils.jsx | 68 |
2 files changed, 43 insertions, 27 deletions
diff --git a/web/react/components/post_body.jsx b/web/react/components/post_body.jsx index 45eae8c6a..7138e2cb4 100644 --- a/web/react/components/post_body.jsx +++ b/web/react/components/post_body.jsx @@ -297,7 +297,7 @@ export default class PostBody extends React.Component { } let embed; - if (filenames.length === 0 && this.state.links) { + if (filenames.length === 0 && this.state.links && this.state.links.length > 0) { embed = this.createEmbed(this.state.links[0]); } diff --git a/web/react/utils/utils.jsx b/web/react/utils/utils.jsx index 0d16ef82e..35ce49ae2 100644 --- a/web/react/utils/utils.jsx +++ b/web/react/utils/utils.jsx @@ -231,46 +231,62 @@ export function getTimestamp() { return Date.now(); } -function testUrlMatch(text) { - var urlMatcher = new Autolinker.matchParser.MatchParser({ +// extracts links not styled by Markdown +export function extractLinks(text) { + const urlMatcher = new Autolinker.matchParser.MatchParser({ urls: true, emails: false, twitter: false, phone: false, hashtag: false }); - var result = []; + const links = []; + let replaceText = text; + + // pull out the Markdown code blocks + const codeBlocks = []; + const splitText = replaceText.split('`'); // also handles ``` + for (let i = 1; i < splitText.length; i += 2) { + if (splitText[i].trim() !== '') { + codeBlocks.push(splitText[i]); + } + } + function replaceFn(match) { - var linkData = {}; - var matchText = match.getMatchedText(); + let link = ''; + const matchText = match.getMatchedText(); + const tempText = replaceText; + + const start = replaceText.indexOf(matchText); + const end = start + matchText.length; + + replaceText = replaceText.substring(0, start) + replaceText.substring(end); + + // if it's a Markdown link, just skip it + if (start > 1) { + if (tempText.charAt(start - 2) === ']' && tempText.charAt(start - 1) === '(' && tempText.charAt(end) === ')') { + return; + } + } + + // if it's in a Markdown code block, skip it + for (const i in codeBlocks) { + if (codeBlocks[i].indexOf(matchText) === 0) { + codeBlocks[i] = codeBlocks[i].replace(matchText, ''); + return; + } + } - linkData.text = matchText; if (matchText.trim().indexOf('http') === 0) { - linkData.link = matchText; + link = matchText; } else { - linkData.link = 'http://' + matchText; + link = 'http://' + matchText; } - result.push(linkData); + links.push(link); } urlMatcher.replace(text, replaceFn, this); - return result; -} - -export function extractLinks(text) { - var repRegex = new RegExp('<br>', 'g'); - var matches = testUrlMatch(text.replace(repRegex, '\n')); - - if (!matches.length) { - return {links: null, text: text}; - } - - var links = []; - for (var i = 0; i < matches.length; i++) { - links.push(matches[i].link); - } - - return {links: links, text: text}; + return {links, text}; } export function escapeRegExp(string) { |