feat(bios): Linkify twitter hashtags and mentions in bios

This commit is contained in:
Ben Gotow 2016-09-29 11:25:08 -07:00
parent 88258cced7
commit 56285ad6c4
2 changed files with 29 additions and 2 deletions

View file

@ -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(<a key={count} href={`https://twitter.com/hashtag/${match[2]}`}>{`#${match[2]}`}</a>);
}
if (match[1] === '@') {
bioNodes.push(<a key={count} href={`https://twitter.com/${match[2]}`}>{`@${match[2]}`}</a>);
}
bioRemainder = bioRemainder.substr(match.index + match[0].length);
count += 1;
}
bioNodes.push(bioRemainder);
return (
<p className="selectable bio">{this.state.bio}</p>
<p className="selectable bio">{bioNodes}</p>
)
}

View file

@ -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)