From 56285ad6c45df03cf6216237e7f92c6bd1f60b28 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Thu, 29 Sep 2016 11:25:08 -0700 Subject: [PATCH] feat(bios): Linkify twitter hashtags and mentions in bios --- .../lib/sidebar-participant-profile.jsx | 28 +++++++++++++++++-- src/regexp-utils.coffee | 3 ++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/internal_packages/participant-profile/lib/sidebar-participant-profile.jsx b/internal_packages/participant-profile/lib/sidebar-participant-profile.jsx index 880b43442..6a52f1476 100644 --- a/internal_packages/participant-profile/lib/sidebar-participant-profile.jsx +++ b/internal_packages/participant-profile/lib/sidebar-participant-profile.jsx @@ -1,6 +1,6 @@ import _ from 'underscore' import React from 'react' -import {DOMUtils, Utils} from 'nylas-exports' +import {DOMUtils, RegExpUtils, Utils} from 'nylas-exports' import {RetinaImg} from 'nylas-component-kit' import ParticipantProfileStore from './participant-profile-store' @@ -134,8 +134,32 @@ export default class SidebarParticipantProfile extends React.Component { _renderBio() { if (!this.state.bio) { return false; } + + const bioNodes = []; + const hashtagOrMentionRegex = RegExpUtils.hashtagOrMentionRegex(); + + let bioRemainder = this.state.bio; + let match = null; + let count = 0; + + /* I thought we were friends. */ + /* eslint no-cond-assign: 0 */ + while (match = hashtagOrMentionRegex.exec(bioRemainder)) { + // the first char of the match is whitespace, match[1] is # or @, match[2] is the tag itself. + bioNodes.push(bioRemainder.substr(0, match.index + 1)); + if (match[1] === '#') { + bioNodes.push({`#${match[2]}`}); + } + if (match[1] === '@') { + bioNodes.push({`@${match[2]}`}); + } + bioRemainder = bioRemainder.substr(match.index + match[0].length); + count += 1; + } + bioNodes.push(bioRemainder); + return ( -

{this.state.bio}

+

{bioNodes}

) } diff --git a/src/regexp-utils.coffee b/src/regexp-utils.coffee index 4de1817a8..d53f91e03 100644 --- a/src/regexp-utils.coffee +++ b/src/regexp-utils.coffee @@ -29,6 +29,9 @@ RegExpUtils = # NOTE: This does not match full urls with `http` protocol components. domainRegex: -> new RegExp("^(?!:\\/\\/)([a-zA-Z#{UnicodeEmailChars}0-9-_]+\\.)*[a-zA-Z#{UnicodeEmailChars}0-9][a-zA-Z#{UnicodeEmailChars}0-9-_]+\\.[a-zA-Z]{2,11}?", 'i') + # http://www.regexpal.com/?fam=95875 + hashtagOrMentionRegex: -> new RegExp(/\s([@#])([\w_-]+)/i) + # https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9780596802837/ch07s16.html ipAddressRegex: -> new RegExp(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/i)