FIX: emojis are hard :shrug:

FIX: emojis are hard :shrug:

diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/emoji.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/emoji.js.es6
index ad9e834..f1fc15e 100644
--- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/emoji.js.es6
+++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/emoji.js.es6
@@ -122,16 +122,22 @@ function getEmojiTokenByTranslation(content, pos, state) {
   let found = null;
 
   while (t.length > 0 && pos < content.length) {
+    let matched = false;
     let code = content.charCodeAt(pos);
 
     for (let i = 0; i < t.length; i++) {
       if (t[i][0] === code) {
+        matched = true;
         found = t[i][2];
         t = t[i][1];
         break;
       }
     }
 
+    if (!matched) {
+      return;
+    }
+
     pos++;
   }
 
@@ -164,26 +170,32 @@ function getEmojiTokenByTranslation(content, pos, state) {
   }
 }
 
-function applyEmoji(content, state, emojiUnicodeReplacer, enableShortcuts, inlineEmoji) {
+function applyEmoji(
+  content,
+  state,
+  emojiUnicodeReplacer,
+  enableShortcuts,
+  inlineEmoji
+) {
   let result = null;
-  let contentToken = null;
   let start = 0;
 
   if (emojiUnicodeReplacer) {
     content = emojiUnicodeReplacer(content);
   }
 
-  let endToken = content.length;
+  let end = content.length;
 
   for (let i = 0; i < content.length - 1; i++) {
     let offset = 0;
     let token = null;
-    const emojiName = getEmojiName(content, i, state, inlineEmoji);
 
-    if (emojiName) {
-      token = getEmojiTokenByName(emojiName, state);
+    const name = getEmojiName(content, i, state, inlineEmoji);
+
+    if (name) {
+      token = getEmojiTokenByName(name, state);
       if (token) {
-        offset = emojiName.length + 2;
+        offset = name.length + 2;
       }
     }
 
@@ -199,22 +211,24 @@ function applyEmoji(content, state, emojiUnicodeReplacer, enableShortcuts, inlin
 
     if (token) {
       result = result || [];
+
       if (i - start > 0) {
-        contentToken = new state.Token("text", "", 0);
-        contentToken.content = content.slice(start, i);
-        result.push(contentToken);
+        let text = new state.Token("text", "", 0);
+        text.content = content.slice(start, i);
+        result.push(text);
       }
 
       result.push(token);
-      i += offset;
-      endToken = start = i;
+
+      end = start = i + offset;
+      i += offset - 1;
     }
   }
 
-  if (endToken < content.length) {
-    contentToken = new state.Token("text", "", 0);
-    contentToken.content = content.slice(endToken);
-    result.push(contentToken);
+  if (end < content.length) {
+    let text = new state.Token("text", "", 0);
+    text.content = content.slice(end);
+    result.push(text);
   }
 
   return result;

GitHub
sha: 5ac1e3d4

What was the problem that was being fixed here?

This was to fix the build due to a bug I introduced in

1 Like