From ff01fc7337eedc51c84610a8c6bf578e4f42b9df Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Thu, 31 Mar 2016 18:16:55 -0300 Subject: Replace Autolinker in extractLinks with a pattern --- webapp/utils/utils.jsx | 37 ++++++------------------------------- 1 file changed, 6 insertions(+), 31 deletions(-) diff --git a/webapp/utils/utils.jsx b/webapp/utils/utils.jsx index b248368fc..9af36f9dd 100644 --- a/webapp/utils/utils.jsx +++ b/webapp/utils/utils.jsx @@ -14,7 +14,6 @@ var ActionTypes = Constants.ActionTypes; import * as Client from './client.jsx'; import * as AsyncClient from './async_client.jsx'; import * as client from './client.jsx'; -import Autolinker from 'autolinker'; import React from 'react'; import {browserHistory} from 'react-router'; @@ -315,13 +314,8 @@ export function getTimestamp() { // extracts links not styled by Markdown export function extractLinks(text) { - text; // eslint-disable-line no-unused-expressions - Autolinker; // eslint-disable-line no-unused-expressions - - // skip this operation because autolinker is having issues - return []; - - /*const links = []; + const pattern = /(^|[\s\n]|)((?:https?|ftp):\/\/[\-A-Z0-9+\u0026\u2019@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|])/gi; + let links; let inText = text; // strip out code blocks @@ -330,32 +324,13 @@ export function extractLinks(text) { // strip out inline markdown images inText = inText.replace(/!\[[^\]]*\]\([^\)]*\)/g, ''); - function replaceFn(autolinker, match) { - let link = ''; - const matchText = match.getMatchedText(); - - if (matchText.trim().indexOf('http') === 0) { - link = matchText; - } else { - link = 'http://' + matchText; - } - - links.push(link); - } + links = inText.match(pattern) || []; - Autolinker.link( - inText, - { - replaceFn, - urls: {schemeMatches: true, wwwMatches: true, tldMatches: false}, - emails: false, - twitter: false, - phone: false, - hashtag: false + return links.map( + (url) => { + return url.trim(); } ); - - return links;*/ } export function escapeRegExp(string) { -- cgit v1.2.3-1-g7c22 From 9ec459526a2bf4d119ab779473c9606c524b6131 Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Fri, 1 Apr 2016 12:40:07 -0300 Subject: Rename extractLinks to extractFirstLink --- webapp/components/post_body_additional_content.jsx | 2 +- webapp/utils/utils.jsx | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/webapp/components/post_body_additional_content.jsx b/webapp/components/post_body_additional_content.jsx index 2cd82f213..452597dde 100644 --- a/webapp/components/post_body_additional_content.jsx +++ b/webapp/components/post_body_additional_content.jsx @@ -70,7 +70,7 @@ export default class PostBodyAdditionalContent extends React.Component { return this.getSlackAttachment(); } - const link = Utils.extractLinks(this.props.post.message)[0]; + const link = Utils.extractFirstLink(this.props.post.message); if (!link) { return null; } diff --git a/webapp/utils/utils.jsx b/webapp/utils/utils.jsx index 9af36f9dd..dc9291ab7 100644 --- a/webapp/utils/utils.jsx +++ b/webapp/utils/utils.jsx @@ -313,9 +313,8 @@ export function getTimestamp() { } // extracts links not styled by Markdown -export function extractLinks(text) { - const pattern = /(^|[\s\n]|)((?:https?|ftp):\/\/[\-A-Z0-9+\u0026\u2019@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|])/gi; - let links; +export function extractFirstLink(text) { + const pattern = /(^|[\s\n]|)((?:https?|ftp):\/\/[\-A-Z0-9+\u0026\u2019@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|])/i; let inText = text; // strip out code blocks @@ -324,13 +323,12 @@ export function extractLinks(text) { // strip out inline markdown images inText = inText.replace(/!\[[^\]]*\]\([^\)]*\)/g, ''); - links = inText.match(pattern) || []; + const match = pattern.exec(inText); + if (match) { + return match[0].trim(); + } - return links.map( - (url) => { - return url.trim(); - } - ); + return ''; } export function escapeRegExp(string) { -- cgit v1.2.3-1-g7c22