summaryrefslogtreecommitdiffstats
path: root/web/react
diff options
context:
space:
mode:
authorhmhealey <harrisonmhealey@gmail.com>2015-09-10 11:05:31 -0400
committerhmhealey <harrisonmhealey@gmail.com>2015-09-14 09:19:26 -0400
commit91f258c725bd749fc44b177e131e61c936e7a88b (patch)
tree904186fccc8eee792626f9f6473de6a1a3ca9fec /web/react
parent2d2939576cce38b3f4517d243d51dd6998eda42f (diff)
downloadchat-91f258c725bd749fc44b177e131e61c936e7a88b.tar.gz
chat-91f258c725bd749fc44b177e131e61c936e7a88b.tar.bz2
chat-91f258c725bd749fc44b177e131e61c936e7a88b.zip
Added handling for @mentions to the new text formatting
Diffstat (limited to 'web/react')
-rw-r--r--web/react/utils/text_formatting.jsx63
1 files changed, 54 insertions, 9 deletions
diff --git a/web/react/utils/text_formatting.jsx b/web/react/utils/text_formatting.jsx
index 2e1416d1d..111155c3e 100644
--- a/web/react/utils/text_formatting.jsx
+++ b/web/react/utils/text_formatting.jsx
@@ -1,12 +1,21 @@
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
-const Constants = require('../utils/constants.jsx');
+const Constants = require('./constants.jsx');
const UserStore = require('../stores/user_store.jsx');
export function formatText(text, options = {}) {
let output = sanitize(text);
+ let atMentions;
+ [output, atMentions] = stripAtMentions(output);
+
+ output = reinsertAtMentions(output, atMentions);
+
+ output = replaceNewlines(output, options.singleline);
+
+ return output;
+
// TODO autolink @mentions
// TODO highlight mentions of self
// TODO autolink urls
@@ -14,14 +23,6 @@ export function formatText(text, options = {}) {
// TODO autolink hashtags
// TODO leave space for markdown
-
- if (options.singleline) {
- output = output.replace('\n', ' ');
- } else {
- output = output.replace('\n', '<br />');
- }
-
- return output;
}
export function sanitize(text) {
@@ -34,3 +35,47 @@ export function sanitize(text) {
return output;
}
+
+function stripAtMentions(text) {
+ let output = text;
+ let atMentions = new Map();
+
+ function stripAtMention(fullMatch, prefix, mentionText, username) {
+ if (Constants.SPECIAL_MENTIONS.indexOf(username) !== -1 || UserStore.getProfileByUsername(username)) {
+ const index = atMentions.size;
+ const alias = `ATMENTION${index}`;
+
+ atMentions.set(alias, {mentionText: mentionText, username: username});
+
+ return prefix + alias;
+ } else {
+ return fullMatch;
+ }
+ }
+
+ output = output.replace(/(^|\s)(@([a-z0-9.\-_]+))/g, stripAtMention);
+
+ return [output, atMentions];
+}
+window.stripAtMentions = stripAtMentions;
+
+function reinsertAtMentions(text, atMentions) {
+ let output = text;
+
+ function reinsertAtMention(replacement, alias) {
+ output = output.replace(alias, `<a class='mention-link' href='#' data-mention=${replacement.username}>${replacement.mentionText}</a>`);
+ }
+
+ atMentions.forEach(reinsertAtMention);
+
+ return output;
+}
+window.reinsertAtMentions = reinsertAtMentions;
+
+function replaceNewlines(text, singleline) {
+ if (!singleline) {
+ return text.replace(/\n/g, '<br />');
+ } else {
+ return text.replace(/\n/g, ' ');
+ }
+}