diff options
author | hmhealey <harrisonmhealey@gmail.com> | 2015-09-10 15:19:37 -0400 |
---|---|---|
committer | hmhealey <harrisonmhealey@gmail.com> | 2015-09-14 09:19:26 -0400 |
commit | 3ab2814af39df15d66e3fa149b5024692b3c6310 (patch) | |
tree | 40d6d26b6bbb61872b6201a39841ba0d9e40a426 /web/react/utils/text_formatting.jsx | |
parent | 5d28ffa4447d3eb616536620df6678944918d3dd (diff) | |
download | chat-3ab2814af39df15d66e3fa149b5024692b3c6310.tar.gz chat-3ab2814af39df15d66e3fa149b5024692b3c6310.tar.bz2 chat-3ab2814af39df15d66e3fa149b5024692b3c6310.zip |
Added autolinking of urls to the new text formatting
Diffstat (limited to 'web/react/utils/text_formatting.jsx')
-rw-r--r-- | web/react/utils/text_formatting.jsx | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/web/react/utils/text_formatting.jsx b/web/react/utils/text_formatting.jsx index 90ff7f41f..db1568bab 100644 --- a/web/react/utils/text_formatting.jsx +++ b/web/react/utils/text_formatting.jsx @@ -1,6 +1,7 @@ // Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. // See License.txt for license information. +const Autolinker = require('autolinker'); const Constants = require('./constants.jsx'); const UserStore = require('../stores/user_store.jsx'); @@ -8,8 +9,7 @@ export function formatText(text, options = {}) { let output = sanitize(text); let tokens = new Map(); - // TODO strip urls first - + output = stripLinks(output, tokens); output = stripAtMentions(output, tokens); output = stripSelfMentions(output, tokens); @@ -19,7 +19,6 @@ export function formatText(text, options = {}) { return output; - // TODO autolink urls // TODO highlight search terms // TODO autolink hashtags @@ -37,6 +36,38 @@ export function sanitize(text) { return output; } +function stripLinks(text, tokens) { + function stripLink(autolinker, match) { + let text = match.getMatchedText(); + let url = text; + if (!url.startsWith('http')) { + url = `http://${text}`; + } + + const index = tokens.size; + const alias = `LINK${index}`; + + tokens.set(alias, { + value: `<a class='theme' target='_blank' href='${url}'>${text}</a>`, + originalText: text + }); + + return alias; + } + + // we can't just use a static autolinker because we need to set replaceFn + const autolinker = new Autolinker({ + urls: true, + email: false, + phone: false, + twitter: false, + hashtag: false, + replaceFn: stripLink + }); + + return autolinker.link(text); +} + function stripAtMentions(text, tokens) { let output = text; @@ -47,7 +78,7 @@ function stripAtMentions(text, tokens) { tokens.set(alias, { value: `<a class='mention-link' href='#' data-mention='${username}'>${mention}</a>`, - originalText: mention + oreplaceLinkriginalText: mention }); return prefix + alias; |