From ffe34aa1544172b42658cd628cdcc4036d6ff403 Mon Sep 17 00:00:00 2001
From: hmhealey <harrisonmhealey@gmail.com>
Date: Tue, 22 Sep 2015 13:53:43 -0400
Subject: Restored changes which remove emojify and switch to our own version

---
 web/react/utils/text_formatting.jsx | 6 ++++++
 1 file changed, 6 insertions(+)

(limited to 'web/react/utils/text_formatting.jsx')

diff --git a/web/react/utils/text_formatting.jsx b/web/react/utils/text_formatting.jsx
index 4e390f708..be82f7b9c 100644
--- a/web/react/utils/text_formatting.jsx
+++ b/web/react/utils/text_formatting.jsx
@@ -3,6 +3,7 @@
 
 const Autolinker = require('autolinker');
 const Constants = require('./constants.jsx');
+const Emoticons = require('./emoticons.jsx');
 const Markdown = require('./markdown.jsx');
 const UserStore = require('../stores/user_store.jsx');
 const Utils = require('./utils.jsx');
@@ -17,6 +18,7 @@ const markdownRenderer = new Markdown.MattermostMarkdownRenderer();
 // - searchTerm - If specified, this word is highlighted in the resulting html. Defaults to nothing.
 // - mentionHighlight - Specifies whether or not to highlight mentions of the current user. Defaults to true.
 // - singleline - Specifies whether or not to remove newlines. Defaults to false.
+// - emoticons - Enables emoticon parsing. Defaults to true.
 // - markdown - Enables markdown parsing. Defaults to true.
 export function formatText(text, options = {}) {
     if (!('markdown' in options)) {
@@ -34,6 +36,10 @@ export function formatText(text, options = {}) {
     const tokens = new Map();
 
     // replace important words and phrases with tokens
+    if (!('emoticons' in options) || options.emoticon) {
+        output = Emoticons.handleEmoticons(output, tokens);
+    }
+
     output = autolinkUrls(output, tokens, !!options.markdown);
     output = autolinkAtMentions(output, tokens);
     output = autolinkHashtags(output, tokens);
-- 
cgit v1.2.3-1-g7c22


From 1709532ae6f48df0c152afb6a58597cd07d6df49 Mon Sep 17 00:00:00 2001
From: hmhealey <harrisonmhealey@gmail.com>
Date: Tue, 22 Sep 2015 14:14:54 -0400
Subject: Moved emoticon parsing to happen after url parsing so that we don't
 have smilies accidentally occuring in urls

---
 web/react/utils/text_formatting.jsx | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

(limited to 'web/react/utils/text_formatting.jsx')

diff --git a/web/react/utils/text_formatting.jsx b/web/react/utils/text_formatting.jsx
index be82f7b9c..940bd0485 100644
--- a/web/react/utils/text_formatting.jsx
+++ b/web/react/utils/text_formatting.jsx
@@ -36,14 +36,14 @@ export function formatText(text, options = {}) {
     const tokens = new Map();
 
     // replace important words and phrases with tokens
-    if (!('emoticons' in options) || options.emoticon) {
-        output = Emoticons.handleEmoticons(output, tokens);
-    }
-
     output = autolinkUrls(output, tokens, !!options.markdown);
     output = autolinkAtMentions(output, tokens);
     output = autolinkHashtags(output, tokens);
 
+    if (!('emoticons' in options) || options.emoticon) {
+        output = Emoticons.handleEmoticons(output, tokens);
+    }
+
     if (options.searchTerm) {
         output = highlightSearchTerm(output, tokens, options.searchTerm);
     }
-- 
cgit v1.2.3-1-g7c22


From d025da5da9b6b6bf3c0003191f07a8184e545f8a Mon Sep 17 00:00:00 2001
From: hmhealey <harrisonmhealey@gmail.com>
Date: Tue, 22 Sep 2015 14:28:43 -0400
Subject: Changed hashtag regex to only accept hashtags that begin with a
 letter

---
 web/react/utils/text_formatting.jsx | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'web/react/utils/text_formatting.jsx')

diff --git a/web/react/utils/text_formatting.jsx b/web/react/utils/text_formatting.jsx
index 940bd0485..089fdd4f9 100644
--- a/web/react/utils/text_formatting.jsx
+++ b/web/react/utils/text_formatting.jsx
@@ -247,7 +247,7 @@ function autolinkHashtags(text, tokens) {
         return prefix + alias;
     }
 
-    return output.replace(/(^|\W)(#[a-zA-Z0-9.\-_]+)\b/g, replaceHashtagWithToken);
+    return output.replace(/(^|\W)(#[a-zA-Z][a-zA-Z0-9.\-_]*)\b/g, replaceHashtagWithToken);
 }
 
 function highlightSearchTerm(text, tokens, searchTerm) {
-- 
cgit v1.2.3-1-g7c22


From 1afa1d37c77b9508a4256e32c4e1fea27113033f Mon Sep 17 00:00:00 2001
From: hmhealey <harrisonmhealey@gmail.com>
Date: Tue, 22 Sep 2015 14:30:50 -0400
Subject: Moved text formatting code to only occur inside of markdown text
 nodes

---
 web/react/utils/text_formatting.jsx | 39 +++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 19 deletions(-)

(limited to 'web/react/utils/text_formatting.jsx')

diff --git a/web/react/utils/text_formatting.jsx b/web/react/utils/text_formatting.jsx
index 089fdd4f9..41dc98084 100644
--- a/web/react/utils/text_formatting.jsx
+++ b/web/react/utils/text_formatting.jsx
@@ -10,8 +10,6 @@ const Utils = require('./utils.jsx');
 
 const marked = require('marked');
 
-const markdownRenderer = new Markdown.MattermostMarkdownRenderer();
-
 // Performs formatting of user posts including highlighting mentions and search terms and converting urls, hashtags, and
 // @mentions to links by taking a user's message and returning a string of formatted html. Also takes a number of options
 // as part of the second parameter:
@@ -25,14 +23,30 @@ export function formatText(text, options = {}) {
         options.markdown = true;
     }
 
-    // wait until marked can sanitize the html so that we don't break markdown block quotes
     let output;
-    if (!options.markdown) {
-        output = sanitizeHtml(text);
+
+    if (options.markdown) {
+        // the markdown renderer will call doFormatText as necessary so just call marked
+        output = marked(text, {
+            renderer: new Markdown.MattermostMarkdownRenderer(null, options),
+            sanitize: true
+        });
     } else {
-        output = text;
+        output = sanitizeHtml(text);
+        output = doFormatText(output, options);
     }
 
+    // replace newlines with spaces if necessary
+    if (options.singleline) {
+        output = replaceNewlines(output);
+    }
+
+    return output;
+}
+
+export function doFormatText(text, options) {
+    let output = text;
+
     const tokens = new Map();
 
     // replace important words and phrases with tokens
@@ -52,22 +66,9 @@ export function formatText(text, options = {}) {
         output = highlightCurrentMentions(output, tokens);
     }
 
-    // perform markdown parsing while we have an html-free input string
-    if (options.markdown) {
-        output = marked(output, {
-            renderer: markdownRenderer,
-            sanitize: true
-        });
-    }
-
     // reinsert tokens with formatted versions of the important words and phrases
     output = replaceTokens(output, tokens);
 
-    // replace newlines with html line breaks
-    if (options.singleline) {
-        output = replaceNewlines(output);
-    }
-
     return output;
 }
 
-- 
cgit v1.2.3-1-g7c22


From 55f11ba8719d133103bc1d4779bb8cb66d591b58 Mon Sep 17 00:00:00 2001
From: hmhealey <harrisonmhealey@gmail.com>
Date: Tue, 22 Sep 2015 14:34:59 -0400
Subject: Removed extra handling of markdown links when doing autolinking

---
 web/react/utils/text_formatting.jsx | 36 +++++-------------------------------
 1 file changed, 5 insertions(+), 31 deletions(-)

(limited to 'web/react/utils/text_formatting.jsx')

diff --git a/web/react/utils/text_formatting.jsx b/web/react/utils/text_formatting.jsx
index 41dc98084..56bf49c3f 100644
--- a/web/react/utils/text_formatting.jsx
+++ b/web/react/utils/text_formatting.jsx
@@ -19,13 +19,9 @@ const marked = require('marked');
 // - emoticons - Enables emoticon parsing. Defaults to true.
 // - markdown - Enables markdown parsing. Defaults to true.
 export function formatText(text, options = {}) {
-    if (!('markdown' in options)) {
-        options.markdown = true;
-    }
-
     let output;
 
-    if (options.markdown) {
+    if (!('markdown' in options) || options.markdown) {
         // the markdown renderer will call doFormatText as necessary so just call marked
         output = marked(text, {
             renderer: new Markdown.MattermostMarkdownRenderer(null, options),
@@ -44,13 +40,14 @@ export function formatText(text, options = {}) {
     return output;
 }
 
+// Performs most of the actual formatting work for formatText. Not intended to be called normally.
 export function doFormatText(text, options) {
     let output = text;
 
     const tokens = new Map();
 
     // replace important words and phrases with tokens
-    output = autolinkUrls(output, tokens, !!options.markdown);
+    output = autolinkUrls(output, tokens);
     output = autolinkAtMentions(output, tokens);
     output = autolinkHashtags(output, tokens);
 
@@ -85,7 +82,7 @@ export function sanitizeHtml(text) {
     return output;
 }
 
-function autolinkUrls(text, tokens, markdown) {
+function autolinkUrls(text, tokens) {
     function replaceUrlWithToken(autolinker, match) {
         const linkText = match.getMatchedText();
         let url = linkText;
@@ -115,30 +112,7 @@ function autolinkUrls(text, tokens, markdown) {
         replaceFn: replaceUrlWithToken
     });
 
-    let output = text;
-
-    // temporarily replace markdown links if markdown is enabled so that we don't accidentally parse them twice
-    const markdownLinkTokens = new Map();
-    if (markdown) {
-        function replaceMarkdownLinkWithToken(markdownLink) {
-            const index = markdownLinkTokens.size;
-            const alias = `MM_MARKDOWNLINK${index}`;
-
-            markdownLinkTokens.set(alias, {value: markdownLink});
-
-            return alias;
-        }
-
-        output = output.replace(/\]\([^\)]*\)/g, replaceMarkdownLinkWithToken);
-    }
-
-    output = autolinker.link(output);
-
-    if (markdown) {
-        output = replaceTokens(output, markdownLinkTokens);
-    }
-
-    return output;
+    return autolinker.link(text);
 }
 
 function autolinkAtMentions(text, tokens) {
-- 
cgit v1.2.3-1-g7c22

