From 4acff76bdd64d6a47863b46a8fd367fc6692b6c8 Mon Sep 17 00:00:00 2001 From: nickago Date: Tue, 7 Jul 2015 09:24:42 -0700 Subject: Changed linkify to autolinker and wrote gap-bridging code --- web/react/package.json | 1 - web/react/utils/utils.jsx | 36 +++++++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 12 deletions(-) (limited to 'web/react') diff --git a/web/react/package.json b/web/react/package.json index 055530fea..8d9d57fab 100644 --- a/web/react/package.json +++ b/web/react/package.json @@ -8,7 +8,6 @@ "object-assign": "^1.0.0", "react": "^0.12.0", "autolinker": "^0.15.2", - "linkify-it": "^1.1.0", "react-zeroclipboard-mixin": "^0.1.0" }, "devDependencies": { diff --git a/web/react/utils/utils.jsx b/web/react/utils/utils.jsx index f8a7d6450..ce803c2fb 100644 --- a/web/react/utils/utils.jsx +++ b/web/react/utils/utils.jsx @@ -8,7 +8,7 @@ var Constants = require('../utils/constants.jsx'); var ActionTypes = Constants.ActionTypes; var AsyncClient = require('./async_client.jsx'); var client = require('./client.jsx'); -var LinkifyIt = require('linkify-it'); +var Autolinker = require('autolinker'); module.exports.isEmail = function(email) { var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/; @@ -211,16 +211,31 @@ module.exports.getTimestamp = function() { return Date.now(); } +var testUrlMatch = function(text) { + var urlMatcher = new Autolinker.matchParser.MatchParser; + var result = []; + var replaceFn = function(match) { + var linkData = {}; + var matchText = match.getMatchedText(); + + linkData.text = matchText; + linkData.link = matchText.trim().indexOf("http") !== 0 ? "http://" + matchText : matchText; + + result.push(linkData); + } + urlMatcher.replace(text,replaceFn,this); + return result; +} + module.exports.extractLinks = function(text) { var repRegex = new RegExp("
", "g"); - var linkMatcher = new LinkifyIt(); - var matches = linkMatcher.match(text.replace(repRegex, "\n")); + var matches = testUrlMatch(text.replace(repRegex, "\n")); if (!matches) return { "links": null, "text": text }; var links = [] for (var i = 0; i < matches.length; i++) { - links.push(matches[i].url) + links.push(matches[i].link) } return { "links": links, "text": text }; @@ -402,7 +417,6 @@ module.exports.textToJsx = function(text, options) { var implicitKeywords = UserStore.getCurrentMentionKeys(); var lines = text.split("\n"); - var urlMatcher = new LinkifyIt(); for (var i = 0; i < lines.length; i++) { var line = lines[i]; var words = line.split(" "); @@ -434,14 +448,14 @@ module.exports.textToJsx = function(text, options) { } inner.push({prefix}@{name}{suffix} ); - } else if (urlMatcher.test(word)) { - var match = urlMatcher.match(word)[0]; - var link = match.url; + } else if (testUrlMatch(word).length) { + var match = testUrlMatch(word)[0]; + var link = match.link - var prefix = word.substring(0,word.indexOf(match.raw)) - var suffix = word.substring(word.indexOf(match.raw)+match.raw.length); + var prefix = word.substring(0,word.indexOf(match.text)) + var suffix = word.substring(word.indexOf(match.text)+match.text.length); - inner.push({prefix}{match.raw}{suffix} ); + inner.push({prefix}{match.text}{suffix} ); } else if (trimWord.match(hashRegex)) { var suffix = word.match(puncEndRegex); -- cgit v1.2.3-1-g7c22